dimensioned::dim_impl_binary! [] [src]

macro_rules! dim_impl_binary { ($Trait:ident, $fun:ident, $op:ident, $In:ty => $Out:ty) => (
    pub trait $Trait<RHS> {
        type Output;
        fn $fun(self, rhs: RHS) -> Self::Output;
    }
    impl<Dl, Dr> $Trait<Dim<Dr, $In>> for Dim<Dl, $In> where Dl: Dimension + $op<Dr>, Dr: Dimension, <Dl as $op<Dr>>::Output: Dimension {
        type Output = Dim<<Dl as $op<Dr>>::Output, $Out>;
        fn $fun(self, rhs: Dim<Dr, $In>) -> Self::Output { Dim::new( (self.0).$fun(rhs.0) ) }
    }
    );
}

Used for implementing binary members of V for Dim<D, V>.

Assume you have some type V with a member function fun that takes one argument also of type V and has output type Out.

Then, you can implement fun as a member for Dim<D, V> with the macro invocation:

dim_impl_binary!(Trait, fun, Op, V => Out);

where Trait is the name of the trait that you want to put this member in; it can be any available name.

Finally, Op determines how the dimensions should change when calling fun() and is one of:

Note: This macro requires that Dim and Dimension be imported.

Example

#[macro_use]
extern crate dimensioned;
use dimensioned::{Dim, Dimension};
use dimensioned::si::m;
use std::ops::Mul;

pub struct Vector2 {
    x: f64,
    y: f64
}
impl Vector2 {
    fn dot(self, rhs: Vector2) -> f64 { self.x*rhs.x + self.y*rhs.y }
}
impl Mul<Vector2> for f64 {
    type Output = Vector2;
    fn mul(self, rhs: Vector2) -> Vector2 { Vector2{ x: self*rhs.x, y: self*rhs.y } }
}

dim_impl_binary!(Dot, dot, Mul, Vector2 => f64);

fn main() {
    let v1 = m * Vector2{ x: 1.0, y: 2.0 };
    let v2 = m * Vector2{ x: 3.0, y: 5.0 };
    assert_eq!(13.0*m*m, v1.dot(v2));
}