Skip to content

Commit 10aa3d3

Browse files
authored
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`.
2 parents 536b0c0 + d7afe2a commit 10aa3d3

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

library/core/src/array/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,17 @@ impl<T, const N: usize> [T; N] {
422422
// and we just need to cast it to the correct type.
423423
unsafe { crate::mem::transmute_copy::<_, [U; N]>(&dst) }
424424
}
425+
426+
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
427+
#[unstable(feature = "array_methods", issue = "76118")]
428+
pub fn as_slice(&self) -> &[T] {
429+
self
430+
}
431+
432+
/// Returns a mutable slice containing the entire array. Equivalent to
433+
/// `&mut s[..]`.
434+
#[unstable(feature = "array_methods", issue = "76118")]
435+
pub fn as_mut_slice(&mut self) -> &mut [T] {
436+
self
437+
}
425438
}

library/core/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(alloc_layout_extra)]
22
#![feature(array_chunks)]
3+
#![feature(array_methods)]
34
#![feature(array_map)]
45
#![feature(bool_to_option)]
56
#![feature(bound_cloned)]

library/core/tests/option.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use core::array::FixedSizeArray;
21
use core::clone::Clone;
32
use core::mem;
43
use core::ops::DerefMut;

library/core/tests/result.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use core::array::FixedSizeArray;
21
use core::ops::DerefMut;
32
use core::option::*;
43

0 commit comments

Comments
 (0)