Skip to content

Commit 8287026

Browse files
MultisampledNightmbrubeck
authored andcommitted
Add From<i32> and similar impls to NotNan
1 parent 76b11cd commit 8287026

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,30 @@ impl TryFrom<f64> for NotNan<f64> {
984984
}
985985
}
986986

987+
macro_rules! impl_from_int_primitive {
988+
($primitive:ty, $inner:ty) => {
989+
impl From<$primitive> for NotNan<$inner> {
990+
fn from(source: $primitive) -> Self {
991+
// the primitives with which this macro will be called cannot hold a value that
992+
// f64::from would convert to NaN, so this does not hurt invariants
993+
NotNan(<$inner as From<$primitive>>::from(source))
994+
}
995+
}
996+
};
997+
}
998+
999+
impl_from_int_primitive!(i8, f64);
1000+
impl_from_int_primitive!(i16, f64);
1001+
impl_from_int_primitive!(i32, f64);
1002+
impl_from_int_primitive!(u8, f64);
1003+
impl_from_int_primitive!(u16, f64);
1004+
impl_from_int_primitive!(u32, f64);
1005+
1006+
impl_from_int_primitive!(i8, f32);
1007+
impl_from_int_primitive!(i16, f32);
1008+
impl_from_int_primitive!(u8, f32);
1009+
impl_from_int_primitive!(u16, f32);
1010+
9871011
impl From<NotNan<f32>> for NotNan<f64> {
9881012
#[inline]
9891013
fn from(v: NotNan<f32>) -> NotNan<f64> {

0 commit comments

Comments
 (0)