mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-09 06:16:06 +00:00
Rollup merge of #133512 - bjoernager:slice-as-array, r=Amanieu
Add `as_array` and `as_mut_array` conversion methods to slices. Tracking issue: #133508 This PR unstably implements the `as_array` and `as_mut_array` converters to `[T]`, `*const [T]`, and `*mut [T]`.
This commit is contained in:
commit
b1c33f4f09
@ -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
|
||||
/// `slice.len() == N`.
|
||||
/// Tries to create an array `[T; N]` by copying from a slice `&[T]`.
|
||||
/// Succeeds if `slice.len() == N`.
|
||||
///
|
||||
/// ```
|
||||
/// 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]
|
||||
fn try_from(slice: &'a [T]) -> Result<&'a [T; N], TryFromSliceError> {
|
||||
if slice.len() == N {
|
||||
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(()))
|
||||
}
|
||||
slice.as_array().ok_or(TryFromSliceError(()))
|
||||
}
|
||||
}
|
||||
|
||||
@ -310,13 +304,7 @@ impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N] {
|
||||
|
||||
#[inline]
|
||||
fn try_from(slice: &'a mut [T]) -> Result<&'a mut [T; N], TryFromSliceError> {
|
||||
if slice.len() == N {
|
||||
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(()))
|
||||
}
|
||||
slice.as_mut_array().ok_or(TryFromSliceError(()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -144,6 +144,7 @@
|
||||
#![feature(ptr_alignment_type)]
|
||||
#![feature(ptr_metadata)]
|
||||
#![feature(set_ptr_value)]
|
||||
#![feature(slice_as_array)]
|
||||
#![feature(slice_as_chunks)]
|
||||
#![feature(slice_ptr_get)]
|
||||
#![feature(str_internals)]
|
||||
|
@ -1526,6 +1526,22 @@ impl<T> *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
|
||||
/// checking.
|
||||
///
|
||||
|
@ -1760,6 +1760,22 @@ impl<T> *mut [T] {
|
||||
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.
|
||||
///
|
||||
/// The first will contain all indices from `[0, mid)` (excluding
|
||||
|
@ -855,6 +855,44 @@ impl<T> [T] {
|
||||
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.
|
||||
///
|
||||
/// If `a` equals to `b`, it's guaranteed that elements won't change value.
|
||||
|
Loading…
Reference in New Issue
Block a user