From 745dcac6bff340e1076d6cdd771b8501e87c2f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20K=C3=A4nner?= Date: Sun, 23 Apr 2023 18:39:49 +0200 Subject: [PATCH] Make the interface more Newtype like --- src/lib.rs | 21 +++++++-------------- src/types.rs | 22 +++++++++++++++++++--- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4d9e220..4ecbf10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,15 +31,13 @@ where Candela: Integer, { value: T, - _s: PhantomData, - _m: PhantomData, - _kg: PhantomData, - _a: PhantomData, - _k: PhantomData, - _mol: PhantomData, - _cd: PhantomData, + _marker: PhantomData<(Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela)>, } +#[allow(non_snake_case)] +pub fn SiUnit( + value: T, +) -> SiUnit where Second: Integer, Metre: Integer, @@ -49,6 +47,7 @@ where Mole: Integer, Candela: Integer, { + SiUnit::new(value) } impl Deref @@ -609,13 +608,7 @@ where pub const fn new(value: T) -> Self { Self { value, - _s: PhantomData, - _m: PhantomData, - _kg: PhantomData, - _a: PhantomData, - _k: PhantomData, - _mol: PhantomData, - _cd: PhantomData, + _marker: PhantomData, } } } diff --git a/src/types.rs b/src/types.rs index c5a755e..f598b5a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,10 +1,26 @@ use crate::SiUnit; 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 = SiUnit; + #[allow(non_snake_case)] + $( + #[doc = $doc] + )? + pub const fn $acronym(value: T) -> $acronym { + $acronym::new(value) + } + }; +} + // Base units -pub type Unit = SiUnit; -/// time -pub type Second = SiUnit; +unit!(Unit -> Z0, Z0, Z0, Z0, Z0, Z0, Z0); +unit!(Second -> P1, Z0, Z0, Z0, Z0, Z0, Z0; "time"); +//pub type Second = SiUnit; /// length pub type Metre = SiUnit; /// mass