Skip to content

Commit fa32827

Browse files
f-frmbrubeck
authored andcommitted
Implement Bounded for NotNaN
1 parent 9675e56 commit fa32827

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/lib.rs

Lines changed: 11 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::Float;
18+
use num_traits::{Bounded, Float};
1919

2020
/// A wrapper around Floats providing an implementation of Ord and Hash.
2121
///
@@ -569,6 +569,16 @@ fn raw_double_bits<F: Float>(f: &F) -> u64 {
569569
(man & MAN_MASK) | ((exp_u64 << 52) & EXP_MASK) | ((sign_u64 << 63) & SIGN_MASK)
570570
}
571571

572+
impl<T: Float + Bounded> Bounded for NotNaN<T> {
573+
fn min_value() -> Self {
574+
NotNaN(Bounded::min_value())
575+
}
576+
577+
fn max_value() -> Self {
578+
NotNaN(Bounded::max_value())
579+
}
580+
}
581+
572582
#[cfg(feature = "serde")]
573583
mod impl_serde {
574584
extern crate serde;

tests/test.rs

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

@@ -104,6 +104,11 @@ describe! not_nan32 {
104104
assert!(panic::catch_unwind(|| {let mut tmp = NotNan::from(0.0f32); tmp /= f32::NAN;}).is_err());
105105
assert!(panic::catch_unwind(|| {let mut tmp = NotNan::from(0.0f32); tmp %= f32::NAN;}).is_err());
106106
}
107+
108+
it "should implement Bounded" {
109+
assert_eq!(NotNaN::<f32>::min_value(), NotNaN::from(<f32 as Bounded>::min_value()));
110+
assert_eq!(NotNaN::<f32>::max_value(), NotNaN::from(<f32 as Bounded>::max_value()));
111+
}
107112
}
108113

109114
describe! not_nan64 {
@@ -167,6 +172,11 @@ describe! not_nan64 {
167172
assert!(panic::catch_unwind(|| {let mut tmp = NotNan::from(0.0f64); tmp /= f64::NAN;}).is_err());
168173
assert!(panic::catch_unwind(|| {let mut tmp = NotNan::from(0.0f64); tmp %= f64::NAN;}).is_err());
169174
}
175+
176+
it "should implement Bounded" {
177+
assert_eq!(NotNaN::<f64>::min_value(), NotNaN::from(<f64 as Bounded>::min_value()));
178+
assert_eq!(NotNaN::<f64>::max_value(), NotNaN::from(<f64 as Bounded>::max_value()));
179+
}
170180
}
171181

172182
describe! hashing {

0 commit comments

Comments
 (0)