Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add From<i32> and similar impls to NotNan #103

Merged
merged 1 commit into from
Jan 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,30 @@ impl TryFrom<f64> for NotNan<f64> {
}
}

macro_rules! impl_from_int_primitive {
($primitive:ty, $inner:ty) => {
impl From<$primitive> for NotNan<$inner> {
fn from(source: $primitive) -> Self {
// the primitives with which this macro will be called cannot hold a value that
// f64::from would convert to NaN, so this does not hurt invariants
NotNan(<$inner as From<$primitive>>::from(source))
}
}
};
}

impl_from_int_primitive!(i8, f64);
impl_from_int_primitive!(i16, f64);
impl_from_int_primitive!(i32, f64);
impl_from_int_primitive!(u8, f64);
impl_from_int_primitive!(u16, f64);
impl_from_int_primitive!(u32, f64);

impl_from_int_primitive!(i8, f32);
impl_from_int_primitive!(i16, f32);
impl_from_int_primitive!(u8, f32);
impl_from_int_primitive!(u16, f32);

impl From<NotNan<f32>> for NotNan<f64> {
#[inline]
fn from(v: NotNan<f32>) -> NotNan<f64> {
Expand Down