Skip to content
Open
Show file tree
Hide file tree
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
77 changes: 77 additions & 0 deletions src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,28 @@ pub struct Iter<'data, T: Sync> {
slice: &'data [T],
}

impl<'data, T: Sync> Iter<'data, T> {
/// Views the underlying data as a subslice of the original data.
///
/// # Examples
///
/// ```
/// # use rayon::prelude::*;
/// let slice = &['a', 'b', 'c'];
/// let iter = slice[1..3].par_iter();
/// assert_eq!(iter.as_slice(), &['b', 'c']);
/// ```
pub fn as_slice(&self) -> &'data [T] {
self.slice
}
}

impl<'data, T: Sync> AsRef<[T]> for Iter<'data, T> {
fn as_ref(&self) -> &[T] {
self.as_slice()
}
}

impl<'data, T: Sync> Clone for Iter<'data, T> {
fn clone(&self) -> Self {
Iter { ..*self }
Expand Down Expand Up @@ -932,6 +954,61 @@ pub struct IterMut<'data, T: Send> {
slice: &'data mut [T],
}

impl<'data, T: Send> IterMut<'data, T> {
/// Returns the remaining items in this iterator as a mutable slice with the original lifetime.
///
/// To return with the lifetime of the original slice, the iterator must be consumed.
///
/// # Examples
///
/// ```
/// # use rayon::prelude::*;
/// let mut slice = &mut [1, 2, 3];
/// let mut iter = slice.par_iter_mut();
/// let mut slice_mut = iter.into_slice();
/// slice_mut[0] = 7;
/// assert_eq!(slice_mut, &[7, 2, 3]);
/// ```
pub fn into_slice(self) -> &'data mut [T] {
self.slice
}

/// Views the underlying data as a subslice of the original data.
///
/// # Examples
///
/// ```
/// # use rayon::prelude::*;
/// let mut slice = &[1, 2, 3];
/// let mut iter = slice[1..].par_iter();
/// assert_eq!(iter.as_slice(), &[2, 3]);
/// ```
pub fn as_slice(&self) -> &[T] {
self.slice
}

/// Views the underlying data as a mutable subslice of the original data.
///
/// # Examples
///
/// ```
/// # use rayon::prelude::*;
/// let mut slice = &mut [1, 2, 3];
/// let mut iter = slice[1..].par_iter_mut();
/// iter.as_mut_slice()[0] = 7;
/// assert_eq!(iter.as_mut_slice(), &[7, 3]);
/// ```
pub fn as_mut_slice(&mut self) -> &mut [T] {
self.slice
}
}

impl<'data, T: Send> AsRef<[T]> for IterMut<'data, T> {
fn as_ref(&self) -> &[T] {
self.as_slice()
}
}

impl<'data, T: Send + 'data> ParallelIterator for IterMut<'data, T> {
type Item = &'data mut T;

Expand Down
74 changes: 74 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,43 @@ pub struct IntoIter<T: Send> {
vec: Vec<T>,
}

impl<T: Send> IntoIter<T> {
/// Returns the remaining items of this iterator as a slice.
///
/// # Examples
///
/// ```
/// # use rayon::prelude::*;
/// let mut vec = vec!['a', 'b', 'c'];
/// let drain = vec.par_drain(0..2);
/// assert_eq!(drain.as_slice(), &['a', 'b']);
/// ```
pub fn as_slice(&self) -> &[T] {
&self.vec
}

/// Returns the remaining items of this iterator as a mutable slice.
///
/// # Examples
///
/// ```
/// # use rayon::prelude::*;
/// let mut vec = vec!['a', 'b', 'c'];
/// let mut drain = vec.par_drain(0..2);
/// *drain.as_mut_slice().last_mut().unwrap() = 'd';
/// assert_eq!(drain.as_slice(), &['a', 'd']);
/// ```
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.vec
}
}

impl<T: Send> AsRef<[T]> for IntoIter<T> {
fn as_ref(&self) -> &[T] {
self.as_slice()
}
}

impl<T: Send> IntoParallelIterator for Vec<T> {
type Item = T;
type Iter = IntoIter<T>;
Expand Down Expand Up @@ -105,6 +142,43 @@ pub struct Drain<'data, T: Send> {
orig_len: usize,
}

impl<'data, T: Send> Drain<'data, T> {
/// Returns the remaining items of this iterator as a slice.
///
/// # Examples
///
/// ```
/// # use rayon::prelude::*;
/// let mut vec = vec!['a', 'b', 'c'];
/// let drain = vec.par_drain(0..2);
/// assert_eq!(drain.as_slice(), &['a', 'b']);
/// ```
pub fn as_slice(&self) -> &[T] {
&self.vec[self.range.clone()]
}

/// Returns the remaining items of this iterator as a mutable slice.
///
/// # Examples
///
/// ```
/// # use rayon::prelude::*;
/// let mut vec = vec!['a', 'b', 'c'];
/// let mut drain = vec.par_drain(0..2);
/// *drain.as_mut_slice().last_mut().unwrap() = 'd';
/// assert_eq!(drain.as_slice(), &['a', 'd']);
/// ```
pub fn as_mut_slice(&mut self) -> &mut [T] {
&mut self.vec[self.range.clone()]
}
}

impl<'data, T: Send> AsRef<[T]> for Drain<'data, T> {
fn as_ref(&self) -> &[T] {
self.as_slice()
}
}

impl<'data, T: Send> ParallelIterator for Drain<'data, T> {
type Item = T;

Expand Down