diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2021-10-24 15:48:44 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-24 15:48:44 +0200 |
| commit | c16ee19dd4d81d3b1150d67cfb4ea84c826f4c36 (patch) | |
| tree | 1e6fc487449390e55fda1753491e6bf6eefd2ba4 /library/core/src/array | |
| parent | b8376050127b3d1806ae88e6916ddaf330ca699c (diff) | |
| parent | 5f390cfb722cf95b0df81f9563bf97b1663cff9e (diff) | |
| download | rust-c16ee19dd4d81d3b1150d67cfb4ea84c826f4c36.tar.gz rust-c16ee19dd4d81d3b1150d67cfb4ea84c826f4c36.zip | |
Rollup merge of #90162 - WaffleLapkin:const_array_slice_from_ref_mut, r=oli-obk
Mark `{array, slice}::{from_ref, from_mut}` as const fn
This PR marks the following APIs as `const`:
```rust
// core::array
pub const fn from_ref<T>(s: &T) -> &[T; 1];
pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1];
// core::slice
pub const fn from_ref<T>(s: &T) -> &[T];
pub const fn from_mut<T>(s: &mut T) -> &mut [T];
```
Note that `from_ref` methods require `const_raw_ptr_deref` feature (which seems totally fine, since it's being stabilized, see #89551), `from_mut` methods require `const_mut_refs` (which seems fine too since this PR marks `from_mut` functions as const unstable).
r? ````@oli-obk````
Diffstat (limited to 'library/core/src/array')
| -rw-r--r-- | library/core/src/array/mod.rs | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 73340fda2cb..811850af367 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -85,14 +85,16 @@ where /// Converts a reference to `T` into a reference to an array of length 1 (without copying). #[stable(feature = "array_from_ref", since = "1.53.0")] -pub fn from_ref<T>(s: &T) -> &[T; 1] { +#[rustc_const_unstable(feature = "const_array_from_ref", issue = "90206")] +pub const fn from_ref<T>(s: &T) -> &[T; 1] { // SAFETY: Converting `&T` to `&[T; 1]` is sound. unsafe { &*(s as *const T).cast::<[T; 1]>() } } /// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying). #[stable(feature = "array_from_ref", since = "1.53.0")] -pub fn from_mut<T>(s: &mut T) -> &mut [T; 1] { +#[rustc_const_unstable(feature = "const_array_from_ref", issue = "90206")] +pub const fn from_mut<T>(s: &mut T) -> &mut [T; 1] { // SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound. unsafe { &mut *(s as *mut T).cast::<[T; 1]>() } } |
