1
Fork 0

Add '<[T]>::as_array', '<[T]>::as_mut_array', '<*const [T]>::as_array', and '<*mut [T]>::as_mut_array' conversion methods;

This commit is contained in:
Gabriel Bjørnager Jensen 2024-11-26 21:49:28 +01:00
parent dff3e7ccd4
commit 4b8ca28a1e
5 changed files with 75 additions and 16 deletions

View file

@ -214,8 +214,8 @@ impl<T, const N: usize> BorrowMut<[T]> for [T; N] {
} }
} }
/// Tries to create an array `[T; N]` by copying from a slice `&[T]`. Succeeds if /// Tries to create an array `[T; N]` by copying from a slice `&[T]`.
/// `slice.len() == N`. /// Succeeds if `slice.len() == N`.
/// ///
/// ``` /// ```
/// let bytes: [u8; 3] = [1, 0, 2]; /// let bytes: [u8; 3] = [1, 0, 2];
@ -282,13 +282,7 @@ impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N] {
#[inline] #[inline]
fn try_from(slice: &'a [T]) -> Result<&'a [T; N], TryFromSliceError> { fn try_from(slice: &'a [T]) -> Result<&'a [T; N], TryFromSliceError> {
if slice.len() == N { slice.as_array().ok_or(TryFromSliceError(()))
let ptr = slice.as_ptr() as *const [T; N];
// SAFETY: ok because we just checked that the length fits
unsafe { Ok(&*ptr) }
} else {
Err(TryFromSliceError(()))
}
} }
} }
@ -310,13 +304,7 @@ impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {
#[inline] #[inline]
fn try_from(slice: &'a mut [T]) -> Result<&'a mut [T; N], TryFromSliceError> { fn try_from(slice: &'a mut [T]) -> Result<&'a mut [T; N], TryFromSliceError> {
if slice.len() == N { slice.as_mut_array().ok_or(TryFromSliceError(()))
let ptr = slice.as_mut_ptr() as *mut [T; N];
// SAFETY: ok because we just checked that the length fits
unsafe { Ok(&mut *ptr) }
} else {
Err(TryFromSliceError(()))
}
} }
} }

View file

@ -144,6 +144,7 @@
#![feature(ptr_alignment_type)] #![feature(ptr_alignment_type)]
#![feature(ptr_metadata)] #![feature(ptr_metadata)]
#![feature(set_ptr_value)] #![feature(set_ptr_value)]
#![feature(slice_as_array)]
#![feature(slice_as_chunks)] #![feature(slice_as_chunks)]
#![feature(slice_ptr_get)] #![feature(slice_ptr_get)]
#![feature(str_internals)] #![feature(str_internals)]

View file

@ -1526,6 +1526,22 @@ impl<T> *const [T] {
self as *const T self as *const T
} }
/// Gets a raw pointer to the underlying array.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
#[unstable(feature = "slice_as_array", issue = "133508")]
#[rustc_const_unstable(feature = "slice_as_array", issue = "133508")]
#[inline]
#[must_use]
pub const fn as_array<const N: usize>(self) -> Option<*const [T; N]> {
if self.len() == N {
let me = self.as_ptr() as *const [T; N];
Some(me)
} else {
None
}
}
/// Returns a raw pointer to an element or subslice, without doing bounds /// Returns a raw pointer to an element or subslice, without doing bounds
/// checking. /// checking.
/// ///

View file

@ -1760,6 +1760,22 @@ impl<T> *mut [T] {
self.len() == 0 self.len() == 0
} }
/// Gets a raw, mutable pointer to the underlying array.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
#[unstable(feature = "slice_as_array", issue = "133508")]
#[rustc_const_unstable(feature = "slice_as_array", issue = "133508")]
#[inline]
#[must_use]
pub const fn as_mut_array<const N: usize>(self) -> Option<*mut [T; N]> {
if self.len() == N {
let me = self.as_mut_ptr() as *mut [T; N];
Some(me)
} else {
None
}
}
/// Divides one mutable raw slice into two at an index. /// Divides one mutable raw slice into two at an index.
/// ///
/// The first will contain all indices from `[0, mid)` (excluding /// The first will contain all indices from `[0, mid)` (excluding

View file

@ -855,6 +855,44 @@ impl<T> [T] {
start..end start..end
} }
/// Gets a reference to the underlying array.
///
/// If `N` is not exactly equal to slice's the length of `self`, then this method returns `None`.
#[unstable(feature = "slice_as_array", issue = "133508")]
#[rustc_const_unstable(feature = "slice_as_array", issue = "133508")]
#[inline]
#[must_use]
pub const fn as_array<const N: usize>(&self) -> Option<&[T; N]> {
if self.len() == N {
let ptr = self.as_ptr() as *const [T; N];
// SAFETY: The underlying array of a slice can be reinterpreted as an actual array `[T; N]` if `N` is not greater than the slice's length.
let me = unsafe { &*ptr };
Some(me)
} else {
None
}
}
/// Gets a mutable reference to the slice's underlying array.
///
/// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
#[unstable(feature = "slice_as_array", issue = "133508")]
#[rustc_const_unstable(feature = "slice_as_array", issue = "133508")]
#[inline]
#[must_use]
pub const fn as_mut_array<const N: usize>(&mut self) -> Option<&mut [T; N]> {
if self.len() == N {
let ptr = self.as_mut_ptr() as *mut [T; N];
// SAFETY: The underlying array of a slice can be reinterpreted as an actual array `[T; N]` if `N` is not greater than the slice's length.
let me = unsafe { &mut *ptr };
Some(me)
} else {
None
}
}
/// Swaps two elements in the slice. /// Swaps two elements in the slice.
/// ///
/// If `a` equals to `b`, it's guaranteed that elements won't change value. /// If `a` equals to `b`, it's guaranteed that elements won't change value.