const Deref and const Neg impl

This commit is contained in:
Max Känner 2023-04-06 22:33:27 +02:00
parent 7fb0b6cda3
commit 93f8051626
2 changed files with 45 additions and 0 deletions

View File

@ -22,3 +22,4 @@ default = ["std"]
defmt = ["dep:defmt"]
fixed = ["dep:fixed"]
std = ["num-traits/std"]
nightly = []

View File

@ -1,7 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "nightly", feature(const_trait_impl))]
#![cfg_attr(feature = "nightly", feature(trait_alias))]
mod display;
pub mod types;
#[cfg(feature = "nightly")]
use core::marker::Destruct;
use core::{
marker::PhantomData,
ops::{
@ -37,6 +41,7 @@ where
_cd: PhantomData<Candela>,
}
#[cfg(not(feature = "nightly"))]
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> Deref
for SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>
where
@ -54,6 +59,24 @@ where
&self.value
}
}
#[cfg(feature = "nightly")]
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> const Deref
for SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>
where
Second: Integer,
Metre: Integer,
Kilogram: Integer,
Ampere: Integer,
Kelvin: Integer,
Mole: Integer,
Candela: Integer,
{
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> DerefMut
for SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>
@ -71,6 +94,7 @@ where
}
}
#[cfg(not(feature = "nightly"))]
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> Neg
for SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>
where
@ -89,6 +113,26 @@ where
Self::Output::new(-self.value)
}
}
#[cfg(feature = "nightly")]
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> const Neg
for SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>
where
Second: Integer,
Metre: Integer,
Kilogram: Integer,
Ampere: Integer,
Kelvin: Integer,
Mole: Integer,
Candela: Integer,
T: ~const Neg,
Self: ~const Destruct,
{
type Output = SiUnit<T::Output, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>;
fn neg(self) -> Self::Output {
Self::Output::new(-self.value)
}
}
impl<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela> Zero
for SiUnit<T, Second, Metre, Kilogram, Ampere, Kelvin, Mole, Candela>