dimensioned::dim_impl_unary! [] [src]

macro_rules! dim_impl_unary {
    ($Trait:ident, $fun:ident, $op:ident, $In:ty => $Out:ty) => { ... };
}

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:

  • Same: Keeps the dimensions the same.
  • Mul: Multiplies Self by Self. The same as Pow<P2>.
  • Div: Divides Self by Self. The same as Pow<Zero>.
  • Recip: Gives the reciprocal of Self.
  • Pow<N>: Raises Self to the exponent N where N is a Peano number.
  • Root<N>: Takes the Nth root of Self where N is a Peano number.
  • Sqrt: Takes the square root of Self. The same as Root<P2>.
  • Cbrt: Takes the cube root of Self. The same as Root<P3>.

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

Example

extern crate peano;
#[macro_use]
extern crate dimensioned;

use peano::Same;
use dimensioned::{Dim, Dimension};
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());
}