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
/// A type representing a rational number whose value is known at compile time.
///
/// `Self::Denom / Self::DENOM` must be [reduced].
///
/// [reduced]: http://mathworld.wolfram.com/ReducedFraction.html
pub trait Rational {
    const NUM: i64;
    const DENOM: i64 = 1;
}

macro_rules! ratio {
    (
        $(
            $( #[$meta:meta] )*
            $T:ident => $n:tt / $d:tt
         ),* $(,)*
    ) => {
        $(
            $( #[$meta] )*
            pub enum $T {}

            impl Rational for $T {
                const NUM: i64 = $n;
                const DENOM: i64 = $d;
            }
        )*
    }
}

/// SI-prefixes
ratio! {
    Atto  => 1 / 1_000_000_000_000_000_000,
    Femto => 1 / 1_000_000_000_000_000,
    Pico  => 1 / 1_000_000_000_000,
    Nano  => 1 / 1_000_000_000,
    Micro => 1 / 1_000_000,
    Milli => 1 / 1_000,
    Centi => 1 / 100,
    Deci  => 1 / 10,
    Unity => 1 / 1,
    Deca  => 10 / 1,
    Hecto => 100 / 1,
    Kilo  => 1_000 / 1,
    Mega  => 1_000_000 / 1,
    Giga  => 1_000_000_000 / 1,
    Tera  => 1_000_000_000_000 / 1,
    Peta  => 1_000_000_000_000_000 / 1,
    Exa   => 1_000_000_000_000_000_000 / 1,
}