Skip to content

Commit

Permalink
Rollup merge of rust-lang#76120 - LukasKalbertodt:add-as-slice-method…
Browse files Browse the repository at this point in the history
…-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 rust-lang#75212.  Tracking issue: rust-lang#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`.
  • Loading branch information
Dylan-DPC authored Sep 3, 2020
2 parents 536b0c0 + d7afe2a commit 10aa3d3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
13 changes: 13 additions & 0 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(alloc_layout_extra)]
#![feature(array_chunks)]
#![feature(array_methods)]
#![feature(array_map)]
#![feature(bool_to_option)]
#![feature(bound_cloned)]
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/option.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::array::FixedSizeArray;
use core::clone::Clone;
use core::mem;
use core::ops::DerefMut;
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/result.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use core::array::FixedSizeArray;
use core::ops::DerefMut;
use core::option::*;

Expand Down

0 comments on commit 10aa3d3

Please sign in to comment.