about summary refs log tree commit diff
path: root/library/core/src/array
diff options
context:
space:
mode:
authorBastian Kauschke <bastian_kauschke@hotmail.de>2020-09-22 21:35:43 +0200
committerBastian Kauschke <bastian_kauschke@hotmail.de>2020-09-22 21:35:43 +0200
commit179f63dafcf9463b48d82e8698a844e18bf94370 (patch)
treef044122cd793a312a97503c64b39cd95e13bbd95 /library/core/src/array
parente0bc267512fc0cb49c86978192857e8187017f0b (diff)
downloadrust-179f63dafcf9463b48d82e8698a844e18bf94370.tar.gz
rust-179f63dafcf9463b48d82e8698a844e18bf94370.zip
add array from_ref
Diffstat (limited to 'library/core/src/array')
-rw-r--r--library/core/src/array/mod.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs
index c1d3aca6fdd..5730074bf27 100644
--- a/library/core/src/array/mod.rs
+++ b/library/core/src/array/mod.rs
@@ -19,6 +19,20 @@ mod iter;
 #[unstable(feature = "array_value_iter", issue = "65798")]
 pub use iter::IntoIter;
 
+/// Converts a reference to `T` into a reference to an array of length 1 (without copying).
+#[unstable(feature = "array_from_ref", issue = "none")]
+pub 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).
+#[unstable(feature = "array_from_ref", issue = "none")]
+pub 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]>() }
+}
+
 /// Utility trait implemented only on arrays of fixed size
 ///
 /// This trait can be used to implement other traits on fixed-size arrays