-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add [T]::as_chunks(_mut) #76635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add [T]::as_chunks(_mut) #76635
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -883,6 +883,36 @@ impl<T> [T] { | |
ChunksExactMut::new(self, chunk_size) | ||
} | ||
|
||
/// Splits the slice into a slice of `N`-element arrays, | ||
/// starting at the beginning of the slice, | ||
/// and a remainder slice with length strictly less than `N`. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `N` is 0. This check will most probably get changed to a compile time | ||
/// error before this method gets stabilized. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(slice_as_chunks)] | ||
/// let slice = ['l', 'o', 'r', 'e', 'm']; | ||
/// let (chunks, remainder) = slice.as_chunks(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the example be: I imagine this example with implicit N only works because Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does indeed infer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could either put But in some cases type is not infered which may be confusing, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it does infer Maybe some like these, to emphasize the length math: let slice = ['-'; 100];
let (chunks, remainder) = slice.as_chunks::<20>();
assert_eq!((chunks.len(), remainder.len()), (5, 0));
let (chunks, remainder) = slice.as_chunks::<30>();
assert_eq!((chunks.len(), remainder.len()), (3, 10)); |
||
/// assert_eq!(chunks, &[['l', 'o'], ['r', 'e']]); | ||
/// assert_eq!(remainder, &['m']); | ||
/// ``` | ||
#[unstable(feature = "slice_as_chunks", issue = "74985")] | ||
#[inline] | ||
pub fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) { | ||
assert_ne!(N, 0); | ||
let len = self.len() / N; | ||
let (multiple_of_n, remainder) = self.split_at(len * N); | ||
// SAFETY: We cast a slice of `len * N` elements into | ||
// a slice of `len` many `N` elements chunks. | ||
let array_slice: &[[T; N]] = unsafe { from_raw_parts(multiple_of_n.as_ptr().cast(), len) }; | ||
(array_slice, remainder) | ||
} | ||
|
||
/// Returns an iterator over `N` elements of the slice at a time, starting at the | ||
/// beginning of the slice. | ||
/// | ||
|
@@ -917,6 +947,43 @@ impl<T> [T] { | |
ArrayChunks::new(self) | ||
} | ||
|
||
/// Splits the slice into a slice of `N`-element arrays, | ||
/// starting at the beginning of the slice, | ||
/// and a remainder slice with length strictly less than `N`. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `N` is 0. This check will most probably get changed to a compile time | ||
/// error before this method gets stabilized. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(slice_as_chunks)] | ||
/// let v = &mut [0, 0, 0, 0, 0]; | ||
/// let mut count = 1; | ||
/// | ||
/// let (chunks, remainder) = v.as_chunks_mut(); | ||
/// remainder[0] = 9; | ||
/// for chunk in chunks { | ||
/// *chunk = [count; 2]; | ||
/// count += 1; | ||
/// } | ||
/// assert_eq!(v, &[1, 1, 2, 2, 9]); | ||
/// ``` | ||
#[unstable(feature = "slice_as_chunks", issue = "74985")] | ||
#[inline] | ||
pub fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) { | ||
assert_ne!(N, 0); | ||
let len = self.len() / N; | ||
let (multiple_of_n, remainder) = self.split_at_mut(len * N); | ||
let array_slice: &mut [[T; N]] = | ||
// SAFETY: We cast a slice of `len * N` elements into | ||
// a slice of `len` many `N` elements chunks. | ||
unsafe { from_raw_parts_mut(multiple_of_n.as_mut_ptr().cast(), len) }; | ||
(array_slice, remainder) | ||
} | ||
|
||
/// Returns an iterator over `N` elements of the slice at a time, starting at the | ||
/// beginning of the slice. | ||
/// | ||
|
Uh oh!
There was an error while loading. Please reload this page.