Skip to content

Commit a7ae234

Browse files
authored
Unrolled build for rust-lang#136511
Rollup merge of rust-lang#136511 - joshtriplett:nonzero-cast-signed-unsigned, r=dtolnay Add `cast_signed` and `cast_unsigned` methods for `NonZero` types Requested in rust-lang#125882 . Note that this keeps the same names as the methods currently present on other integer types. If we want to rename them, we can rename them all at the same time.
2 parents 7b31983 + f4a92e3 commit a7ae234

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

library/core/src/num/nonzero.rs

+64-2
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ macro_rules! nonzero_integer {
474474
#[$stability:meta]
475475
Self = $Ty:ident,
476476
Primitive = $signedness:ident $Int:ident,
477+
SignedPrimitive = $Sint:ty,
477478
UnsignedPrimitive = $Uint:ty,
478479

479480
// Used in doc comments.
@@ -905,6 +906,7 @@ macro_rules! nonzero_integer {
905906

906907
nonzero_integer_signedness_dependent_methods! {
907908
Primitive = $signedness $Int,
909+
SignedPrimitive = $Sint,
908910
UnsignedPrimitive = $Uint,
909911
}
910912

@@ -1128,6 +1130,7 @@ macro_rules! nonzero_integer {
11281130
(
11291131
Self = $Ty:ident,
11301132
Primitive = unsigned $Int:ident,
1133+
SignedPrimitive = $Sint:ident,
11311134
rot = $rot:literal,
11321135
rot_op = $rot_op:literal,
11331136
rot_result = $rot_result:literal,
@@ -1140,6 +1143,7 @@ macro_rules! nonzero_integer {
11401143
#[stable(feature = "nonzero", since = "1.28.0")]
11411144
Self = $Ty,
11421145
Primitive = unsigned $Int,
1146+
SignedPrimitive = $Sint,
11431147
UnsignedPrimitive = $Int,
11441148
rot = $rot,
11451149
rot_op = $rot_op,
@@ -1154,7 +1158,7 @@ macro_rules! nonzero_integer {
11541158
(
11551159
Self = $Ty:ident,
11561160
Primitive = signed $Int:ident,
1157-
UnsignedPrimitive = $UInt:ident,
1161+
UnsignedPrimitive = $Uint:ident,
11581162
rot = $rot:literal,
11591163
rot_op = $rot_op:literal,
11601164
rot_result = $rot_result:literal,
@@ -1166,7 +1170,8 @@ macro_rules! nonzero_integer {
11661170
#[stable(feature = "signed_nonzero", since = "1.34.0")]
11671171
Self = $Ty,
11681172
Primitive = signed $Int,
1169-
UnsignedPrimitive = $UInt,
1173+
SignedPrimitive = $Int,
1174+
UnsignedPrimitive = $Uint,
11701175
rot = $rot,
11711176
rot_op = $rot_op,
11721177
rot_result = $rot_result,
@@ -1286,6 +1291,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
12861291
// Associated items for unsigned nonzero types only.
12871292
(
12881293
Primitive = unsigned $Int:ident,
1294+
SignedPrimitive = $Sint:ty,
12891295
UnsignedPrimitive = $Uint:ty,
12901296
) => {
12911297
/// The smallest value that can be represented by this non-zero
@@ -1620,11 +1626,35 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
16201626
// results will be sqrt(1), which is 1, so a result can't be zero.
16211627
unsafe { Self::new_unchecked(result) }
16221628
}
1629+
1630+
/// Returns the bit pattern of `self` reinterpreted as a signed integer of the same size.
1631+
///
1632+
/// # Examples
1633+
///
1634+
/// Basic usage:
1635+
///
1636+
/// ```
1637+
/// #![feature(integer_sign_cast)]
1638+
/// # use std::num::NonZero;
1639+
///
1640+
#[doc = concat!("let n = NonZero::<", stringify!($Int), ">::MAX;")]
1641+
///
1642+
#[doc = concat!("assert_eq!(n.cast_signed(), NonZero::new(-1", stringify!($Sint), ").unwrap());")]
1643+
/// ```
1644+
#[unstable(feature = "integer_sign_cast", issue = "125882")]
1645+
#[must_use = "this returns the result of the operation, \
1646+
without modifying the original"]
1647+
#[inline(always)]
1648+
pub const fn cast_signed(self) -> NonZero<$Sint> {
1649+
// SAFETY: `self.get()` can't be zero
1650+
unsafe { NonZero::new_unchecked(self.get().cast_signed()) }
1651+
}
16231652
};
16241653

16251654
// Associated items for signed nonzero types only.
16261655
(
16271656
Primitive = signed $Int:ident,
1657+
SignedPrimitive = $Sint:ty,
16281658
UnsignedPrimitive = $Uint:ty,
16291659
) => {
16301660
/// The smallest value that can be represented by this non-zero
@@ -2035,12 +2065,37 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
20352065
// SAFETY: negation of nonzero cannot yield zero values.
20362066
unsafe { Self::new_unchecked(result) }
20372067
}
2068+
2069+
/// Returns the bit pattern of `self` reinterpreted as an unsigned integer of the same size.
2070+
///
2071+
/// # Examples
2072+
///
2073+
/// Basic usage:
2074+
///
2075+
/// ```
2076+
/// #![feature(integer_sign_cast)]
2077+
/// # use std::num::NonZero;
2078+
///
2079+
#[doc = concat!("let n = NonZero::new(-1", stringify!($Int), ").unwrap();")]
2080+
///
2081+
#[doc = concat!("assert_eq!(n.cast_unsigned(), NonZero::<", stringify!($Uint), ">::MAX);")]
2082+
/// ```
2083+
#[unstable(feature = "integer_sign_cast", issue = "125882")]
2084+
#[must_use = "this returns the result of the operation, \
2085+
without modifying the original"]
2086+
#[inline(always)]
2087+
pub const fn cast_unsigned(self) -> NonZero<$Uint> {
2088+
// SAFETY: `self.get()` can't be zero
2089+
unsafe { NonZero::new_unchecked(self.get().cast_unsigned()) }
2090+
}
2091+
20382092
};
20392093
}
20402094

20412095
nonzero_integer! {
20422096
Self = NonZeroU8,
20432097
Primitive = unsigned u8,
2098+
SignedPrimitive = i8,
20442099
rot = 2,
20452100
rot_op = "0x82",
20462101
rot_result = "0xa",
@@ -2052,6 +2107,7 @@ nonzero_integer! {
20522107
nonzero_integer! {
20532108
Self = NonZeroU16,
20542109
Primitive = unsigned u16,
2110+
SignedPrimitive = i16,
20552111
rot = 4,
20562112
rot_op = "0xa003",
20572113
rot_result = "0x3a",
@@ -2063,6 +2119,7 @@ nonzero_integer! {
20632119
nonzero_integer! {
20642120
Self = NonZeroU32,
20652121
Primitive = unsigned u32,
2122+
SignedPrimitive = i32,
20662123
rot = 8,
20672124
rot_op = "0x10000b3",
20682125
rot_result = "0xb301",
@@ -2074,6 +2131,7 @@ nonzero_integer! {
20742131
nonzero_integer! {
20752132
Self = NonZeroU64,
20762133
Primitive = unsigned u64,
2134+
SignedPrimitive = i64,
20772135
rot = 12,
20782136
rot_op = "0xaa00000000006e1",
20792137
rot_result = "0x6e10aa",
@@ -2085,6 +2143,7 @@ nonzero_integer! {
20852143
nonzero_integer! {
20862144
Self = NonZeroU128,
20872145
Primitive = unsigned u128,
2146+
SignedPrimitive = i128,
20882147
rot = 16,
20892148
rot_op = "0x13f40000000000000000000000004f76",
20902149
rot_result = "0x4f7613f4",
@@ -2097,6 +2156,7 @@ nonzero_integer! {
20972156
nonzero_integer! {
20982157
Self = NonZeroUsize,
20992158
Primitive = unsigned usize,
2159+
SignedPrimitive = isize,
21002160
rot = 4,
21012161
rot_op = "0xa003",
21022162
rot_result = "0x3a",
@@ -2109,6 +2169,7 @@ nonzero_integer! {
21092169
nonzero_integer! {
21102170
Self = NonZeroUsize,
21112171
Primitive = unsigned usize,
2172+
SignedPrimitive = isize,
21122173
rot = 8,
21132174
rot_op = "0x10000b3",
21142175
rot_result = "0xb301",
@@ -2121,6 +2182,7 @@ nonzero_integer! {
21212182
nonzero_integer! {
21222183
Self = NonZeroUsize,
21232184
Primitive = unsigned usize,
2185+
SignedPrimitive = isize,
21242186
rot = 12,
21252187
rot_op = "0xaa00000000006e1",
21262188
rot_result = "0x6e10aa",

0 commit comments

Comments
 (0)