Skip to content

Commit

Permalink
Add '<[T]>::as_array', '<[T]>::as_mut_array', '<*const [T]>::as_array…
Browse files Browse the repository at this point in the history
…', and '<*mut [T]>::as_mut_array' conversion methods;
  • Loading branch information
bjoernager committed Nov 26, 2024
1 parent dff3e7c commit 4b8ca28
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
20 changes: 4 additions & 16 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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(()))
}
}

Expand All @@ -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(()))
}
}

Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
16 changes: 16 additions & 0 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
16 changes: 16 additions & 0 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4b8ca28

Please sign in to comment.