@@ -474,6 +474,7 @@ macro_rules! nonzero_integer {
474
474
#[ $stability: meta]
475
475
Self = $Ty: ident,
476
476
Primitive = $signedness: ident $Int: ident,
477
+ SignedPrimitive = $Sint: ty,
477
478
UnsignedPrimitive = $Uint: ty,
478
479
479
480
// Used in doc comments.
@@ -905,6 +906,7 @@ macro_rules! nonzero_integer {
905
906
906
907
nonzero_integer_signedness_dependent_methods! {
907
908
Primitive = $signedness $Int,
909
+ SignedPrimitive = $Sint,
908
910
UnsignedPrimitive = $Uint,
909
911
}
910
912
@@ -1128,6 +1130,7 @@ macro_rules! nonzero_integer {
1128
1130
(
1129
1131
Self = $Ty: ident,
1130
1132
Primitive = unsigned $Int: ident,
1133
+ SignedPrimitive = $Sint: ident,
1131
1134
rot = $rot: literal,
1132
1135
rot_op = $rot_op: literal,
1133
1136
rot_result = $rot_result: literal,
@@ -1140,6 +1143,7 @@ macro_rules! nonzero_integer {
1140
1143
#[ stable( feature = "nonzero" , since = "1.28.0" ) ]
1141
1144
Self = $Ty,
1142
1145
Primitive = unsigned $Int,
1146
+ SignedPrimitive = $Sint,
1143
1147
UnsignedPrimitive = $Int,
1144
1148
rot = $rot,
1145
1149
rot_op = $rot_op,
@@ -1154,7 +1158,7 @@ macro_rules! nonzero_integer {
1154
1158
(
1155
1159
Self = $Ty: ident,
1156
1160
Primitive = signed $Int: ident,
1157
- UnsignedPrimitive = $UInt : ident,
1161
+ UnsignedPrimitive = $Uint : ident,
1158
1162
rot = $rot: literal,
1159
1163
rot_op = $rot_op: literal,
1160
1164
rot_result = $rot_result: literal,
@@ -1166,7 +1170,8 @@ macro_rules! nonzero_integer {
1166
1170
#[ stable( feature = "signed_nonzero" , since = "1.34.0" ) ]
1167
1171
Self = $Ty,
1168
1172
Primitive = signed $Int,
1169
- UnsignedPrimitive = $UInt,
1173
+ SignedPrimitive = $Int,
1174
+ UnsignedPrimitive = $Uint,
1170
1175
rot = $rot,
1171
1176
rot_op = $rot_op,
1172
1177
rot_result = $rot_result,
@@ -1286,6 +1291,7 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
1286
1291
// Associated items for unsigned nonzero types only.
1287
1292
(
1288
1293
Primitive = unsigned $Int: ident,
1294
+ SignedPrimitive = $Sint: ty,
1289
1295
UnsignedPrimitive = $Uint: ty,
1290
1296
) => {
1291
1297
/// The smallest value that can be represented by this non-zero
@@ -1620,11 +1626,35 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
1620
1626
// results will be sqrt(1), which is 1, so a result can't be zero.
1621
1627
unsafe { Self :: new_unchecked( result) }
1622
1628
}
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
+ }
1623
1652
} ;
1624
1653
1625
1654
// Associated items for signed nonzero types only.
1626
1655
(
1627
1656
Primitive = signed $Int: ident,
1657
+ SignedPrimitive = $Sint: ty,
1628
1658
UnsignedPrimitive = $Uint: ty,
1629
1659
) => {
1630
1660
/// The smallest value that can be represented by this non-zero
@@ -2035,12 +2065,37 @@ macro_rules! nonzero_integer_signedness_dependent_methods {
2035
2065
// SAFETY: negation of nonzero cannot yield zero values.
2036
2066
unsafe { Self :: new_unchecked( result) }
2037
2067
}
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
+
2038
2092
} ;
2039
2093
}
2040
2094
2041
2095
nonzero_integer ! {
2042
2096
Self = NonZeroU8 ,
2043
2097
Primitive = unsigned u8 ,
2098
+ SignedPrimitive = i8 ,
2044
2099
rot = 2 ,
2045
2100
rot_op = "0x82" ,
2046
2101
rot_result = "0xa" ,
@@ -2052,6 +2107,7 @@ nonzero_integer! {
2052
2107
nonzero_integer ! {
2053
2108
Self = NonZeroU16 ,
2054
2109
Primitive = unsigned u16 ,
2110
+ SignedPrimitive = i16 ,
2055
2111
rot = 4 ,
2056
2112
rot_op = "0xa003" ,
2057
2113
rot_result = "0x3a" ,
@@ -2063,6 +2119,7 @@ nonzero_integer! {
2063
2119
nonzero_integer ! {
2064
2120
Self = NonZeroU32 ,
2065
2121
Primitive = unsigned u32 ,
2122
+ SignedPrimitive = i32 ,
2066
2123
rot = 8 ,
2067
2124
rot_op = "0x10000b3" ,
2068
2125
rot_result = "0xb301" ,
@@ -2074,6 +2131,7 @@ nonzero_integer! {
2074
2131
nonzero_integer ! {
2075
2132
Self = NonZeroU64 ,
2076
2133
Primitive = unsigned u64 ,
2134
+ SignedPrimitive = i64 ,
2077
2135
rot = 12 ,
2078
2136
rot_op = "0xaa00000000006e1" ,
2079
2137
rot_result = "0x6e10aa" ,
@@ -2085,6 +2143,7 @@ nonzero_integer! {
2085
2143
nonzero_integer ! {
2086
2144
Self = NonZeroU128 ,
2087
2145
Primitive = unsigned u128 ,
2146
+ SignedPrimitive = i128 ,
2088
2147
rot = 16 ,
2089
2148
rot_op = "0x13f40000000000000000000000004f76" ,
2090
2149
rot_result = "0x4f7613f4" ,
@@ -2097,6 +2156,7 @@ nonzero_integer! {
2097
2156
nonzero_integer ! {
2098
2157
Self = NonZeroUsize ,
2099
2158
Primitive = unsigned usize ,
2159
+ SignedPrimitive = isize ,
2100
2160
rot = 4 ,
2101
2161
rot_op = "0xa003" ,
2102
2162
rot_result = "0x3a" ,
@@ -2109,6 +2169,7 @@ nonzero_integer! {
2109
2169
nonzero_integer ! {
2110
2170
Self = NonZeroUsize ,
2111
2171
Primitive = unsigned usize ,
2172
+ SignedPrimitive = isize ,
2112
2173
rot = 8 ,
2113
2174
rot_op = "0x10000b3" ,
2114
2175
rot_result = "0xb301" ,
@@ -2121,6 +2182,7 @@ nonzero_integer! {
2121
2182
nonzero_integer ! {
2122
2183
Self = NonZeroUsize ,
2123
2184
Primitive = unsigned usize ,
2185
+ SignedPrimitive = isize ,
2124
2186
rot = 12 ,
2125
2187
rot_op = "0xaa00000000006e1" ,
2126
2188
rot_result = "0x6e10aa" ,
0 commit comments