Skip to content

Commit 7bf66d9

Browse files
author
Peter
committed
From<NonZero*> impls for wider NonZero types
issue: #66196
1 parent 86c2832 commit 7bf66d9

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/libcore/num/mod.rs

+70
Original file line numberDiff line numberDiff line change
@@ -5250,3 +5250,73 @@ impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
52505250

52515251
// Float -> Float
52525252
impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
5253+
5254+
// Conversion traits for non-zero integer types
5255+
macro_rules! nzint_impl_from {
5256+
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
5257+
#[$attr]
5258+
#[doc = $doc]
5259+
impl From<$Small> for $Large {
5260+
/// Widens a non-zero integer without checking the value is zero.
5261+
///
5262+
/// # Safety
5263+
///
5264+
/// The value is assumed to be non-zero.
5265+
#[inline]
5266+
fn from(small: $Small) -> $Large {
5267+
unsafe {
5268+
<$Large>::new_unchecked(small.get().into())
5269+
}
5270+
}
5271+
}
5272+
};
5273+
($Small: ty, $Large: ty, #[$attr:meta]) => {
5274+
nzint_impl_from!($Small,
5275+
$Large,
5276+
#[$attr],
5277+
concat!("Converts `",
5278+
stringify!($Small),
5279+
"` to `",
5280+
stringify!($Large),
5281+
"` losslessly."));
5282+
}
5283+
}
5284+
5285+
// Non-zero Unsigned -> Non-zero Unsigned
5286+
nzint_impl_from! { NonZeroU8, NonZeroU16, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5287+
nzint_impl_from! { NonZeroU8, NonZeroU32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5288+
nzint_impl_from! { NonZeroU8, NonZeroU64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5289+
nzint_impl_from! { NonZeroU8, NonZeroU128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5290+
nzint_impl_from! { NonZeroU8, NonZeroUsize, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5291+
nzint_impl_from! { NonZeroU16, NonZeroU32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5292+
nzint_impl_from! { NonZeroU16, NonZeroU64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5293+
nzint_impl_from! { NonZeroU16, NonZeroU128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5294+
nzint_impl_from! { NonZeroU32, NonZeroU64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5295+
nzint_impl_from! { NonZeroU32, NonZeroU128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5296+
nzint_impl_from! { NonZeroU64, NonZeroU128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5297+
5298+
// Non-zero Signed -> Non-zero Signed
5299+
nzint_impl_from! { NonZeroI8, NonZeroI16, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5300+
nzint_impl_from! { NonZeroI8, NonZeroI32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5301+
nzint_impl_from! { NonZeroI8, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5302+
nzint_impl_from! { NonZeroI8, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5303+
nzint_impl_from! { NonZeroI8, NonZeroIsize, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5304+
nzint_impl_from! { NonZeroI16, NonZeroI32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5305+
nzint_impl_from! { NonZeroI16, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5306+
nzint_impl_from! { NonZeroI16, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5307+
nzint_impl_from! { NonZeroI32, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5308+
nzint_impl_from! { NonZeroI32, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5309+
nzint_impl_from! { NonZeroI64, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5310+
5311+
// NonZero UnSigned -> Non-zero Signed
5312+
nzint_impl_from! { NonZeroU8, NonZeroI16, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5313+
nzint_impl_from! { NonZeroU8, NonZeroI32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5314+
nzint_impl_from! { NonZeroU8, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5315+
nzint_impl_from! { NonZeroU8, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5316+
nzint_impl_from! { NonZeroU8, NonZeroIsize, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5317+
nzint_impl_from! { NonZeroU16, NonZeroI32, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5318+
nzint_impl_from! { NonZeroU16, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5319+
nzint_impl_from! { NonZeroU16, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5320+
nzint_impl_from! { NonZeroU32, NonZeroI64, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5321+
nzint_impl_from! { NonZeroU32, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }
5322+
nzint_impl_from! { NonZeroU64, NonZeroI128, #[unstable(feature = "lossless_non_zero_int_conv", issue = "66196")] }

0 commit comments

Comments
 (0)