extern crate yaiouom; use yaiouom::*; use yaiouom::si::*; // The following should build unsafely with Rust, then yaiouom-driver will ensure the safety of `unify`. fn get_speed(distance: Measure, duration: Measure) -> Measure>> { return (distance / duration).unify(); } // The following should build unsafely with Rust, then yaiouom-driver will ensure the safety of `unify`. fn get_speed_2(distance: Measure, duration: Measure) -> Measure>> { return ((Dimensionless::new(1.) / duration) * distance ).unify(); } // The following should build safely with vanilla Rust. fn get_average(number: usize) -> Measure { let total = (1..number) .map(|x| x as f64) .map(U::new) .fold(U::new(0.), std::ops::Add::add); total / (number as f64) } // The following should build safely with vanilla Rust. fn get_average_2>(number: usize) -> Measure { let total : Measure<_, _> = (1..number) .map(|x| x as f64) .map(U::new) .sum(); total / (number as f64) } trait Distance: Unit {} trait Duration: Unit {} // The following should build unsafely with Rust, then yaiouom-driver will ensure the safety of `unify`. fn get_speed_generic(distance: Measure, duration: Measure) -> Measure>> { return (distance / duration).unify(); } // The same one, with annotations. Should build with Rust and yaiouom-driver. fn get_speed_generic_2_annotated(distance: Measure, duration: Measure) -> Measure>> { let a : Measure<_, Mul>> = (distance / duration) .unify(); return a .unify(); } // The same one, with annotations. Should build with Rust and yaiouom-driver. fn get_speed_generic_2_annotated_2(distance: Measure, duration: Measure) -> Measure>> { (distance / duration) .unify::>>() .unify() } // Using type aliases. type MeterAlias = Meter; fn get_speed_alias(distance: Measure, duration: Measure) -> Measure>> { return ((Dimensionless::new(1.) / duration) * distance ).unify(); } struct Foo where T: std::ops::Div + Copy { distance: Measure, duration: Measure } impl Foo where T: std::ops::Div + Copy { fn get_speed(&self) -> Measure>> { self.distance / self.duration } fn get_speed_2(&self) -> Measure, Meter>> { (self.distance / self.duration).unify() } } fn main() { // We just want to check that everything compiles. }