Module angulus::units

source ·
Expand description

This module provides wrappers to “colorize” an angle with a specific unit.

Display

Because angles are unit agnostic they cannot implement the Display trait.

But unit wrappers implement the Display trait. The value is displayed by writting the angle value in the desired unit followed by the unit symbole.

let angle = 90.0_f32.deg();

assert_eq!(format!("{}", Radians(angle)), "1.5707964 rad");
assert_eq!(format!("{}", Degrees(angle)), "90°");
assert_eq!(format!("{}", Turns(angle)), "0.25 tr");
assert_eq!(format!("{}", Gradians(angle)), "100g");

(De)Serialization

This require the serde feature flag.

The angle types (de)serialize into/from radians. But unit wrappers will (de)serialize the value into/from the specified unit.

#[derive(Serialize, Deserialize)]
struct Foo {
    rad: Radians<Angle32>,
    deg: Degrees<Angle32>,
    tr: Turns<Angle32>,
    g: Gradians<Angle32>,
}

let json = serde_json::json!{
    {
        "rad": 1.0,
        "deg": 90.0,
        "tr": 0.5,
        "g": 50,
    }
};

let foo: Foo = serde_json::from_value(json).unwrap();

assert_float_eq!(foo.rad.0.to_radians(), 1.0, abs <= 0.000001);
assert_float_eq!(foo.deg.0.to_degrees(), 90.0, abs <= 0.000001);
assert_float_eq!(foo.tr.0.to_turns(), 0.5, abs <= 0.000001);
assert_float_eq!(foo.g.0.to_gradians(), 50.0, abs <= 0.000001);

Structs

Unit wrapper to “colorize” an angle in degrees.
Unit wrapper to “colorize” an angle in gradians.
Unit wrapper to “colorize” an angle in radians.
Unit wrapper to “colorize” an angle in turns.