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
//! # Proof of Concept: Physical units through const generics
//!
//! Like a tiny [uom](https://crates.io/crates/uom) with better error messages!
//!
//! Correct usage:
//!
//! ```
//! #![feature(const_generics, const_evaluatable_checked)]
//! use const_unit_poc::values::{m, kg, s, N};
//!
//! let distance = 1.0 * m;
//! let mass = 18.0 * kg;
//! let force = distance * mass / (1.8 * s * 2.0 * s);
//! assert_eq!(force, 5.0 * N);
//!
//! let mut mutable_distance = 3.2 * m;
//! mutable_distance -= 0.2 * m;
//! mutable_distance += 2.0 * m;
//!
//! assert_eq!(mutable_distance, 5.0 * m);
//! ```
//!
//! Wrong usage:
//!
//! ```compile_fail
//! #![feature(const_generics, const_evaluatable_checked)]
//! use const_unit_poc::values::{kg, m};
//!
//! let mut distance = 15.0 * m;
//! distance = 2.0 * kg;
//! ```
//!
//! ```plain
//! error[E0308]: mismatched types
//!  --> src/some_module.rs:8:16
//!   |
//! 8 |     distance = 2.0 * kg;
//!   |                ^^^^^^^^ expected `1_i8`, found `0_i8`
//!   |
//!   = note: expected struct `Quantity<SiUnit { m: 1_i8, kg: 0_i8, s: 0_i8, A: 0_i8, K: 0_i8, mol: 0_i8, cd: 0_i8 }>`
//!              found struct `Quantity<SiUnit { m: 0_i8, kg: 1_i8, s: 0_i8, A: 0_i8, K: 0_i8, mol: 0_i8, cd: 0_i8 }>`
//! ```
#![feature(const_generics, const_evaluatable_checked, doc_cfg)]
#![allow(incomplete_features)]
#![cfg_attr(feature = "non_ascii", feature(non_ascii_idents))]

use std::ops;

pub mod units;
pub mod values;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[allow(non_snake_case)]
pub struct SiUnit {
    m: i8,
    kg: i8,
    s: i8,
    A: i8,
    K: i8,
    mol: i8,
    cd: i8,
}

// Can't call trait methods in const context, so these are inherent methods
impl SiUnit {
    const fn neg(self) -> Self {
        Self {
            m: -self.m,
            kg: -self.kg,
            s: -self.s,
            A: -self.A,
            K: -self.K,
            mol: -self.mol,
            cd: -self.cd,
        }
    }

    const fn unit_mul(self, rhs: Self) -> Self {
        Self {
            m: self.m + rhs.m,
            kg: self.kg + rhs.kg,
            s: self.s + rhs.s,
            A: self.A + rhs.A,
            K: self.K + rhs.K,
            mol: self.mol + rhs.mol,
            cd: self.cd + rhs.cd,
        }
    }

    const fn unit_div(self, rhs: Self) -> Self {
        Self {
            m: self.m - rhs.m,
            kg: self.kg - rhs.kg,
            s: self.s - rhs.s,
            A: self.A - rhs.A,
            K: self.K - rhs.K,
            mol: self.mol - rhs.mol,
            cd: self.cd - rhs.cd,
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct Quantity<const U: SiUnit> {
    pub raw_value: f64,
}

impl<const U: SiUnit> ops::Add for Quantity<U> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Self { raw_value: self.raw_value + rhs.raw_value }
    }
}

impl<const U: SiUnit> ops::AddAssign for Quantity<U> {
    fn add_assign(&mut self, rhs: Self) {
        self.raw_value += rhs.raw_value;
    }
}

impl<const U: SiUnit> ops::Sub for Quantity<U> {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self { raw_value: self.raw_value - rhs.raw_value }
    }
}

impl<const U: SiUnit> ops::SubAssign for Quantity<U> {
    fn sub_assign(&mut self, rhs: Self) {
        self.raw_value -= rhs.raw_value;
    }
}

impl<const U: SiUnit> ops::Mul<f64> for Quantity<U> {
    type Output = Quantity<U>;

    fn mul(self, rhs: f64) -> Self::Output {
        Quantity { raw_value: self.raw_value * rhs }
    }
}

impl<const U: SiUnit> ops::Mul<Quantity<U>> for f64 {
    type Output = Quantity<U>;

    fn mul(self, rhs: Quantity<U>) -> Self::Output {
        Quantity { raw_value: self * rhs.raw_value }
    }
}

impl<const UL: SiUnit, const UR: SiUnit> ops::Mul<Quantity<UR>> for Quantity<UL>
where
    Quantity<{ UL.unit_mul(UR) }>: ,
{
    type Output = Quantity<{ UL.unit_mul(UR) }>;

    fn mul(self, rhs: Quantity<UR>) -> Self::Output {
        Quantity { raw_value: self.raw_value * rhs.raw_value }
    }
}

impl<const U: SiUnit> ops::MulAssign<f64> for Quantity<U> {
    fn mul_assign(&mut self, rhs: f64) {
        self.raw_value *= rhs;
    }
}

impl<const U: SiUnit> ops::Div<f64> for Quantity<U> {
    type Output = Quantity<U>;

    fn div(self, rhs: f64) -> Self::Output {
        Quantity { raw_value: self.raw_value / rhs }
    }
}

impl<const U: SiUnit> ops::Div<Quantity<U>> for f64
where
    Quantity<{ U.neg() }>: ,
{
    type Output = Quantity<{ U.neg() }>;

    fn div(self, rhs: Quantity<U>) -> Self::Output {
        Quantity { raw_value: self / rhs.raw_value }
    }
}

impl<const UL: SiUnit, const UR: SiUnit> ops::Div<Quantity<UR>> for Quantity<UL>
where
    Quantity<{ UL.unit_div(UR) }>: ,
{
    type Output = Quantity<{ UL.unit_div(UR) }>;

    fn div(self, rhs: Quantity<UR>) -> Self::Output {
        Quantity { raw_value: self.raw_value / rhs.raw_value }
    }
}

impl<const U: SiUnit> ops::DivAssign<f64> for Quantity<U> {
    fn div_assign(&mut self, rhs: f64) {
        self.raw_value /= rhs;
    }
}