Skip to content

Commit 35c3e34

Browse files
f-frmbrubeck
authored andcommitted
Implement FromPrimitive for NotNaN
1 parent 96ff7e7 commit 35c3e34

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::fmt;
1515
use std::io;
1616
use std::mem;
1717
use unreachable::unreachable;
18-
use num_traits::{Bounded, Float, One, Zero};
18+
use num_traits::{Bounded, Float, FromPrimitive, One, Zero};
1919

2020
/// A wrapper around Floats providing an implementation of Ord and Hash.
2121
///
@@ -589,6 +589,22 @@ impl<T: Float + Bounded> Bounded for NotNaN<T> {
589589
}
590590
}
591591

592+
impl<T: Float + FromPrimitive> FromPrimitive for NotNaN<T> {
593+
fn from_i64(n: i64) -> Option<Self> { T::from_i64(n).and_then(|n| NotNaN::new(n).ok()) }
594+
fn from_u64(n: u64) -> Option<Self> { T::from_u64(n).and_then(|n| NotNaN::new(n).ok()) }
595+
596+
fn from_isize(n: isize) -> Option<Self> { T::from_isize(n).and_then(|n| NotNaN::new(n).ok()) }
597+
fn from_i8(n: i8) -> Option<Self> { T::from_i8(n).and_then(|n| NotNaN::new(n).ok()) }
598+
fn from_i16(n: i16) -> Option<Self> { T::from_i16(n).and_then(|n| NotNaN::new(n).ok()) }
599+
fn from_i32(n: i32) -> Option<Self> { T::from_i32(n).and_then(|n| NotNaN::new(n).ok()) }
600+
fn from_usize(n: usize) -> Option<Self> { T::from_usize(n).and_then(|n| NotNaN::new(n).ok()) }
601+
fn from_u8(n: u8) -> Option<Self> { T::from_u8(n).and_then(|n| NotNaN::new(n).ok()) }
602+
fn from_u16(n: u16) -> Option<Self> { T::from_u16(n).and_then(|n| NotNaN::new(n).ok()) }
603+
fn from_u32(n: u32) -> Option<Self> { T::from_u32(n).and_then(|n| NotNaN::new(n).ok()) }
604+
fn from_f32(n: f32) -> Option<Self> { T::from_f32(n).and_then(|n| NotNaN::new(n).ok()) }
605+
fn from_f64(n: f64) -> Option<Self> { T::from_f64(n).and_then(|n| NotNaN::new(n).ok()) }
606+
}
607+
592608
#[cfg(feature = "serde")]
593609
mod impl_serde {
594610
extern crate serde;

tests/test.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate ordered_float;
55
extern crate num_traits;
66

77
pub use ordered_float::*;
8-
pub use num_traits::{Bounded, Float, One, Zero};
8+
pub use num_traits::{Bounded, Float, FromPrimitive, One, Zero};
99
pub use std::cmp::Ordering::*;
1010
pub use std::{f32, f64, panic};
1111

@@ -118,6 +118,25 @@ describe! not_nan32 {
118118
assert_eq!(NotNaN::<f32>::min_value(), NotNaN::from(<f32 as Bounded>::min_value()));
119119
assert_eq!(NotNaN::<f32>::max_value(), NotNaN::from(<f32 as Bounded>::max_value()));
120120
}
121+
122+
it "should implement FromPrimitive" {
123+
assert_eq!(NotNaN::<f32>::from_i8(42i8), Some(NotNaN::from(42.0)));
124+
assert_eq!(NotNaN::<f32>::from_u8(42u8), Some(NotNaN::from(42.0)));
125+
assert_eq!(NotNaN::<f32>::from_i16(42i16), Some(NotNaN::from(42.0)));
126+
assert_eq!(NotNaN::<f32>::from_u16(42u16), Some(NotNaN::from(42.0)));
127+
assert_eq!(NotNaN::<f32>::from_i32(42i32), Some(NotNaN::from(42.0)));
128+
assert_eq!(NotNaN::<f32>::from_u32(42u32), Some(NotNaN::from(42.0)));
129+
assert_eq!(NotNaN::<f32>::from_i64(42i64), Some(NotNaN::from(42.0)));
130+
assert_eq!(NotNaN::<f32>::from_u64(42u64), Some(NotNaN::from(42.0)));
131+
assert_eq!(NotNaN::<f32>::from_isize(42isize), Some(NotNaN::from(42.0)));
132+
assert_eq!(NotNaN::<f32>::from_usize(42usize), Some(NotNaN::from(42.0)));
133+
assert_eq!(NotNaN::<f32>::from_f32(42f32), Some(NotNaN::from(42.0)));
134+
assert_eq!(NotNaN::<f32>::from_f32(42f32), Some(NotNaN::from(42.0)));
135+
assert_eq!(NotNaN::<f32>::from_f64(42f64), Some(NotNaN::from(42.0)));
136+
assert_eq!(NotNaN::<f32>::from_f64(42f64), Some(NotNaN::from(42.0)));
137+
assert_eq!(NotNaN::<f32>::from_f32(Float::nan()), None);
138+
assert_eq!(NotNaN::<f32>::from_f64(Float::nan()), None);
139+
}
121140
}
122141

123142
describe! not_nan64 {
@@ -195,6 +214,25 @@ describe! not_nan64 {
195214
assert_eq!(NotNaN::<f64>::min_value(), NotNaN::from(<f64 as Bounded>::min_value()));
196215
assert_eq!(NotNaN::<f64>::max_value(), NotNaN::from(<f64 as Bounded>::max_value()));
197216
}
217+
218+
it "should implement FromPrimitive" {
219+
assert_eq!(NotNaN::<f64>::from_i8(42i8), Some(NotNaN::from(42.0)));
220+
assert_eq!(NotNaN::<f64>::from_u8(42u8), Some(NotNaN::from(42.0)));
221+
assert_eq!(NotNaN::<f64>::from_i16(42i16), Some(NotNaN::from(42.0)));
222+
assert_eq!(NotNaN::<f64>::from_u16(42u16), Some(NotNaN::from(42.0)));
223+
assert_eq!(NotNaN::<f64>::from_i32(42i32), Some(NotNaN::from(42.0)));
224+
assert_eq!(NotNaN::<f64>::from_u32(42u32), Some(NotNaN::from(42.0)));
225+
assert_eq!(NotNaN::<f64>::from_i64(42i64), Some(NotNaN::from(42.0)));
226+
assert_eq!(NotNaN::<f64>::from_u64(42u64), Some(NotNaN::from(42.0)));
227+
assert_eq!(NotNaN::<f64>::from_isize(42isize), Some(NotNaN::from(42.0)));
228+
assert_eq!(NotNaN::<f64>::from_usize(42usize), Some(NotNaN::from(42.0)));
229+
assert_eq!(NotNaN::<f64>::from_f32(42f32), Some(NotNaN::from(42.0)));
230+
assert_eq!(NotNaN::<f64>::from_f32(42f32), Some(NotNaN::from(42.0)));
231+
assert_eq!(NotNaN::<f64>::from_f64(42f64), Some(NotNaN::from(42.0)));
232+
assert_eq!(NotNaN::<f64>::from_f64(42f64), Some(NotNaN::from(42.0)));
233+
assert_eq!(NotNaN::<f64>::from_f32(Float::nan()), None);
234+
assert_eq!(NotNaN::<f64>::from_f64(Float::nan()), None);
235+
}
198236
}
199237

200238
describe! hashing {

0 commit comments

Comments
 (0)