Compare commits
2 Commits
a91059a325
...
41e2594f1f
Author | SHA1 | Date |
---|---|---|
elfeiin | 41e2594f1f | |
elfeiin | 88936545f7 |
74
build.rs
74
build.rs
|
@ -5,12 +5,34 @@ use std::collections::HashMap;
|
|||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::ops::{Add, Sub};
|
||||
use std::ops::{Add, Div, Mul, Sub};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Default, Hash, Eq, PartialEq, PartialOrd, Ord, Clone, Copy)]
|
||||
pub struct Matrix([i8; 7]);
|
||||
|
||||
impl Mul<i8> for Matrix {
|
||||
type Output = Matrix;
|
||||
fn mul(self, rhs: i8) -> Self::Output {
|
||||
let mut output = Matrix::default();
|
||||
for (i, v) in self.0.iter().enumerate() {
|
||||
output.0[i] = v * rhs;
|
||||
}
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<i8> for Matrix {
|
||||
type Output = Matrix;
|
||||
fn div(self, rhs: i8) -> Self::Output {
|
||||
let mut output = Matrix::default();
|
||||
for (i, v) in self.0.iter().enumerate() {
|
||||
output.0[i] = v / rhs;
|
||||
}
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Matrix {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
|
@ -223,10 +245,47 @@ fn main() {
|
|||
}
|
||||
};
|
||||
dims_mod.extend(decl);
|
||||
let mut exponentiation = quote! {};
|
||||
for (method_name, n) in [
|
||||
("square", 2),
|
||||
("cube", 3),
|
||||
("square_root", -2),
|
||||
("cube_root", -3),
|
||||
] {
|
||||
if n == 0 {
|
||||
continue;
|
||||
}
|
||||
let matrix = if n > 0 {
|
||||
*matrix * n
|
||||
} else {
|
||||
if matrix.0.iter().fold(false, |a, v| a || (v % n.abs() != 0)) {
|
||||
continue;
|
||||
}
|
||||
*matrix / n.abs()
|
||||
};
|
||||
if let Some(derived) = dimensions.get(&matrix) {
|
||||
let ident = format_ident!["{method_name}"];
|
||||
let derived_ident = format_ident!["{}", derived.basename];
|
||||
exponentiation.extend(if n > 0 {
|
||||
quote! {
|
||||
pub fn #ident(self) -> #derived_ident {
|
||||
#derived_ident(self.0.powf(#n.abs() as f64))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
pub fn #ident(self) -> #derived_ident {
|
||||
#derived_ident(self.0.powf(1.0 / (#n.abs() as f64)))
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
let mut conversion = quote! {};
|
||||
conversion.extend(impl_convert(basename));
|
||||
dims_mod.extend(quote! {
|
||||
impl #basename_ident {
|
||||
#exponentiation
|
||||
#conversion
|
||||
}
|
||||
});
|
||||
|
@ -255,7 +314,16 @@ fn main() {
|
|||
dims_mod.extend(impl_div(basename, other_basename, derived.basename));
|
||||
}
|
||||
}
|
||||
units_mod.extend(gen_unit_structs(subunits, basename));
|
||||
let submod_ident = format_ident!["{}", basename.to_lowercase()];
|
||||
let unit_structs = gen_unit_structs(subunits, basename);
|
||||
units_mod.extend(quote! {
|
||||
pub mod #submod_ident {
|
||||
use crate::Scale;
|
||||
use crate::dimensions::#basename_ident;
|
||||
#unit_structs
|
||||
}
|
||||
pub use #submod_ident::*;
|
||||
});
|
||||
}
|
||||
code.extend(quote! {
|
||||
pub mod dimensions {
|
||||
|
@ -264,8 +332,6 @@ fn main() {
|
|||
#dims_mod
|
||||
}
|
||||
pub mod units {
|
||||
use crate::Scale;
|
||||
use crate::dimensions::*;
|
||||
#units_mod
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue