Rollup merge of #76120 - LukasKalbertodt:add-as-slice-method-to-array, r=Mark-Simulacrum
Add `[T; N]::as_[mut_]slice` Part of me trying to populate arrays with a couple of basic useful methods, like slices already have. The ability to add methods to arrays were added in #75212. Tracking issue: #76118 This adds: ```rust impl<T, const N: usize> [T; N] { pub fn as_slice(&self) -> &[T]; pub fn as_mut_slice(&mut self) -> &mut [T]; } ``` These methods are like the ones on `std::array::FixedSizeArray` and in the crate `arraytools`.
This commit is contained in:
commit
10aa3d3f89
@ -422,4 +422,17 @@ impl<T, const N: usize> [T; N] {
|
||||
// and we just need to cast it to the correct type.
|
||||
unsafe { crate::mem::transmute_copy::<_, [U; N]>(&dst) }
|
||||
}
|
||||
|
||||
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
|
||||
#[unstable(feature = "array_methods", issue = "76118")]
|
||||
pub fn as_slice(&self) -> &[T] {
|
||||
self
|
||||
}
|
||||
|
||||
/// Returns a mutable slice containing the entire array. Equivalent to
|
||||
/// `&mut s[..]`.
|
||||
#[unstable(feature = "array_methods", issue = "76118")]
|
||||
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#![feature(alloc_layout_extra)]
|
||||
#![feature(array_chunks)]
|
||||
#![feature(array_methods)]
|
||||
#![feature(array_map)]
|
||||
#![feature(bool_to_option)]
|
||||
#![feature(bound_cloned)]
|
||||
|
@ -1,4 +1,3 @@
|
||||
use core::array::FixedSizeArray;
|
||||
use core::clone::Clone;
|
||||
use core::mem;
|
||||
use core::ops::DerefMut;
|
||||
|
@ -1,4 +1,3 @@
|
||||
use core::array::FixedSizeArray;
|
||||
use core::ops::DerefMut;
|
||||
use core::option::*;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user