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 as_slice() to slice::IterMut and vec::Drain #58924

Merged
merged 4 commits into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,25 @@ impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
}
}

impl<'a, T> Drain<'a, T> {
/// Returns the remaining items of this iterator as a slice.
///
/// # Examples
///
/// ```
/// # #![feature(vec_drain_as_slice)]
/// let mut vec = vec!['a', 'b', 'c'];
/// let mut drain = vec.drain(..);
/// assert_eq!(drain.as_slice(), &['a', 'b', 'c']);
/// let _ = drain.next().unwrap();
/// assert_eq!(drain.as_slice(), &['b', 'c']);
/// ```
#[unstable(feature = "vec_drain_as_slice", reason = "recently added", issue = "0")]
pub fn as_slice(&self) -> &[T] {
self.iter.as_slice()
}
}

#[stable(feature = "drain", since = "1.6.0")]
unsafe impl<T: Sync> Sync for Drain<'_, T> {}
#[stable(feature = "drain", since = "1.6.0")]
Expand Down
32 changes: 32 additions & 0 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3288,6 +3288,38 @@ impl<'a, T> IterMut<'a, T> {
pub fn into_slice(self) -> &'a mut [T] {
unsafe { from_raw_parts_mut(self.ptr, len!(self)) }
}

/// Views the underlying data as a subslice of the original data.
///
/// To avoid creating `&mut` references that alias, this has a
cuviper marked this conversation as resolved.
Show resolved Hide resolved
/// borrowed lifetime from the iterator.
cuviper marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # #![feature(slice_iter_mut_as_slice)]
/// // First, we declare a type which has `iter_mut` method to get the `IterMut`
cuviper marked this conversation as resolved.
Show resolved Hide resolved
/// // struct (&[usize here]):
/// let mut slice = &mut [1, 2, 3];
cuviper marked this conversation as resolved.
Show resolved Hide resolved
///
/// // Then, we get the iterator:
/// let mut iter = slice.iter_mut();
/// // So if we print what `as_slice` method returns here, we have "[1, 2, 3]":
cuviper marked this conversation as resolved.
Show resolved Hide resolved
/// println!("{:?}", iter.as_slice());
cuviper marked this conversation as resolved.
Show resolved Hide resolved
/// assert_eq!(iter.as_slice(), &[1, 2, 3]);
///
/// // Next, we move to the second element of the slice:
/// iter.next();
/// // Now `as_slice` returns "[2, 3]":
/// println!("{:?}", iter.as_slice());
cuviper marked this conversation as resolved.
Show resolved Hide resolved
/// assert_eq!(iter.as_slice(), &[2, 3]);
/// ```
#[unstable(feature = "slice_iter_mut_as_slice", reason = "recently added", issue = "0")]
pub fn as_slice(&self) -> &[T] {
self.make_slice()
}
}

iterator!{struct IterMut -> *mut T, &'a mut T, mut, {mut}, {}}
Expand Down