Skip to content
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 examples to par_split_mut and par_chunks_mut #530

Merged
merged 1 commit into from
Feb 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ pub trait ParallelSlice<T: Sync> {
///
/// ```
/// use rayon::prelude::*;
/// let windows: Vec<_> = [1, 2, 3, 4].par_chunks(2).collect();
/// assert_eq!(vec![[1, 2], [3, 4]], windows);
/// let chunks: Vec<_> = [1, 2, 3, 4, 5].par_chunks(2).collect();
/// assert_eq!(chunks, vec![&[1, 2][..], &[3, 4], &[5]]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the trailing [..] here, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be forced as a slice instead of array, or else the type of [5] won't match.

I think I could do this like let expected: Vec<&[_]> = ... if that's clearer.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep. I personally think the way it is now is fine to read, I was just nit-picking for the sake of the review.

/// ```
fn par_chunks(&self, chunk_size: usize) -> Chunks<T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
Expand All @@ -101,6 +101,16 @@ pub trait ParallelSliceMut<T: Send> {

/// Returns a parallel iterator over mutable subslices separated by
/// elements that match the separator.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
/// let mut array = [1, 2, 3, 0, 2, 4, 8, 0, 3, 6, 9];
/// array.par_split_mut(|i| *i == 0)
/// .for_each(|slice| slice.reverse());
/// assert_eq!(array, [3, 2, 1, 0, 8, 4, 2, 0, 9, 6, 3]);
/// ```
fn par_split_mut<P>(&mut self, separator: P) -> SplitMut<T, P>
where P: Fn(&T) -> bool + Sync + Send
{
Expand All @@ -112,6 +122,16 @@ pub trait ParallelSliceMut<T: Send> {

/// Returns a parallel iterator over at most `size` elements of
/// `self` at a time. The chunks are mutable and do not overlap.
///
/// # Examples
///
/// ```
/// use rayon::prelude::*;
/// let mut array = [1, 2, 3, 4, 5];
/// array.par_chunks_mut(2)
/// .for_each(|slice| slice.reverse());
/// assert_eq!(array, [2, 1, 4, 3, 5]);
/// ```
fn par_chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T> {
assert!(chunk_size != 0, "chunk_size must not be zero");
ChunksMut {
Expand Down