use gen_ops::*; use std::ops::*; mod method_call { use super::*; #[derive(Copy, Clone, PartialEq, Debug)] pub struct Complex(pub T, pub T); impl Complex { fn negative(&self) -> Self where T: Neg + Copy { Complex(-self.0, -self.1) } } gen_ops!( ; types Complex => Complex; for - call Complex::::negative; where T: Neg + Copy ); #[test] fn method_call_neg_test() { let c = Complex(1, -5); assert_eq!(-c, Complex(-1, 5)); } } mod func_call { use super::*; #[derive(Copy, Clone, PartialEq, Debug)] pub struct Complex(pub T, pub T); fn neg_complex(c: &Complex) -> Complex where T: Neg + Copy { Complex(-c.0, -c.1) } gen_ops!( ; types Complex => Complex; for - call neg_complex; where T: Neg + Copy ); #[test] fn func_call_neg_test() { let c = Complex(1, -5); assert_eq!(-c, Complex(-1, 5)); } } mod lifetime_params { use super::*; #[derive(Copy, Clone, PartialEq, Debug)] struct Button { id: u32, } #[derive(Copy, Clone, PartialEq, Debug)] struct ButtonPair<'a, 'b> { pub btn1: &'a Button, pub btn2: &'b Button } gen_ops!( <'a, 'b>; types &'a Button , &'b Button => ButtonPair<'a, 'b>; for + call |a: &'a Button, b: &'b Button| ButtonPair{btn1:a, btn2:b}; ); #[test] fn lifetime_params_test() { let b1 = Button{id:1}; { let b2 = Button{id:2}; let p = &b1 + &b2; assert_eq!(p.btn1.id, 1); assert_eq!(p.btn2.id, 2); } } }