Trait measurements::Measurement [] [src]

pub trait Measurement {
    fn get_base_units_name(&self) -> &'static str;
fn as_base_units(&self) -> f64;
fn from_base_units(units: f64) -> Self; fn get_appropriate_units(&self) -> (&'static str, f64) { ... }
fn pick_appropriate_units(
        &self,
        list: &[(&'static str, f64)]
    ) -> (&'static str, f64) { ... } }

The Measurement trait and the implement_measurement! macro provides a common way for various measurements to be implemented.

Example

#![no_std]
// Importing the `implement_measurement` macro from the external crate is important
#[macro_use]
extern crate measurements;

use measurements::Measurement;

struct Cubits {
    forearms: f64
}

impl Measurement for Cubits {
    fn as_base_units(&self) -> f64 {
        self.forearms
    }

    fn from_base_units(units: f64) -> Self {
        Cubits { forearms: units }
    }

   fn get_base_units_name(&self) -> &'static str {
       "cu"
   }
}

// Invoke the macro to automatically implement Add, Sub, etc...
implement_measurement! { Cubits }

// The main function here is only included to make doc test_utils compile.
// You should't need it in your own code.
fn main() { }

Required Methods

Return the base unit for this type, as a string. For example "kilograms"

Get this quantity in the base units

Create a new quantity from the base units

Provided Methods

Returns a string containing the most appropriate units for this quantity, and a floating point value representing this quantity in those units. Useful when, for example, a length might be in millimeters if it is very small, or kilometers when it is very large.

The default implementation always selects the base unit. Override in your Measurement impl to select better units if required.

Given a list of units and their scale relative to the base unit, select the most appropriate one.

The list must be smallest to largest, e.g. ("nanometre", 10-9) to ("kilometre", 10e3)

Implementations on Foreign Types

impl Measurement for Duration
[src]

[src]

[src]

[src]

[src]

[src]

Implementors