-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 slice::ExactChunks and ::ExactChunksMut iterators #47126
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
51d546f
Add slice::ExactChunks and ::ExactChunksMut iterators
sdroege 83396fc
Add #![feature(exact_chunks)] to the documentation examples to fix th…
sdroege 802ba9e
Fix assertions in examples of the exact_chunk() documentation
sdroege e51a89a
Fix doctests for slice::exact_chunks() for real
sdroege aa0c08a
Apply review comments from @bluss
sdroege cea36f4
Remove useless assertion
sdroege 6bf1dfd
Implement TrustedRandomAccess for slice::{ExactChunks, ExactChunksMut}
sdroege 8a82e8e
Mention in the exact_chunks docs that this can often be optimized bet…
sdroege baa81dc
Use assert_eq!() instead of assert!(a == b) in slice chunks_mut() uni…
sdroege ed77483
Test the whole chunks instead of just an element in the chunks/chunks…
sdroege 5f4fc82
Add unit tests for exact_chunks/exact_chunks_mut
sdroege File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,8 @@ pub use core::slice::{from_raw_parts, from_raw_parts_mut}; | |
pub use core::slice::{from_ref, from_ref_mut}; | ||
#[unstable(feature = "slice_get_slice", issue = "35729")] | ||
pub use core::slice::SliceIndex; | ||
#[unstable(feature = "exact_chunks", issue = "47115")] | ||
pub use core::slice::{ExactChunks, ExactChunksMut}; | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Basic slice extension methods | ||
|
@@ -611,6 +613,9 @@ impl<T> [T] { | |
/// not divide the length of the slice, then the last chunk will | ||
/// not have length `chunk_size`. | ||
/// | ||
/// See [`exact_chunks`] for a variant of this iterator that returns chunks | ||
/// of always exactly `chunk_size` elements. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `chunk_size` is 0. | ||
|
@@ -631,11 +636,44 @@ impl<T> [T] { | |
core_slice::SliceExt::chunks(self, chunk_size) | ||
} | ||
|
||
/// Returns an iterator over `chunk_size` elements of the slice at a | ||
/// time. The chunks are slices and do not overlap. If `chunk_size` does | ||
/// not divide the length of the slice, then the last up to `chunk_size-1` | ||
/// elements will be omitted. | ||
/// | ||
/// Due to each chunk having exactly `chunk_size` elements, the compiler | ||
/// can often optimize the resulting code better than in the case of | ||
/// [`chunks`]. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `chunk_size` is 0. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(exact_chunks)] | ||
/// | ||
/// let slice = ['l', 'o', 'r', 'e', 'm']; | ||
/// let mut iter = slice.exact_chunks(2); | ||
/// assert_eq!(iter.next().unwrap(), &['l', 'o']); | ||
/// assert_eq!(iter.next().unwrap(), &['r', 'e']); | ||
/// assert!(iter.next().is_none()); | ||
/// ``` | ||
#[unstable(feature = "exact_chunks", issue = "47115")] | ||
#[inline] | ||
pub fn exact_chunks(&self, chunk_size: usize) -> ExactChunks<T> { | ||
core_slice::SliceExt::exact_chunks(self, chunk_size) | ||
} | ||
|
||
/// Returns an iterator over `chunk_size` elements of the slice at a time. | ||
/// The chunks are mutable slices, and do not overlap. If `chunk_size` does | ||
/// not divide the length of the slice, then the last chunk will not | ||
/// have length `chunk_size`. | ||
/// | ||
/// See [`exact_chunks_mut`] for a variant of this iterator that returns chunks | ||
/// of always exactly `chunk_size` elements. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `chunk_size` is 0. | ||
|
@@ -660,6 +698,42 @@ impl<T> [T] { | |
core_slice::SliceExt::chunks_mut(self, chunk_size) | ||
} | ||
|
||
/// Returns an iterator over `chunk_size` elements of the slice at a time. | ||
/// The chunks are mutable slices, and do not overlap. If `chunk_size` does | ||
/// not divide the length of the slice, then the last up to `chunk_size-1` | ||
/// elements will be omitted. | ||
/// | ||
/// | ||
/// Due to each chunk having exactly `chunk_size` elements, the compiler | ||
/// can often optimize the resulting code better than in the case of | ||
/// [`chunks_mut`]. | ||
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. broken too. |
||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `chunk_size` is 0. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(exact_chunks)] | ||
/// | ||
/// let v = &mut [0, 0, 0, 0, 0]; | ||
/// let mut count = 1; | ||
/// | ||
/// for chunk in v.exact_chunks_mut(2) { | ||
/// for elem in chunk.iter_mut() { | ||
/// *elem += count; | ||
/// } | ||
/// count += 1; | ||
/// } | ||
/// assert_eq!(v, &[1, 1, 2, 2, 0]); | ||
/// ``` | ||
#[unstable(feature = "exact_chunks", issue = "47115")] | ||
#[inline] | ||
pub fn exact_chunks_mut(&mut self, chunk_size: usize) -> ExactChunksMut<T> { | ||
core_slice::SliceExt::exact_chunks_mut(self, chunk_size) | ||
} | ||
|
||
/// Divides one slice into two at an index. | ||
/// | ||
/// The first will contain all indices from `[0, mid)` (excluding | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Whoops, it seems this is not referenced, resulting in a broken link.
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.
Indeed, thanks for noticing. I'll submit a PR later, not sure yet why... or why rustdoc does not error out on broken links. Oh well :)
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.
Ah understood why!
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.
why ? :)
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.
See sdroege@1756f68 . It seems like you have to provide "full" paths somewhere in your doc chunk for "shortened" links. rustdoc doesn't seem to check the current scope for things with the same name.
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.
I knew well why, you didn't reference the links in the scope :)
I asked more for:
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.
it should.....