@@ -164,6 +164,27 @@ where
164
164
}
165
165
}
166
166
}
167
+
168
+ /// Returns the contained value as a primitive type.
169
+ #[ stable( feature = "nonzero" , since = "1.28.0" ) ]
170
+ #[ rustc_const_stable( feature = "const_nonzero_get" , since = "1.34.0" ) ]
171
+ #[ inline]
172
+ pub const fn get ( self ) -> T {
173
+ // FIXME: This can be changed to simply `self.0` once LLVM supports `!range` metadata
174
+ // for function arguments: https://github.com/llvm/llvm-project/issues/76628
175
+ //
176
+ // Rustc can set range metadata only if it loads `self` from
177
+ // memory somewhere. If the value of `self` was from by-value argument
178
+ // of some not-inlined function, LLVM don't have range metadata
179
+ // to understand that the value cannot be zero.
180
+ match Self :: new ( self . 0 ) {
181
+ Some ( Self ( n) ) => n,
182
+ None => {
183
+ // SAFETY: `NonZero` is guaranteed to only contain non-zero values, so this is unreachable.
184
+ unsafe { intrinsics:: unreachable ( ) }
185
+ }
186
+ }
187
+ }
167
188
}
168
189
169
190
macro_rules! impl_nonzero_fmt {
@@ -225,26 +246,6 @@ macro_rules! nonzero_integer {
225
246
pub type $Ty = NonZero <$Int>;
226
247
227
248
impl $Ty {
228
- /// Returns the value as a primitive type.
229
- #[ $stability]
230
- #[ inline]
231
- #[ rustc_const_stable( feature = "const_nonzero_get" , since = "1.34.0" ) ]
232
- pub const fn get( self ) -> $Int {
233
- // FIXME: Remove this after LLVM supports `!range` metadata for function
234
- // arguments https://github.com/llvm/llvm-project/issues/76628
235
- //
236
- // Rustc can set range metadata only if it loads `self` from
237
- // memory somewhere. If the value of `self` was from by-value argument
238
- // of some not-inlined function, LLVM don't have range metadata
239
- // to understand that the value cannot be zero.
240
-
241
- // SAFETY: It is an invariant of this type.
242
- unsafe {
243
- intrinsics:: assume( self . 0 != 0 ) ;
244
- }
245
- self . 0
246
- }
247
-
248
249
/// The size of this non-zero integer type in bits.
249
250
///
250
251
#[ doc = concat!( "This value is equal to [`" , stringify!( $Int) , "::BITS`]." ) ]
0 commit comments