Skip to content

Commit 8333afb

Browse files
f-frmbrubeck
authored andcommitted
Implement ToPrimitive for NotNaN
1 parent 35c3e34 commit 8333afb

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-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, FromPrimitive, One, Zero};
18+
use num_traits::{Bounded, Float, FromPrimitive, One, ToPrimitive, Zero};
1919

2020
/// A wrapper around Floats providing an implementation of Ord and Hash.
2121
///
@@ -605,6 +605,22 @@ impl<T: Float + FromPrimitive> FromPrimitive for NotNaN<T> {
605605
fn from_f64(n: f64) -> Option<Self> { T::from_f64(n).and_then(|n| NotNaN::new(n).ok()) }
606606
}
607607

608+
impl<T: Float + ToPrimitive> ToPrimitive for NotNaN<T> {
609+
fn to_i64(&self) -> Option<i64> { self.0.to_i64() }
610+
fn to_u64(&self) -> Option<u64> { self.0.to_u64() }
611+
612+
fn to_isize(&self) -> Option<isize> { self.0.to_isize() }
613+
fn to_i8(&self) -> Option<i8> { self.0.to_i8() }
614+
fn to_i16(&self) -> Option<i16> { self.0.to_i16() }
615+
fn to_i32(&self) -> Option<i32> { self.0.to_i32() }
616+
fn to_usize(&self) -> Option<usize> { self.0.to_usize() }
617+
fn to_u8(&self) -> Option<u8> { self.0.to_u8() }
618+
fn to_u16(&self) -> Option<u16> { self.0.to_u16() }
619+
fn to_u32(&self) -> Option<u32> { self.0.to_u32() }
620+
fn to_f32(&self) -> Option<f32> { self.0.to_f32() }
621+
fn to_f64(&self) -> Option<f64> { self.0.to_f64() }
622+
}
623+
608624
#[cfg(feature = "serde")]
609625
mod impl_serde {
610626
extern crate serde;

tests/test.rs

Lines changed: 37 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, FromPrimitive, One, Zero};
8+
pub use num_traits::{Bounded, Float, FromPrimitive, One, ToPrimitive, Zero};
99
pub use std::cmp::Ordering::*;
1010
pub use std::{f32, f64, panic};
1111

@@ -137,6 +137,24 @@ describe! not_nan32 {
137137
assert_eq!(NotNaN::<f32>::from_f32(Float::nan()), None);
138138
assert_eq!(NotNaN::<f32>::from_f64(Float::nan()), None);
139139
}
140+
141+
it "should implement ToPrimitive" {
142+
let x = NotNaN::from(42.0f32);
143+
assert_eq!(x.to_u8(), Some(42u8));
144+
assert_eq!(x.to_i8(), Some(42i8));
145+
assert_eq!(x.to_u16(), Some(42u16));
146+
assert_eq!(x.to_i16(), Some(42i16));
147+
assert_eq!(x.to_u32(), Some(42u32));
148+
assert_eq!(x.to_i32(), Some(42i32));
149+
assert_eq!(x.to_u64(), Some(42u64));
150+
assert_eq!(x.to_i64(), Some(42i64));
151+
assert_eq!(x.to_usize(), Some(42usize));
152+
assert_eq!(x.to_isize(), Some(42isize));
153+
assert_eq!(x.to_f32(), Some(42f32));
154+
assert_eq!(x.to_f32(), Some(42f32));
155+
assert_eq!(x.to_f64(), Some(42f64));
156+
assert_eq!(x.to_f64(), Some(42f64));
157+
}
140158
}
141159

142160
describe! not_nan64 {
@@ -233,6 +251,24 @@ describe! not_nan64 {
233251
assert_eq!(NotNaN::<f64>::from_f32(Float::nan()), None);
234252
assert_eq!(NotNaN::<f64>::from_f64(Float::nan()), None);
235253
}
254+
255+
it "should implement ToPrimitive" {
256+
let x = NotNaN::from(42.0f64);
257+
assert_eq!(x.to_u8(), Some(42u8));
258+
assert_eq!(x.to_i8(), Some(42i8));
259+
assert_eq!(x.to_u16(), Some(42u16));
260+
assert_eq!(x.to_i16(), Some(42i16));
261+
assert_eq!(x.to_u32(), Some(42u32));
262+
assert_eq!(x.to_i32(), Some(42i32));
263+
assert_eq!(x.to_u64(), Some(42u64));
264+
assert_eq!(x.to_i64(), Some(42i64));
265+
assert_eq!(x.to_usize(), Some(42usize));
266+
assert_eq!(x.to_isize(), Some(42isize));
267+
assert_eq!(x.to_f32(), Some(42f32));
268+
assert_eq!(x.to_f32(), Some(42f32));
269+
assert_eq!(x.to_f64(), Some(42f64));
270+
assert_eq!(x.to_f64(), Some(42f64));
271+
}
236272
}
237273

238274
describe! hashing {

0 commit comments

Comments
 (0)