-
Couldn't load subscription status.
- Fork 13.9k
Add Vec::into_chunks
#142138
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 Vec::into_chunks
#142138
Changes from 3 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 |
|---|---|---|
|
|
@@ -2999,6 +2999,61 @@ impl<T, A: Allocator> Vec<T, A> { | |
| (initialized, spare, &mut self.len) | ||
| } | ||
| } | ||
|
|
||
| /// Groups every `N` elements in the `Vec<T>` into chunks to produce a `Vec<[T; N]>`, dropping | ||
| /// elements in the remainder. `N` must be greater than zero. | ||
| /// | ||
| /// If the capacity is not a multiple of the chunk size, the buffer will shrink down to the | ||
| /// nearest multiple with a reallocation or deallocation. | ||
| /// | ||
| /// This function can be used to reverse [`Vec::into_flattened`]. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// #![feature(vec_into_chunks)] | ||
| /// | ||
| /// let vec = vec![0, 1, 2, 3, 4, 5, 6, 7]; | ||
| /// assert_eq!(vec.into_chunks::<3>(), [[0, 1, 2], [3, 4, 5]]); | ||
| /// | ||
| /// let vec = vec![0, 1, 2, 3]; | ||
| /// let chunks: Vec<[u8; 10]> = vec.into_chunks(); | ||
| /// assert!(chunks.is_empty()); | ||
| /// | ||
| /// let flat = vec![0; 8 * 8 * 8]; | ||
| /// let reshaped: Vec<[[[u8; 8]; 8]; 8]> = flat.into_chunks().into_chunks().into_chunks(); | ||
| /// assert_eq!(reshaped.len(), 1); | ||
| /// ``` | ||
| #[cfg(not(no_global_oom_handling))] | ||
| #[unstable(feature = "vec_into_chunks", issue = "142137")] | ||
| pub fn into_chunks<const N: usize>(mut self) -> Vec<[T; N], A> { | ||
| const { | ||
| assert!(N != 0, "chunk size should be greater than zero"); | ||
| } | ||
|
|
||
| let (len, cap) = (self.len(), self.capacity()); | ||
|
|
||
| let len_remainder = len % N; | ||
| if len_remainder != 0 { | ||
| self.truncate(len - len_remainder); | ||
| } | ||
|
|
||
| let cap_remainder = cap % N; | ||
| if !T::IS_ZST && cap_remainder != 0 { | ||
| self.buf.shrink_to_fit(cap - cap_remainder); | ||
| } | ||
|
Comment on lines
+3036
to
+3044
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. I was debating whether the |
||
|
|
||
| let (ptr, _, _, alloc) = self.into_raw_parts_with_alloc(); | ||
|
|
||
| // SAFETY: | ||
| // - `ptr` and `alloc` were just returned from `self.into_raw_parts_with_alloc()` | ||
| // - `[T; N]` has the same alignment as `T` | ||
| // - `size_of::<[T; N]>() * cap / N == size_of::<T>() * cap` | ||
| // - `len / N <= cap / N` because `len <= cap` | ||
| // - the allocated memory consists of `len / N` valid values of type `[T; N]` | ||
| // - `cap / N` fits the size of the allocated memory after shrinking | ||
| unsafe { Vec::from_raw_parts_in(ptr.cast(), len / N, cap / N, alloc) } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Clone, A: Allocator> Vec<T, A> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
language nit: "must be" instead of "should be", as this is not an advisory suggestion.