Compare commits

...

5 Commits

Author SHA1 Message Date
745dcac6bf Make the interface more Newtype like 2023-04-23 18:39:49 +02:00
4c73f860d3 Remove const impl 2023-04-23 18:37:17 +02:00
67daf8d73f Make serde optional 2023-04-16 16:08:56 +02:00
89a91ae59c Add serde support 2023-04-16 16:07:31 +02:00
93f8051626 const Deref and const Neg impl 2023-04-06 22:33:27 +02:00
3 changed files with 44 additions and 17 deletions

View File

@ -16,9 +16,13 @@ typenum = "1.16"
num-traits = { version = "0.2", default-features = false }
defmt = { version = "0.3", optional = true }
fixed = { version = "1.23.0", optional = true }
serde = { version = "1.0", default-features = false, features = [
"derive",
], optional = true }
[features]
default = ["std"]
defmt = ["dep:defmt"]
fixed = ["dep:fixed"]
std = ["num-traits/std"]
serde = ["dep:serde"]

View File

@ -2,6 +2,8 @@
mod display;
pub mod types;
#[cfg(feature = "nightly")]
use core::marker::Destruct;
use core::{
marker::PhantomData,
ops::{
@ -17,6 +19,7 @@ use typenum::{int::Z0, op, Integer};
use types::Unit;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>
where
Second: Integer,
@ -28,13 +31,23 @@ where
Candela: Integer,
{
value: T,
_s: PhantomData<Second>,
_m: PhantomData<Metre>,
_kg: PhantomData<Kilogram>,
_a: PhantomData<Ampere>,
_k: PhantomData<Kelvin>,
_mol: PhantomData<Mole>,
_cd: PhantomData<Candela>,
_marker: PhantomData<(Second, Metre, Kilogram, Ampere, Kelvin, Mole, 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
Second: Integer,
Metre: Integer,
Kilogram: Integer,
Ampere: Integer,
Kelvin: Integer,
Mole: Integer,
Candela: Integer,
{
SiUnit::new(value)
}
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> Deref
@ -595,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,
}
}
}

View File

@ -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<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
pub type Unit<T> = SiUnit<T, Z0, Z0, Z0, Z0, Z0, Z0, Z0>;
/// time
pub type Second<T> = SiUnit<T, P1, Z0, Z0, Z0, Z0, Z0, Z0>;
unit!(Unit -> Z0, Z0, Z0, Z0, Z0, Z0, Z0);
unit!(Second -> P1, Z0, Z0, Z0, Z0, Z0, Z0; "time");
//pub type Second<T> = SiUnit<T, P1, Z0, Z0, Z0, Z0, Z0, Z0>;
/// length
pub type Metre<T> = SiUnit<T, Z0, P1, Z0, Z0, Z0, Z0, Z0>;
/// mass