Skip to content

Commit 21c7f22

Browse files
committed
Fix up some code style and update some trait bounds.
1 parent e2a0efe commit 21c7f22

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/lib.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
extern crate num_traits;
88
#[cfg(feature = "std")] extern crate std;
9+
#[cfg(feature = "std")] use std::error::Error;
910

1011
use core::cmp::Ordering;
1112
use core::convert::TryFrom;
@@ -39,7 +40,7 @@ const CANONICAL_ZERO_BITS: u64 = 0x0u64;
3940
/// to itself, in contradiction with the IEEE standard.
4041
#[derive(Debug, Default, Clone, Copy)]
4142
#[repr(transparent)]
42-
pub struct OrderedFloat<T: Float>(pub T);
43+
pub struct OrderedFloat<T>(pub T);
4344

4445
impl<T: Float> OrderedFloat<T> {
4546
/// Get the value out.
@@ -240,6 +241,8 @@ pub struct NotNan<T>(T);
240241
impl<T> NotNan<T> {
241242
/// Create a NotNan value from a value that is guaranteed to not be NaN
242243
///
244+
/// # Safety
245+
///
243246
/// Behaviour is undefined if `val` is NaN
244247
pub const unsafe fn unchecked_new(val: T) -> Self {
245248
NotNan(val)
@@ -278,6 +281,7 @@ impl<T: Float> Ord for NotNan<T> {
278281
}
279282
}
280283

284+
#[allow(clippy::derive_hash_xor_eq)]
281285
impl<T: Float> Hash for NotNan<T> {
282286
fn hash<H: Hasher>(&self, state: &mut H) {
283287
hash_float(self.as_ref(), state)
@@ -385,9 +389,9 @@ impl<T: Float + Sum> Sum for NotNan<T> {
385389
}
386390
}
387391

388-
impl<'a, T: Float + Sum> Sum<&'a NotNan<T>> for NotNan<T> {
392+
impl<'a, T: Float + Sum + 'a> Sum<&'a NotNan<T>> for NotNan<T> {
389393
fn sum<I: Iterator<Item = &'a NotNan<T>>>(iter: I) -> Self {
390-
iter.map(|v| *v).sum()
394+
iter.cloned().sum()
391395
}
392396
}
393397

@@ -467,9 +471,9 @@ impl<T: Float + Product> Product for NotNan<T> {
467471
}
468472
}
469473

470-
impl<'a, T: Float + Product> Product<&'a NotNan<T>> for NotNan<T> {
474+
impl<'a, T: Float + Product + 'a> Product<&'a NotNan<T>> for NotNan<T> {
471475
fn product<I: Iterator<Item = &'a NotNan<T>>>(iter: I) -> Self {
472-
iter.map(|v| *v).product()
476+
iter.cloned().product()
473477
}
474478
}
475479

@@ -556,7 +560,7 @@ impl<T: Float> Neg for NotNan<T> {
556560
pub struct FloatIsNan;
557561

558562
#[cfg(feature = "std")]
559-
impl std::error::Error for FloatIsNan {
563+
impl Error for FloatIsNan {
560564
fn description(&self) -> &str {
561565
"NotNan constructed with NaN"
562566
}
@@ -678,18 +682,25 @@ pub enum ParseNotNanError<E> {
678682
}
679683

680684
#[cfg(feature = "std")]
681-
impl<E: fmt::Debug> std::error::Error for ParseNotNanError<E> {
685+
impl<E: fmt::Debug + Error + 'static> Error for ParseNotNanError<E> {
682686
fn description(&self) -> &str {
683-
return "Error parsing a not-NaN floating point value";
687+
"Error parsing a not-NaN floating point value"
684688
}
685689

686-
// TODO: add an implementation of cause(). This will be breaking because it requires E: Error.
690+
fn source(&self) -> Option<&(dyn Error + 'static)> {
691+
match self {
692+
ParseNotNanError::ParseFloatError(e) => Some(e),
693+
ParseNotNanError::IsNaN => None,
694+
}
695+
}
687696
}
688697

689-
impl<E: fmt::Debug> fmt::Display for ParseNotNanError<E> {
698+
impl<E: fmt::Display> fmt::Display for ParseNotNanError<E> {
690699
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
691-
// TODO: replace this with a human readable fmt. Will require E: Display.
692-
<Self as fmt::Debug>::fmt(self, f)
700+
match self {
701+
ParseNotNanError::ParseFloatError(e) => write!(f, "Parse error: {}", e),
702+
ParseNotNanError::IsNaN => write!(f, "NotNan parser encounter a NaN"),
703+
}
693704
}
694705
}
695706

@@ -698,7 +709,7 @@ impl<T: Float> Num for NotNan<T> {
698709

699710
fn from_str_radix(src: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
700711
T::from_str_radix(src, radix)
701-
.map_err(|err| ParseNotNanError::ParseFloatError(err))
712+
.map_err(ParseNotNanError::ParseFloatError)
702713
.and_then(|n| NotNan::new(n).map_err(|_| ParseNotNanError::IsNaN))
703714
}
704715
}

tests/test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,5 @@ fn not_nan64_sum_product() {
588588
#[test]
589589
fn not_nan_usage_in_const_context() {
590590
const A: NotNan<f32> = unsafe { NotNan::unchecked_new(111f32) };
591-
592591
assert_eq!(A, NotNan::new(111f32).unwrap());
593592
}

0 commit comments

Comments
 (0)