Compare commits
5 Commits
7fb0b6cda3
...
main
Author | SHA1 | Date | |
---|---|---|---|
745dcac6bf | |||
4c73f860d3 | |||
67daf8d73f | |||
89a91ae59c | |||
93f8051626 |
@ -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"]
|
||||
|
35
src/lib.rs
35
src/lib.rs
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
22
src/types.rs
22
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<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
|
||||
|
Reference in New Issue
Block a user