Struct yaiouom::Measure [] [src]

pub struct Measure<T, U: Unit> { /* fields omitted */ }

A value with a unit.

Methods

impl<T, U: Unit> Measure<T, U>
[src]

[src]

Convert from a dimensionless unit.

use yaiouom::*;
use yaiouom::si::*;

let one_meter : Measure<_, Meter> = Measure::new(1);

Warning

This function is somewhat unsafe, as you can use it to build values that are fully polymorphic in unit.

Future versions will make this constructor private and hide it behind syntactic sugar.

[src]

Compare two units of measure (not their values).

Out of the box, the Rust type system is not powerful enough to resolve general equality between two units of measure. So, for instance, it will not realize that m * s and s * m are the same unit, or that m / m is the same as the dimensionless unit.

For instance, the following will fail to type:

This example deliberately fails to compile
use yaiouom::*;
use yaiouom::si::*;

let one_meter = Meter::new(1);
let one_second = Second::new(1);

let a = one_meter * one_second;
let b = one_second * one_meter;
assert_eq!(a, b);
// ^^^^^^^^^^^^^^^^^ expected struct `Meter`, found struct `Second`

To work around this, use unify(), as follows:

use yaiouom::*;
use yaiouom::si::*;

let one_meter = Meter::new(1);
let one_second = Second::new(1);

let a = one_meter * one_second;
let b = (one_second * one_meter).unify(); // Delays unit check.
assert_eq!(a, b);

Soundness of unify

If you look at the signature of unify, you can notice that it returns a Measure<T, V> for all V. Despite appearances, this is sound, for two reasons:

A refinement type for units of measure

The companion linter for this crate implements a refinement of the Rust type system dedicated to units of measure. What this means, in practice, is that whenever you call foo.unify(), it will check that the returned type is correct. By opposition to the general Rust type system, it will correctly realize that m * s and s * m are the same, or even that W * m / W is m, even if is a type variable.

Please don't use unify() without the linter :)

Dynamic checks

As a fallback, in debug builds, each call to unify will panic if type V is not equivalent ot type U.

[src]

Convert between two value representations (e.g. u32 vs u64) in the same unit.

Ideally, this should be an implementation of From, but it conflicts with the reflexive implementation.

use yaiouom::*;
use yaiouom::si::*;

let one_meter_usize = Meter::new(1 as i32);
let one_meter_isize : Measure<i64, Meter> = Measure::from(one_meter_usize);

[src]

Convert between two value representations (e.g. u32 vs u64) In the same unit.

Ideally, this should be an implementation of From, but it conflicts with the reflexive implementation.

use yaiouom::*;
use yaiouom::si::*;

let one_meter_usize : Measure<i32, Meter> = Measure::new(1);
let one_meter_isize : Measure<i64, Meter> = one_meter_usize.into();

[src]

Extract a runtime representation of a unit.

This runtime representation is designed mainly for debugging purposes.

use yaiouom::*;
use yaiouom::si::*;

let one_meter_usize : Measure<i32, Meter> = Measure::new(1);
assert_eq!(one_meter_usize.as_runtime().to_string(), "m");

Performance note

This method is fine for debugging, but should not be used in a tight loop.

impl<T> Measure<T, Dimensionless>
[src]

[src]

Trait Implementations

impl<T, U: Unit> AsRef<T> for Measure<T, U>
[src]

[src]

Performs the conversion.

impl<T, U: Unit> Clone for Measure<T, U> where
    T: Clone
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<T, U: Unit> Copy for Measure<T, U> where
    T: Copy
[src]

impl<T, U: Unit> PartialEq for Measure<T, U> where
    T: PartialEq
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

1.0.0
[src]

This method tests for !=.

impl<T, U: Unit> Eq for Measure<T, U> where
    T: PartialEq
[src]

impl<T, U: Unit> PartialOrd for Measure<T, U> where
    T: PartialOrd
[src]

[src]

This method returns an ordering between self and other values if one exists. Read more

1.0.0
[src]

This method tests less than (for self and other) and is used by the < operator. Read more

1.0.0
[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

1.0.0
[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

1.0.0
[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<T, U: Unit> Ord for Measure<T, U> where
    T: Ord
[src]

[src]

This method returns an Ordering between self and other. Read more

1.21.0
[src]

Compares and returns the maximum of two values. Read more

1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl<T, U: Unit> Add<Self> for Measure<T, U> where
    T: Add<Output = T>, 
[src]

Out of the box, one may only add two values with the same unit.

It remains, however, possible to manually implement Add e.g. for a time and a duration or a point and a vector.

use yaiouom::*;
use yaiouom::si::*;

let one_meter : Measure<i32, Meter> = Measure::new(1);
let two_meters = one_meter + one_meter;
assert_eq!(*two_meters.as_ref(), 2);

The resulting type after applying the + operator.

[src]

Performs the + operation.

impl<T, U: Unit> Mul<T> for Measure<T, U> where
    T: Mul<T>, 
[src]

The resulting type after applying the * operator.

[src]

Multiply a dimensionless value with a measure.

use yaiouom::*;
use yaiouom::si::*;

let one_meter : Measure<i32, Meter> = Measure::new(1);
let ten_meters : Measure<i32, Meter> = one_meter * 10;
assert_eq!(ten_meters.as_ref(), &10);

impl<T, U: Unit, V: Unit> Mul<Measure<T, V>> for Measure<T, U> where
    T: Mul<T>, 
[src]

The resulting type after applying the * operator.

[src]

Multiply two measures

use yaiouom::*;
use yaiouom::si::*;

let two_meters : Measure<i32, Meter> = Measure::new(2);
let four_sq_meters : Measure<i32, Mul<Meter, Meter>> = two_meters * two_meters;
assert_eq!(four_sq_meters.as_ref(), &4);

impl<T, U: Unit, V: Unit> Div<Measure<T, V>> for Measure<T, U> where
    T: Div<T>, 
[src]

The resulting type after applying the / operator.

[src]

Divide two measures

use yaiouom::*;
use yaiouom::si::*;

let four_sq_meters : Measure<i32, Mul<Meter, Meter>> = Measure::new(4);
let two_meters : Measure<i32, Meter> = Measure::new(2);
let other_two_meters : Measure<i32, _> = four_sq_meters / two_meters;

assert_eq!(two_meters.as_ref(), other_two_meters.as_ref());

impl<T, U: Unit> Div<T> for Measure<T, U> where
    T: Div<T>, 
[src]

The resulting type after applying the / operator.

[src]

Divide a dimensionless value by a measure.

use yaiouom::*;
use yaiouom::si::*;

let ten_meters : Measure<i32, Meter> = Measure::new(10);
let one_meter : Measure<i32, Meter> = ten_meters / 10;
assert_eq!(one_meter.as_ref(), &1);

impl<T, U: Unit> Sum for Measure<T, U> where
    T: Sum
[src]

[src]

Method which takes an iterator and generates Self from the elements by "summing up" the items. Read more

impl<T, U: Unit> Product for Measure<T, U> where
    T: Product
[src]

[src]

Method which takes an iterator and generates Self from the elements by multiplying the items. Read more

impl<T, U: Unit> Debug for Measure<T, U> where
    T: Debug
[src]

[src]

Formats the value using the given formatter. Read more

impl<T> From<T> for Measure<T, Dimensionless>
[src]

[src]

Performs the conversion.

Auto Trait Implementations

impl<T, U> Send for Measure<T, U> where
    T: Send,
    U: Send

impl<T, U> Sync for Measure<T, U> where
    T: Sync,
    U: Sync