Skip to content

Commit a288bf6

Browse files
committed
Mark {array,slice}::{from_ref,from_mut} as const fn
1 parent 1d6f242 commit a288bf6

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

library/core/src/array/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@ where
8585

8686
/// Converts a reference to `T` into a reference to an array of length 1 (without copying).
8787
#[stable(feature = "array_from_ref", since = "1.53.0")]
88-
pub fn from_ref<T>(s: &T) -> &[T; 1] {
88+
#[rustc_const_unstable(feature = "const_array_from_ref", issue = "none")]
89+
pub const fn from_ref<T>(s: &T) -> &[T; 1] {
8990
// SAFETY: Converting `&T` to `&[T; 1]` is sound.
9091
unsafe { &*(s as *const T).cast::<[T; 1]>() }
9192
}
9293

9394
/// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying).
9495
#[stable(feature = "array_from_ref", since = "1.53.0")]
95-
pub fn from_mut<T>(s: &mut T) -> &mut [T; 1] {
96+
#[rustc_const_unstable(feature = "const_array_from_ref", issue = "none")]
97+
pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] {
9698
// SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound.
9799
unsafe { &mut *(s as *mut T).cast::<[T; 1]>() }
98100
}

library/core/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
#![feature(ptr_metadata)]
136136
#![feature(slice_ptr_get)]
137137
#![feature(variant_count)]
138+
#![feature(const_array_from_ref)]
139+
#![feature(const_slice_from_ref)]
138140
//
139141
// Language features:
140142
#![feature(abi_unadjusted)]

library/core/src/slice/raw.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,14 @@ pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]
138138

139139
/// Converts a reference to T into a slice of length 1 (without copying).
140140
#[stable(feature = "from_ref", since = "1.28.0")]
141-
pub fn from_ref<T>(s: &T) -> &[T] {
141+
#[rustc_const_unstable(feature = "const_slice_from_ref", issue = "none")]
142+
pub const fn from_ref<T>(s: &T) -> &[T] {
142143
array::from_ref(s)
143144
}
144145

145146
/// Converts a reference to T into a slice of length 1 (without copying).
146147
#[stable(feature = "from_ref", since = "1.28.0")]
147-
pub fn from_mut<T>(s: &mut T) -> &mut [T] {
148+
#[rustc_const_unstable(feature = "const_slice_from_ref", issue = "none")]
149+
pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
148150
array::from_mut(s)
149151
}

0 commit comments

Comments
 (0)