1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// lenpriv.rs
//
// Copyright (C) 2019  Minnesota Department of Transportation
//
//! Private module for length structs
//!
use crate::length::Unit;
use std::fmt;
use std::marker::PhantomData;
use std::ops::{Add, Div, Mul, Sub};

/// A measurement of physical _length_, _distance_ or _range_.
///
/// Length is a base quantity with a specific [unit].
///
/// ## Operations
///
/// * Length `+` Length `=>` Length
/// * Length `-` Length `=>` Length
/// * Length `*` f64 `=>` Length
/// * f64 `*` Length `=>` Length
/// * f64 `*` [unit] `=>` Length
/// * Length `*` Length `=>` [Area]
/// * Length `*` [unit] `=>` [Area]
/// * Length `/` f64 `=>` Length
///
/// Units must be the same for operations with two Length operands.  The [to]
/// method can be used for conversion.
///
/// ## Example
///
/// ```rust
/// use mag::{Length, length::In};
///
/// let a = 5.5 * In;
/// let b = 4.5 * In;
///
/// assert_eq!(a.to_string(), "5.5 in");
/// assert_eq!(a + b, 10.0 * In);
/// ```
/// [Area]: struct.Area.html
/// [unit]: length/index.html
/// [to]: struct.Length.html#method.to
///
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Length<U> where U: Unit {
    /// Length quantity
    pub quantity: f64,
    /// Measurement unit
    unit: PhantomData<U>,
}

/// A measurement of physical _area_.
///
/// Area is a derived quantity with a specific [unit] squared.
///
/// ## Operations
///
/// * Area `+` Area `=>` Area
/// * Area `-` Area `=>` Area
/// * Area `*` f64 `=>` Area
/// * Area `*` [Length] `=>` [Volume]
/// * Area `/` f64 `=>` Area
/// * Area `/` [Length] `=>` [Length]
///
/// ## Example
///
/// ```rust
/// use mag::length::m;
///
/// let a = 150.0 * m * m; // Area<m>
/// let b = (10.0 * m) * (15.0 * m); // Area<m>
///
/// assert_eq!(a, b);
/// assert_eq!(a.to_string(), "150 m²");
/// assert_eq!(a / (5.0 * m), 30.0 * m);
/// ```
/// [unit]: length/index.html
/// [Length]: struct.Length.html
/// [Volume]: struct.Volume.html
///
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Area<U> where U: Unit {
    /// Area quantity
    pub quantity: f64,
    /// Measurement unit
    unit: PhantomData<U>,
}

/// A measurement of physical _volume_.
///
/// Volume is a derived quantity with a specific [unit] cubed.
///
/// ## Operations
///
/// * Volume `+` Volum `=>` Volume
/// * Volume `-` Volume `=>` Volume
/// * Volume `*` f64 `=>` Volume
/// * Volume `/` f64 `=>` Volume
/// * Volume `/` [Length] `=>` [Area]
/// * Volume `/` [Area] `=>` [Length]
///
/// ## Example
///
/// ```rust
/// use mag::length::yd;
///
/// let a = 2.5 * yd * yd * yd; // Volume<yd>
/// let b = a / (2.0 * yd); // Area<yd>
///
/// assert_eq!(a.to_string(), "2.5 yd³");
/// assert_eq!(b.to_string(), "1.25 yd²");
/// ```
/// [Area]: struct.Area.html
/// [unit]: length/index.html
/// [Length]: struct.Length.html
///
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Volume<U> where U: Unit {
    /// Volume quantity
    pub quantity: f64,
    /// Measurement unit
    unit: PhantomData<U>,
}

impl_base_ops!(Length, Unit);
impl_base_ops!(Area, Unit);
impl_base_ops!(Volume, Unit);

impl<U> Length<U> where U: Unit {
    /// Create a new length measurement
    pub fn new(quantity: f64) -> Self {
        Length::<U> { quantity, unit: PhantomData }
    }

    /// Convert to specified units
    pub fn to<T: Unit>(self) -> Length<T> {
        let quantity = self.quantity * U::factor::<T>();
        Length::new(quantity)
    }
}

impl<U> Area<U> where U: Unit {
    /// Create a new area measurement
    pub fn new(quantity: f64) -> Self {
        Area::<U> { quantity, unit: PhantomData }
    }

    /// Convert to specified units
    pub fn to<T: Unit>(self) -> Area<T> {
        let factor = U::factor::<T>() * U::factor::<T>();
        let quantity = self.quantity * factor;
        Area::new(quantity)
    }
}

impl<U> Volume<U> where U: Unit {
    /// Create a new volume measurement
    pub fn new(quantity: f64) -> Self {
        Volume::<U> { quantity, unit: PhantomData }
    }

    /// Convert to specified units
    pub fn to<T: Unit>(self) -> Volume<T> {
        let factor = U::factor::<T>() * U::factor::<T>() * U::factor::<T>();
        let quantity = self.quantity * factor;
        Volume::new(quantity)
    }
}

impl<U> fmt::Display for Length<U> where U: Unit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.quantity.fmt(f)?;
        write!(f, " {}", U::ABBREVIATION)
    }
}

impl<U> fmt::Display for Area<U> where U: Unit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.quantity.fmt(f)?;
        write!(f, " {}²", U::ABBREVIATION)
    }
}

impl<U> fmt::Display for Volume<U> where U: Unit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.quantity.fmt(f)?;
        write!(f, " {}³", U::ABBREVIATION)
    }
}

// Length * Length => Area
impl<U> Mul for Length<U> where U: Unit {
    type Output = Area<U>;
    fn mul(self, other: Self) -> Self::Output {
        Area::new(self.quantity * other.quantity)
    }
}

// Area * Length => Volume
impl<U> Mul<Length<U>> for Area<U> where U: Unit {
    type Output = Volume<U>;
    fn mul(self, other: Length<U>) -> Self::Output {
        Volume::new(self.quantity * other.quantity)
    }
}

// Area / Length => Length
impl<U> Div<Length<U>> for Area<U> where U: Unit {
    type Output = Length<U>;
    fn div(self, other: Length<U>) -> Self::Output {
        Length::new(self.quantity / other.quantity)
    }
}

// Volume / Length => Area
impl<U> Div<Length<U>> for Volume<U> where U: Unit {
    type Output = Area<U>;
    fn div(self, other: Length<U>) -> Self::Output {
        Area::new(self.quantity / other.quantity)
    }
}

// Volume / Area => Length
impl<U> Div<Area<U>> for Volume<U> where U: Unit {
    type Output = Length<U>;
    fn div(self, other: Area<U>) -> Self::Output {
        Length::new(self.quantity / other.quantity)
    }
}