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 } 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"]

View File

@ -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,
} }
} }
} }

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