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
/*!
Declares foundational `QuantityT<T,U>` struct upon which fts_units is built.

`QuantityT<T,U>` defines a `Quantity` which stores an `amount` of type `T` with units type `U`. Mathematical operations with `QuantityT<T,U>` values produces new types of `QuantityT` with a different `U` type.

If `U` is zero sized then all the type checking done and compile time and boils down to nothing. No run-time overhead for either CPU or memory.

Mathematical operations for `QuantityT<T,U>` are defined so long as they are independently defined for both `T` and `U`.
*/

use std::fmt;
use std::marker::PhantomData;

use crate::ops::*;

// --------------------------------
// Trait declarations
// --------------------------------

/// Must be implemented for `T` in `QuantityT<T,U>`.
pub trait Amount: Copy + Clone {}

/// Helper for performing generic computations with `QuantityT<T,U>` types.
pub trait Quantity {
    type AmountType: Amount;
    type UnitsType;

    fn new(amount: Self::AmountType) -> Self;
    fn amount(&self) -> Self::AmountType;
}

/// Helper for converting units. For example from Meters to Kilometers.
///
/// ```rust
/// # use fts_units::si_system::quantities::f32::*;
/// # use fts_units::quantity::ConvertUnits;
/// let d = Kilometers::new(15.3);
/// let t = Hours::new(2.7);
/// let kph : KilometersPerHour = d / t;
///
/// let mps : MetersPerSecond = kph.convert_into();
/// let mps = MetersPerSecond::convert_from(kph);
/// ```
pub trait ConvertUnits<T> {
    fn convert_from(v: T) -> Self;
    fn convert_into(self) -> T;
}

/// Helpers for casting `T` in `QuantityT<T,U>`.
///
/// ```rust
/// # use fts_units::si_system::quantities::*;
/// # use fts_units::quantity::{CastAmount, Quantity};
/// let m = Meters::<f32>::new(7.73);
/// let i : Meters<i32> = m.cast_into();
/// assert_eq!(i.amount(), 7);
/// ```
pub trait CastAmount<T> {
    fn cast_from(v: T) -> Self;
    fn cast_into(self) -> T;
}

// --------------------------------
// Struct declarations
// --------------------------------

/// Foundational struct used by all run-time quantities.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct QuantityT<T, U>
where
    T: Amount,
{
    amount: T,
    _u: PhantomData<U>,
}

// --------------------------------
// QuantityT impls
// --------------------------------
impl<T, U> QuantityT<T, U>
where
    T: Amount,
{
    pub fn new(amount: T) -> QuantityT<T, U> {
        QuantityT {
            amount: amount,
            _u: PhantomData,
        }
    }
}

impl<T, U> Quantity for QuantityT<T, U>
where
    T: Amount,
{
    type AmountType = T;
    type UnitsType = U;

    fn new(amount: T) -> Self {
        QuantityT::<T, U>::new(amount)
    }

    fn amount(&self) -> Self::AmountType {
        self.amount
    }
}

impl<T, Q> std::ops::Add<Self> for QuantityT<T, Q>
where
    T: Amount + std::ops::Add<T>,
    Q: std::ops::Add<Q>,
    AddOutput<T, T>: Amount,
{
    type Output = QuantityT<AddOutput<T, T>, AddOutput<Q, Q>>;

    fn add(self, other: Self) -> Self::Output {
        Self::Output::new(self.amount + other.amount)
    }
}

impl<T, Q> std::ops::Sub<Self> for QuantityT<T, Q>
where
    T: Amount + std::ops::Sub<T>,
    Q: std::ops::Sub<Q>,
    SubOutput<T, T>: Amount,
{
    type Output = QuantityT<SubOutput<T, T>, SubOutput<Q, Q>>;

    fn sub(self, other: Self) -> Self::Output {
        Self::Output::new(self.amount - other.amount)
    }
}

impl<T, Q0, Q1> std::ops::Mul<QuantityT<T, Q1>> for QuantityT<T, Q0>
where
    T: Amount + std::ops::Mul<T>,
    Q0: std::ops::Mul<Q1>,
    MulOutput<T, T>: Amount,
{
    type Output = QuantityT<MulOutput<T, T>, MulOutput<Q0, Q1>>;

    fn mul(self, other: QuantityT<T, Q1>) -> Self::Output {
        Self::Output::new(self.amount * other.amount)
    }
}

impl<T, Q0, Q1> std::ops::Div<QuantityT<T, Q1>> for QuantityT<T, Q0>
where
    T: Amount + std::ops::Div<T>,
    Q0: std::ops::Div<Q1>,
    DivOutput<T, T>: Amount,
{
    type Output = QuantityT<DivOutput<T, T>, DivOutput<Q0, Q1>>;

    fn div(self, other: QuantityT<T, Q1>) -> Self::Output {
        Self::Output::new(self.amount / other.amount)
    }
}

impl<T, U> std::fmt::Display for QuantityT<T, U>
where
    T: std::fmt::Display + Amount,
    U: std::fmt::Display + Default,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {}", self.amount, U::default())
    }
}

impl<T, U> Invert for QuantityT<T, U>
where
    T: Invert + Amount,
    U: Invert,
    InvertOutput<T>: Amount,
{
    type Output = QuantityT<InvertOutput<T>, InvertOutput<U>>;

    fn invert(self) -> Self::Output {
        Self::Output::new(self.amount.invert())
    }
}

impl<T, U> Sqrt for QuantityT<T, U>
where
    T: num_traits::float::Float + Amount,
    U: Sqrt,
{
    type Output = QuantityT<T, SqrtOutput<U>>;

    fn sqrt(self) -> Self::Output {
        Self::Output::new(self.amount.sqrt())
    }
}

impl<T, U> std::convert::From<T> for QuantityT<T, U>
where
    T: Amount,
{
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

impl Amount for i8 {}
impl Amount for i16 {}
impl Amount for i32 {}
impl Amount for i64 {}
impl Amount for i128 {}
impl Amount for u8 {}
impl Amount for u16 {}
impl Amount for u32 {}
impl Amount for u64 {}
impl Amount for u128 {}
impl Amount for f32 {}
impl Amount for f64 {}