Skip to content

Commit

Permalink
Replace From<T> with TryFrom<T> for NotNan<T>
Browse files Browse the repository at this point in the history
Fixes #56. Fixes #57.
  • Loading branch information
mbrubeck committed Jul 10, 2020
1 parent fc9dc0c commit 39f76bb
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: rust
rust:
- 1.28.0
- 1.34.0
- nightly
- beta
- stable
Expand Down
18 changes: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate num_traits;
#[cfg(feature = "std")] extern crate std;

use core::cmp::Ordering;
use core::convert::TryFrom;
use core::ops::{Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Rem,
RemAssign, Sub, SubAssign};
use core::hash::{Hash, Hasher};
Expand Down Expand Up @@ -300,12 +301,17 @@ impl From<NotNan<f64>> for f64 {
}
}

/// Creates a NotNan value from a Float.
///
/// Panics if the provided value is NaN or the computation results in NaN
impl<T: Float> From<T> for NotNan<T> {
fn from(v: T) -> Self {
NotNan::new(v).expect("Tried to create a NotNan from a NaN")
impl TryFrom<f32> for NotNan<f32> {
type Error = FloatIsNan;
fn try_from(v: f32) -> Result<Self, Self::Error> {
NotNan::new(v)
}
}

impl TryFrom<f64> for NotNan<f64> {
type Error = FloatIsNan;
fn try_from(v: f64) -> Result<Self, Self::Error> {
NotNan::new(v)
}
}

Expand Down
Loading

0 comments on commit 39f76bb

Please sign in to comment.