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

Stabilize peekable_peek_mut #81938

Merged
merged 5 commits into from
Apr 8, 2021
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
3 changes: 1 addition & 2 deletions library/core/src/iter/adapters/peekable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ impl<I: Iterator> Peekable<I> {
/// Basic usage:
///
/// ```
/// #![feature(peekable_peek_mut)]
/// let mut iter = [1, 2, 3].iter().peekable();
///
/// // Like with `peek()`, we can see into the future without advancing the iterator.
Expand All @@ -251,7 +250,7 @@ impl<I: Iterator> Peekable<I> {
/// assert_eq!(iter.collect::<Vec<_>>(), vec![&5, &3]);
/// ```
#[inline]
#[unstable(feature = "peekable_peek_mut", issue = "78302")]
#[stable(feature = "peekable_peek_mut", since = "1.52.0")]
lukaslueg marked this conversation as resolved.
Show resolved Hide resolved
pub fn peek_mut(&mut self) -> Option<&mut I::Item> {
let iter = &mut self.iter;
self.peeked.get_or_insert_with(|| iter.next()).as_mut()
Expand Down
46 changes: 34 additions & 12 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,20 +936,16 @@ pub trait Iterator {
Enumerate::new(self)
}

/// Creates an iterator which can use [`peek`] to look at the next element of
/// the iterator without consuming it.
/// Creates an iterator which can use the [`peek`] and [`peek_mut`] methods
/// to look at the next element of the iterator without consuming it. See
/// their documentation for more information.
///
/// Adds a [`peek`] method to an iterator. See its documentation for
/// more information.
/// Note that the underlying iterator is still advanced when [`peek`] or
/// [`peek_mut`] are called for the first time: In order to retrieve the
/// next element, [`next`] is called on the underlying iterator, hence any
/// side effects (i.e. anything other than fetching the next value) of
lukaslueg marked this conversation as resolved.
Show resolved Hide resolved
/// the [`next`] method will occur.
///
/// Note that the underlying iterator is still advanced when [`peek`] is
/// called for the first time: In order to retrieve the next element,
/// [`next`] is called on the underlying iterator, hence any side effects (i.e.
/// anything other than fetching the next value) of the [`next`] method
/// will occur.
///
/// [`peek`]: Peekable::peek
/// [`next`]: Iterator::next
///
/// # Examples
///
Expand All @@ -976,6 +972,32 @@ pub trait Iterator {
/// assert_eq!(iter.peek(), None);
/// assert_eq!(iter.next(), None);
/// ```
///
/// Using [`peek_mut`] to mutate the next item without advancing the
/// iterator:
///
/// ```
/// let xs = [1, 2, 3];
///
/// let mut iter = xs.iter().peekable();
///
/// // peek_mut() lets us see into the future
lukaslueg marked this conversation as resolved.
Show resolved Hide resolved
/// assert_eq!(iter.peek_mut(), Some(&mut &1));
/// assert_eq!(iter.peek_mut(), Some(&mut &1));
/// assert_eq!(iter.next(), Some(&1));
///
/// if let Some(mut p) = iter.peek_mut() {
/// assert_eq!(*p, &2);
/// // put a value into the iterator
/// *p = &1000;
/// }
///
/// // The value reappears as the iterator continues
/// assert_eq!(iter.collect::<Vec<_>>(), vec![&1000, &3]);
/// ```
/// [`peek`]: Peekable::peek
/// [`peek_mut`]: Peekable::peek_mut
/// [`next`]: Iterator::next
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn peekable(self) -> Peekable<Self>
Expand Down
1 change: 0 additions & 1 deletion library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
#![feature(unwrap_infallible)]
#![feature(option_result_unwrap_unchecked)]
#![feature(result_into_ok_or_err)]
#![feature(peekable_peek_mut)]
#![cfg_attr(not(bootstrap), feature(ptr_metadata))]
#![feature(once_cell)]
#![feature(unsized_tuple_coercion)]
Expand Down