about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-02-17 02:30:53 +0000
committerbors <bors@rust-lang.org>2024-02-17 02:30:53 +0000
commit4316d0c6252cb1f833e582dfa68adb98efd5ddfb (patch)
treed97892a57b48976ba59c9ebf3652ac29ecf1ab22
parent405b22f1a3a39eef5f4698b3662097c8a4f6f5d0 (diff)
parent24e2cf01d36039c7e808a1b95688eb25f606cb2a (diff)
downloadrust-4316d0c6252cb1f833e582dfa68adb98efd5ddfb.tar.gz
rust-4316d0c6252cb1f833e582dfa68adb98efd5ddfb.zip
Auto merge of #120563 - reitermarkus:generic-nonzero-get, r=dtolnay
Make `NonZero::get` generic.

Tracking issue: https://github.com/rust-lang/rust/issues/120257

Depends on https://github.com/rust-lang/rust/pull/120521.

r? `@dtolnay`
-rw-r--r--library/core/src/num/nonzero.rs41
1 files changed, 21 insertions, 20 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs
index 6410ff5f828..fe287326175 100644
--- a/library/core/src/num/nonzero.rs
+++ b/library/core/src/num/nonzero.rs
@@ -160,6 +160,27 @@ where
             }
         }
     }
+
+    /// Returns the contained value as a primitive type.
+    #[stable(feature = "nonzero", since = "1.28.0")]
+    #[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
+    #[inline]
+    pub const fn get(self) -> T {
+        // FIXME: This can be changed to simply `self.0` once LLVM supports `!range` metadata
+        // for function arguments: https://github.com/llvm/llvm-project/issues/76628
+        //
+        // Rustc can set range metadata only if it loads `self` from
+        // memory somewhere. If the value of `self` was from by-value argument
+        // of some not-inlined function, LLVM don't have range metadata
+        // to understand that the value cannot be zero.
+        match Self::new(self.0) {
+            Some(Self(n)) => n,
+            None => {
+                // SAFETY: `NonZero` is guaranteed to only contain non-zero values, so this is unreachable.
+                unsafe { intrinsics::unreachable() }
+            }
+        }
+    }
 }
 
 macro_rules! impl_nonzero_fmt {
@@ -221,26 +242,6 @@ macro_rules! nonzero_integer {
         pub type $Ty = NonZero<$Int>;
 
         impl $Ty {
-            /// Returns the value as a primitive type.
-            #[$stability]
-            #[inline]
-            #[rustc_const_stable(feature = "const_nonzero_get", since = "1.34.0")]
-            pub const fn get(self) -> $Int {
-                // FIXME: Remove this after LLVM supports `!range` metadata for function
-                // arguments https://github.com/llvm/llvm-project/issues/76628
-                //
-                // Rustc can set range metadata only if it loads `self` from
-                // memory somewhere. If the value of `self` was from by-value argument
-                // of some not-inlined function, LLVM don't have range metadata
-                // to understand that the value cannot be zero.
-
-                // SAFETY: It is an invariant of this type.
-                unsafe {
-                    intrinsics::assume(self.0 != 0);
-                }
-                self.0
-            }
-
             /// The size of this non-zero integer type in bits.
             ///
             #[doc = concat!("This value is equal to [`", stringify!($Int), "::BITS`].")]