fugit/
helpers.rs

1/// Needed due to not being allowed to call const-fn in `PartialEq` fo some reasion
2/// get the error:
3///
4/// ```console
5/// error[E0401]: can't use generic parameters from outer function
6///   --> src/main.rs:25:47
7///    |
8/// 21 | impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32>
9///    |                                                                    ------- const parameter from outer function
10/// ...
11/// 25 |         const TEST: u32 = gcd_binary_u32(L_DENOM, R_DENOM);
12///    |                                                   ^^^^^^^ use of generic parameter from outer function
13///
14/// For more information about this error, try `rustc --explain E0401`
15/// ```
16pub struct Helpers<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32>;
17
18impl<const L_NOM: u32, const L_DENOM: u32, const R_NOM: u32, const R_DENOM: u32>
19    Helpers<L_NOM, L_DENOM, R_NOM, R_DENOM>
20{
21    /// Helper constants generated at compile time
22    pub const DIVISOR: u64 =
23        gcd::binary_u64(L_DENOM as u64 * R_NOM as u64, R_DENOM as u64 * L_NOM as u64);
24
25    /// Helper constants generated at compile time
26    pub const DIVISOR_2: u64 =
27        gcd::binary_u64(L_NOM as u64 * R_NOM as u64, R_DENOM as u64 * L_DENOM as u64);
28
29    /// Helper constants generated at compile time for Durations
30    pub const RD_TIMES_LN: u64 = (R_DENOM as u64 * L_NOM as u64) / Self::DIVISOR;
31
32    /// Helper constants generated at compile time
33    pub const LD_TIMES_RN: u64 = (L_DENOM as u64 * R_NOM as u64) / Self::DIVISOR;
34
35    /// Helper constants generated at compile time for Rates
36    pub const LN_TIMES_RN: u64 = (L_NOM as u64 * R_NOM as u64) / Self::DIVISOR_2;
37
38    /// Helper constants generated at compile time for Rates
39    pub const RD_TIMES_LD: u64 = (R_DENOM as u64 * L_DENOM as u64) / Self::DIVISOR_2;
40
41    /// Helper constants generated at compile time for Rates
42    pub const RATE_TO_DURATION_NUMERATOR: u64 = Self::RD_TIMES_LD / Self::LN_TIMES_RN;
43
44    /// Helper constants generated at compile time
45    pub const SAME_BASE: bool = Self::LD_TIMES_RN == Self::RD_TIMES_LN;
46}
47
48#[allow(dead_code)]
49#[allow(path_statements)]
50pub(crate) const fn greater_than_0<const N: u32>() {
51    Assert::<N, 0>::GREATER;
52}
53
54#[allow(dead_code)]
55/// Const assert hack
56pub struct Assert<const L: u32, const R: u32>;
57
58#[allow(dead_code)]
59impl<const L: u32, const R: u32> Assert<L, R> {
60    /// Const assert hack
61    pub const GREATER_EQ: () = assert!(L >= R);
62
63    /// Const assert hack
64    pub const LESS_EQ: () = assert!(L <= R);
65
66    /// Const assert hack
67    pub const NOT_EQ: () = assert!(L != R);
68
69    /// Const assert hack
70    pub const EQ: () = assert!(L == R);
71
72    /// Const assert hack
73    pub const GREATER: () = assert!(L > R);
74
75    /// Const assert hack
76    pub const LESS: () = assert!(L < R);
77
78    /// Const assert hack
79    pub const POWER_OF_TWO: () = assert!(L.is_power_of_two());
80}