Skip to content

Commit

Permalink
Add From<i32> and similar impls to NotNan
Browse files Browse the repository at this point in the history
  • Loading branch information
MultisampledNight authored and mbrubeck committed Jan 3, 2022
1 parent 76b11cd commit 8287026
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,30 @@ impl TryFrom<f64> for NotNan<f64> {
}
}

macro_rules! impl_from_int_primitive {
($primitive:ty, $inner:ty) => {
impl From<$primitive> for NotNan<$inner> {
fn from(source: $primitive) -> Self {
// the primitives with which this macro will be called cannot hold a value that
// f64::from would convert to NaN, so this does not hurt invariants
NotNan(<$inner as From<$primitive>>::from(source))
}
}
};
}

impl_from_int_primitive!(i8, f64);
impl_from_int_primitive!(i16, f64);
impl_from_int_primitive!(i32, f64);
impl_from_int_primitive!(u8, f64);
impl_from_int_primitive!(u16, f64);
impl_from_int_primitive!(u32, f64);

impl_from_int_primitive!(i8, f32);
impl_from_int_primitive!(i16, f32);
impl_from_int_primitive!(u8, f32);
impl_from_int_primitive!(u16, f32);

impl From<NotNan<f32>> for NotNan<f64> {
#[inline]
fn from(v: NotNan<f32>) -> NotNan<f64> {
Expand Down

0 comments on commit 8287026

Please sign in to comment.