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 3 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
68 changes: 68 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5250,3 +5250,71 @@ 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 {
#[inline]
fn from(small: $Small) -> $Large {
// SAFETY: input type guarantees the value is non-zero
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 = "nz_int_conv", issue = "66196")] }

This comment was marked as resolved.

Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't trait impls insta-stable? If so, I am not sure if tracking issue makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

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

@newpavlov you're right. Insta stability is something I hadn't in mind, but peter already fixed that ;)

nzint_impl_from! { NonZeroU8, NonZeroU32, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroU64, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroU128, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU8, NonZeroUsize, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroU32, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroU64, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU16, NonZeroU128, #[unstable(feature = "nz_int_conv", issue = "66196")] }

This comment was marked as resolved.

nzint_impl_from! { NonZeroU16, NonZeroUsize, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU32, NonZeroU64, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU32, NonZeroU128, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroU64, NonZeroU128, #[unstable(feature = "nz_int_conv", issue = "66196")] }

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

This comment was marked as resolved.

nzint_impl_from! { NonZeroI16, NonZeroIsize, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI32, NonZeroI64, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI32, NonZeroI128, #[unstable(feature = "nz_int_conv", issue = "66196")] }
nzint_impl_from! { NonZeroI64, NonZeroI128, #[unstable(feature = "nz_int_conv", issue = "66196")] }

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