Trait dimensioned::traits::MapUnsafe[][src]

pub trait MapUnsafe<ValueOut, UnitsOut>: Dimensioned {
    type Output;
    fn map_unsafe<F: FnOnce(Self::Value) -> ValueOut>(
        self,
        f: F
    ) -> Self::Output; }

Perform an operation on a quantity.

Use of this function is discouraged except when necessary, as the operation may be one that does not perserve units, and this function has no way to protect against that. If you do use it, consider placing it in a trait or function that you can verify is dimensionally safe.

If associated type constructors or higher kinded types are implemented, then this trait should no longer be necessary and may become deprecated.

Example

Let's say we have a function that, when given a quantity with value type Value and unit type Units, has output with value type (Value, Value) and squares the units. Then, we could generically implement it for Dimensioned as follows:

extern crate dimensioned as dim;

use dim::{Dimensioned, MapUnsafe};
use dim::typenum::{Prod, P2};
use std::ops::Mul;

pub trait Weird {
    type Output;
    fn weird(self) -> Self::Output;
}

impl<D, Value, Units> Weird for D where
    Value: Clone,
    Units: Mul<P2>,
    D: Dimensioned<Value=Value, Units=Units> +
       MapUnsafe<(Value, Value), Prod<Units, P2>>,
{
    type Output = <D as MapUnsafe<(Value, Value), Prod<Units, P2>>>::Output;
    fn weird(self) -> Self::Output {
        self.map_unsafe(|v| (v.clone(), v))
    }
}

fn main() {
    use dim::si;
    let x = 3.0 * si::M;
    let w = x.weird();

    assert_eq!(w, si::Meter2::new((3.0, 3.0)));

    println!("w: {:?}", w);
    // prints: w: (3, 3) m^2
}

Associated Types

The type to which the input is mapped

Required Methods

Perform the map

Implementors