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 }
|
num-traits = { version = "0.2", default-features = false }
|
||||||
defmt = { version = "0.3", optional = true }
|
defmt = { version = "0.3", optional = true }
|
||||||
fixed = { version = "1.23.0", optional = true }
|
fixed = { version = "1.23.0", optional = true }
|
||||||
|
serde = { version = "1.0", default-features = false, features = [
|
||||||
|
"derive",
|
||||||
|
], optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
defmt = ["dep:defmt"]
|
defmt = ["dep:defmt"]
|
||||||
fixed = ["dep:fixed"]
|
fixed = ["dep:fixed"]
|
||||||
std = ["num-traits/std"]
|
std = ["num-traits/std"]
|
||||||
|
serde = ["dep:serde"]
|
||||||
|
35
src/lib.rs
35
src/lib.rs
@ -2,6 +2,8 @@
|
|||||||
mod display;
|
mod display;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
#[cfg(feature = "nightly")]
|
||||||
|
use core::marker::Destruct;
|
||||||
use core::{
|
use core::{
|
||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
ops::{
|
ops::{
|
||||||
@ -17,6 +19,7 @@ use typenum::{int::Z0, op, Integer};
|
|||||||
use types::Unit;
|
use types::Unit;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
#[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>
|
pub struct SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>
|
||||||
where
|
where
|
||||||
Second: Integer,
|
Second: Integer,
|
||||||
@ -28,13 +31,23 @@ 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>,
|
#[allow(non_snake_case)]
|
||||||
_k: PhantomData<Kelvin>,
|
pub fn SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>(
|
||||||
_mol: PhantomData<Mole>,
|
value: T,
|
||||||
_cd: PhantomData<Candela>,
|
) -> 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
|
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> Deref
|
||||||
@ -595,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,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
22
src/types.rs
22
src/types.rs
@ -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
|
||||||
|
Reference in New Issue
Block a user