dimensioned::dim_impl_unary! [] [src]

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

Used for implementing unary members of V for Dim<D, V>

Assume you have some type V with a member function fun that takes no arguments and has output of type Out.

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

dim_impl_unary!(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, Same};
use dimensioned::si::m;
use std::ops::Mul;

pub struct Vector2 {
    x: f64,
    y: f64
}
impl Vector2 {
    fn norm(self) -> f64 {
        (self.x*self.x + self.y*self.y).sqrt()
    }
}
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_unary!(Norm, norm, Same, Vector2 => f64);

fn main() {
    let v = m * Vector2{ x: 3.0, y: 4.0 };
    assert_eq!(5.0*m, v.norm());
}