about summary refs log tree commit diff
path: root/library/core
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-06-20 14:07:03 +0200
committerGitHub <noreply@github.com>2024-06-20 14:07:03 +0200
commit7b91d11abb8883d903cbd09b07e022ede814b89d (patch)
tree6d553cba9f4288d817ff81d9a5ebe98f453c50da /library/core
parent9cbfbda1656230ce4ce2addcadef6fda65d11952 (diff)
parent35c65a8c0cf018d472afec5adf9da8242adc8c23 (diff)
downloadrust-7b91d11abb8883d903cbd09b07e022ede814b89d.tar.gz
rust-7b91d11abb8883d903cbd09b07e022ede814b89d.zip
Rollup merge of #126711 - GKFX:const-option-as-slice, r=oli-obk
Make Option::as_[mut_]slice const

These two functions can both be made `const`. I have added them to the `const_option_ext` feature, #91930. I don't believe there is anything blocking stabilization of `as_slice`, but `as_mut_slice` contains mutable references so depends on `const_mut_refs`.
Diffstat (limited to 'library/core')
-rw-r--r--library/core/src/option.rs10
-rw-r--r--library/core/tests/option.rs9
2 files changed, 15 insertions, 4 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index 4d6ba2fa3b3..8ec7716012f 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -797,7 +797,8 @@ impl<T> Option<T> {
     #[inline]
     #[must_use]
     #[stable(feature = "option_as_slice", since = "1.75.0")]
-    pub fn as_slice(&self) -> &[T] {
+    #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
+    pub const fn as_slice(&self) -> &[T] {
         // SAFETY: When the `Option` is `Some`, we're using the actual pointer
         // to the payload, with a length of 1, so this is equivalent to
         // `slice::from_ref`, and thus is safe.
@@ -811,7 +812,7 @@ impl<T> Option<T> {
         unsafe {
             slice::from_raw_parts(
                 (self as *const Self).byte_add(core::mem::offset_of!(Self, Some.0)).cast(),
-                usize::from(self.is_some()),
+                self.is_some() as usize,
             )
         }
     }
@@ -851,7 +852,8 @@ impl<T> Option<T> {
     #[inline]
     #[must_use]
     #[stable(feature = "option_as_slice", since = "1.75.0")]
-    pub fn as_mut_slice(&mut self) -> &mut [T] {
+    #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
+    pub const fn as_mut_slice(&mut self) -> &mut [T] {
         // SAFETY: When the `Option` is `Some`, we're using the actual pointer
         // to the payload, with a length of 1, so this is equivalent to
         // `slice::from_mut`, and thus is safe.
@@ -867,7 +869,7 @@ impl<T> Option<T> {
         unsafe {
             slice::from_raw_parts_mut(
                 (self as *mut Self).byte_add(core::mem::offset_of!(Self, Some.0)).cast(),
-                usize::from(self.is_some()),
+                self.is_some() as usize,
             )
         }
     }
diff --git a/library/core/tests/option.rs b/library/core/tests/option.rs
index b1b9492f182..336a79a02ce 100644
--- a/library/core/tests/option.rs
+++ b/library/core/tests/option.rs
@@ -574,4 +574,13 @@ fn as_slice() {
     assert_eq!(Some(43).as_mut_slice(), &[43]);
     assert_eq!(None::<i32>.as_slice(), &[]);
     assert_eq!(None::<i32>.as_mut_slice(), &[]);
+
+    const A: &[u32] = Some(44).as_slice();
+    const B: &[u32] = None.as_slice();
+    const _: () = {
+        let [45] = Some(45).as_mut_slice() else { panic!() };
+        let []: &[u32] = None.as_mut_slice() else { panic!() };
+    };
+    assert_eq!(A, &[44]);
+    assert_eq!(B, &[]);
 }