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
//! Traits and helpers to faciliate working with generic Quantities.

// --------------------------------
// Traits
// --------------------------------

/// Helper to reduce Ratios to RatioZero when Exponent becomes zero
pub trait Reduce {
    type Output;
    fn reduce(self) -> Self::Output;
}

/// Helper to reduce Ratios to RatioZero when Exponent becomes zero
pub trait ReduceWith<T> {
    type Output;
    fn reduce_with(self, other: T) -> Self::Output;
}

/// Provide generic Invert interface for invertable types (such as floats)
pub trait Invert {
    type Output;

    fn invert(self) -> Self::Output;
}

/// Provide generic Sqrt interface for supported types (such as floats)
pub trait Sqrt {
    type Output;

    fn sqrt(self) -> Self::Output;
}

/// Used only at the type level for Xor'ing Zero and Non-Zero Ratios
pub trait Xor<T> {
    type Output;
    fn xor(self, other: T) -> Self::Output;
}

// --------------------------------
// Utilities
// --------------------------------
pub type AddOutput<T, U> = <T as std::ops::Add<U>>::Output;
pub type SubOutput<T, U> = <T as std::ops::Sub<U>>::Output;
pub type MulOutput<T, U> = <T as std::ops::Mul<U>>::Output;
pub type DivOutput<T, U> = <T as std::ops::Div<U>>::Output;
pub type XorOutput<T, U> = <T as Xor<U>>::Output;

pub type MulOutput3<T, U, V> = MulOutput<MulOutput<T, U>, V>;
pub type MulOutput4<T, U, V, W> = MulOutput<MulOutput<MulOutput<T, U>, V>, W>;

pub type InvertOutput<T> = <T as Invert>::Output;
pub type SqrtOutput<T> = <T as Sqrt>::Output;
pub type ReduceOutput<T> = <T as Reduce>::Output;
pub type ReduceWithOutput<T, U> = <T as ReduceWith<U>>::Output;

// --------------------------------
// Impls for built-in types
// --------------------------------
impl Invert for f32 {
    type Output = f32;
    fn invert(self) -> Self::Output {
        1.0 / self
    }
}

impl Invert for f64 {
    type Output = f64;
    fn invert(self) -> Self::Output {
        1.0 / self
    }
}