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

From<NonZero*> impls for wider NonZero types #66277

Merged
merged 5 commits into from
Dec 10, 2019
Merged
Changes from 1 commit
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
70 changes: 70 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5250,3 +5250,73 @@ impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"

// Float -> Float
impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }

// Conversion traits for non-zero integer types
macro_rules! nzint_impl_from {
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
#[$attr]
#[doc = $doc]
impl From<$Small> for $Large {
/// Widens a non-zero integer without checking the value is zero.
///
/// # Safety
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safety section here is irrelevant, because the input type already guarantees the value is non-zero.

///
/// The value is assumed to be non-zero.
#[inline]
fn from(small: $Small) -> $Large {
unsafe {
<$Large>::new_unchecked(small.get().into())
}
}
}
};
($Small: ty, $Large: ty, #[$attr:meta]) => {
nzint_impl_from!($Small,
$Large,
#[$attr],
concat!("Converts `",
stringify!($Small),
"` to `",
stringify!($Large),
"` losslessly."));
}
}

// Non-zero Unsigned -> Non-zero Unsigned
nzint_impl_from! { NonZeroU8, NonZeroU16, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroU32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroU64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroU128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroUsize, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroU32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroU64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroU128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU32, NonZeroU64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU32, NonZeroU128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU64, NonZeroU128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }

// Non-zero Signed -> Non-zero Signed
nzint_impl_from! { NonZeroI8, NonZeroI16, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI8, NonZeroI32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI8, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI8, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI8, NonZeroIsize, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI16, NonZeroI32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI16, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI16, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI32, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI32, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI64, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }

// NonZero UnSigned -> Non-zero Signed
nzint_impl_from! { NonZeroU8, NonZeroI16, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroI32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroIsize, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroI32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU32, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU32, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU64, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }