Skip to content

Commit 09b0bd6

Browse files
Rollup merge of #77074 - lcnr:array-from-ref, r=SimonSapin
add array::from_ref mirrors the methods in `std::slice` with the same name. I guess this method previously didn't exist as there was close to no reason to create an array of size `1`. This will change due to const generics in the near future.
2 parents 862faea + 5b30161 commit 09b0bd6

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

library/core/src/array/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ mod iter;
1919
#[unstable(feature = "array_value_iter", issue = "65798")]
2020
pub use iter::IntoIter;
2121

22+
/// Converts a reference to `T` into a reference to an array of length 1 (without copying).
23+
#[unstable(feature = "array_from_ref", issue = "77101")]
24+
pub fn from_ref<T>(s: &T) -> &[T; 1] {
25+
// SAFETY: Converting `&T` to `&[T; 1]` is sound.
26+
unsafe { &*(s as *const T).cast::<[T; 1]>() }
27+
}
28+
29+
/// Converts a mutable reference to `T` into a mutable reference to an array of length 1 (without copying).
30+
#[unstable(feature = "array_from_ref", issue = "77101")]
31+
pub fn from_mut<T>(s: &mut T) -> &mut [T; 1] {
32+
// SAFETY: Converting `&mut T` to `&mut [T; 1]` is sound.
33+
unsafe { &mut *(s as *mut T).cast::<[T; 1]>() }
34+
}
35+
2236
/// Utility trait implemented only on arrays of fixed size
2337
///
2438
/// This trait can be used to implement other traits on fixed-size arrays

library/core/src/slice/raw.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Free functions to create `&[T]` and `&mut [T]`.
22
3+
use crate::array;
34
use crate::intrinsics::is_aligned_and_not_null;
45
use crate::mem;
56
use crate::ptr;
@@ -140,19 +141,11 @@ pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]
140141
/// Converts a reference to T into a slice of length 1 (without copying).
141142
#[stable(feature = "from_ref", since = "1.28.0")]
142143
pub fn from_ref<T>(s: &T) -> &[T] {
143-
// SAFETY: a reference is guaranteed to be valid for reads. The returned
144-
// reference cannot be mutated as it is an immutable reference.
145-
// `mem::size_of::<T>()` cannot be larger than `isize::MAX`.
146-
// Thus the call to `from_raw_parts` is safe.
147-
unsafe { from_raw_parts(s, 1) }
144+
array::from_ref(s)
148145
}
149146

150147
/// Converts a reference to T into a slice of length 1 (without copying).
151148
#[stable(feature = "from_ref", since = "1.28.0")]
152149
pub fn from_mut<T>(s: &mut T) -> &mut [T] {
153-
// SAFETY: a mutable reference is guaranteed to be valid for writes.
154-
// The reference cannot be accessed by another pointer as it is an mutable reference.
155-
// `mem::size_of::<T>()` cannot be larger than `isize::MAX`.
156-
// Thus the call to `from_raw_parts_mut` is safe.
157-
unsafe { from_raw_parts_mut(s, 1) }
150+
array::from_mut(s)
158151
}

library/core/tests/array.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::array::{FixedSizeArray, IntoIter};
1+
use core::array::{self, FixedSizeArray, IntoIter};
22
use core::convert::TryFrom;
33

44
#[test]
@@ -19,6 +19,21 @@ fn fixed_size_array() {
1919
assert_eq!(FixedSizeArray::as_mut_slice(&mut empty_zero_sized).len(), 0);
2020
}
2121

22+
#[test]
23+
fn array_from_ref() {
24+
let value: String = "Hello World!".into();
25+
let arr: &[String; 1] = array::from_ref(&value);
26+
assert_eq!(&[value.clone()], arr);
27+
}
28+
29+
#[test]
30+
fn array_from_mut() {
31+
let mut value: String = "Hello World".into();
32+
let arr: &mut [String; 1] = array::from_mut(&mut value);
33+
arr[0].push_str("!");
34+
assert_eq!(&value, "Hello World!");
35+
}
36+
2237
#[test]
2338
fn array_try_from() {
2439
macro_rules! test {

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(alloc_layout_extra)]
22
#![feature(array_chunks)]
3+
#![feature(array_from_ref)]
34
#![feature(array_methods)]
45
#![feature(array_map)]
56
#![feature(array_windows)]

0 commit comments

Comments
 (0)