Make the interface more Newtype like

This commit is contained in:
Max Känner 2023-04-23 18:39:49 +02:00
parent 4c73f860d3
commit 745dcac6bf
2 changed files with 26 additions and 17 deletions

View File

@ -31,15 +31,13 @@ where
Candela: Integer, Candela: Integer,
{ {
value: T, value: T,
_s: PhantomData<Second>, _marker: PhantomData<(Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela)>,
_m: PhantomData<Metre>,
_kg: PhantomData<Kilogram>,
_a: PhantomData<Ampere>,
_k: PhantomData<Kelvin>,
_mol: PhantomData<Mole>,
_cd: PhantomData<Candela>,
} }
#[allow(non_snake_case)]
pub fn SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>(
value: T,
) -> SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>
where where
Second: Integer, Second: Integer,
Metre: Integer, Metre: Integer,
@ -49,6 +47,7 @@ where
Mole: Integer, Mole: Integer,
Candela: Integer, Candela: Integer,
{ {
SiUnit::new(value)
} }
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> Deref impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> Deref
@ -609,13 +608,7 @@ where
pub const fn new(value: T) -> Self { pub const fn new(value: T) -> Self {
Self { Self {
value, value,
_s: PhantomData, _marker: PhantomData,
_m: PhantomData,
_kg: PhantomData,
_a: PhantomData,
_k: PhantomData,
_mol: PhantomData,
_cd: PhantomData,
} }
} }
} }

View File

@ -1,10 +1,26 @@
use crate::SiUnit; use crate::SiUnit;
use typenum::consts::{N1, N2, N3, P1, P2, P3, P4, Z0}; use typenum::consts::{N1, N2, N3, P1, P2, P3, P4, Z0};
macro_rules! unit {
($acronym: ident -> $second: ty, $metre: ty, $kilogram: ty, $ampere: ty, $kelvin: ty, $mole: ty, $candela: ty $(; $doc: literal)?) => {
$(
#[doc = $doc]
)?
pub type $acronym<T> = SiUnit<T, $second, $metre, $kilogram, $ampere, $kelvin, $mole, $candela>;
#[allow(non_snake_case)]
$(
#[doc = $doc]
)?
pub const fn $acronym<T>(value: T) -> $acronym<T> {
$acronym::new(value)
}
};
}
// Base units // Base units
pub type Unit<T> = SiUnit<T, Z0, Z0, Z0, Z0, Z0, Z0, Z0>; unit!(Unit -> Z0, Z0, Z0, Z0, Z0, Z0, Z0);
/// time unit!(Second -> P1, Z0, Z0, Z0, Z0, Z0, Z0; "time");
pub type Second<T> = SiUnit<T, P1, Z0, Z0, Z0, Z0, Z0, Z0>; //pub type Second<T> = SiUnit<T, P1, Z0, Z0, Z0, Z0, Z0, Z0>;
/// length /// length
pub type Metre<T> = SiUnit<T, Z0, P1, Z0, Z0, Z0, Z0, Z0>; pub type Metre<T> = SiUnit<T, Z0, P1, Z0, Z0, Z0, Z0, Z0>;
/// mass /// mass