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