dimensioned::dim_impl_binary! [] [src]

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

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:

  • Same<RHS>: Ensures that Self has the same dimensions as RHS but doesn't change them.
  • Mul<RHS>: Multiplies Self by RHS.
  • Div<RHS>: Divides Self by RHS.

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));
}