extern crate yaiouom; extern crate num_traits; use yaiouom::*; use yaiouom::si::*; use std::marker::PhantomData; fn convert_2d(convert_1d: F, input: Measure>) -> Measure> where F: Fn(Measure) -> Measure { let b = convert_1d(input.sqrt()); b * b } fn convert_inv(convert_1d: F, input: Measure>) -> Measure> where F: Fn(Measure) -> Measure { use num_traits::ops::inv::Inv; convert_1d(input.inv().unify()) .inv() .unify() } struct Converter) -> Measure> { left: PhantomData, right: PhantomData, convert_1d: F, } impl Converter where A: Unit, B: Unit, F: Fn(Measure) -> Measure { fn convert_2d(&self, input: Measure>) -> Measure> { let b = (self.convert_1d)(input.sqrt()); (b * b) } fn convert_inv(&self, input: Measure>) -> Measure> { use num_traits::ops::inv::Inv; (self.convert_1d)(input.inv().unify()) .inv() .unify() } } fn main() { // Let's make sure that it doesn't cause a panic. let result_1 = convert_2d(|x| x, Mul::::new(1.0)); let converter = Converter { left: PhantomData, right: PhantomData, convert_1d: |x| x, }; let result_2 = converter.convert_2d(Mul::::new(1.0)); assert_eq!(result_1, result_2); let converter = Converter { left: PhantomData, right: PhantomData, convert_1d: |x: Measure| x, }; let result_3 = convert_inv(|x| x, Inv::::new(2.).unify()); let result_4 = converter.convert_inv(Inv::::new(2.).unify()); assert_eq!(result_3, result_4); }