From abcbe5457571eabf4d51c439a78af0485dd0ccb1 Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Tue, 9 Feb 2021 22:14:32 +0100 Subject: [PATCH 1/4] Stabilize `peekable_peek_mut` Resolves #78302 Update peekable.rs Update library/core/src/iter/traits/iterator.rs Co-authored-by: Ashley Mannix --- library/core/src/iter/adapters/peekable.rs | 3 +- library/core/src/iter/traits/iterator.rs | 46 ++++++++++++++++------ library/core/tests/lib.rs | 1 - 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/library/core/src/iter/adapters/peekable.rs b/library/core/src/iter/adapters/peekable.rs index 43301444e3e..dfa59df6dab 100644 --- a/library/core/src/iter/adapters/peekable.rs +++ b/library/core/src/iter/adapters/peekable.rs @@ -233,7 +233,6 @@ pub fn peek(&mut self) -> Option<&I::Item> { /// 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. @@ -251,7 +250,7 @@ pub fn peek(&mut self) -> Option<&I::Item> { /// assert_eq!(iter.collect::>(), vec![&5, &3]); /// ``` #[inline] - #[unstable(feature = "peekable_peek_mut", issue = "78302")] + #[stable(feature = "peekable_peek_mut", since = "1.52.0")] 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() diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 46e1a3a4aa2..7de068f5835 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -936,20 +936,16 @@ fn enumerate(self) -> Enumerate 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 + /// 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 /// @@ -976,6 +972,32 @@ fn enumerate(self) -> Enumerate /// 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 + /// 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![&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 diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 997e618cc51..4d34ade210f 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -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)] From 7f32fda78c60bb5b05e610a1c0c0fecaff07f497 Mon Sep 17 00:00:00 2001 From: lukaslueg Date: Tue, 6 Apr 2021 18:16:02 +0200 Subject: [PATCH 2/4] Update library/core/src/iter/adapters/peekable.rs Co-authored-by: Alexander Ronald Altman --- library/core/src/iter/adapters/peekable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/iter/adapters/peekable.rs b/library/core/src/iter/adapters/peekable.rs index dfa59df6dab..21386e28a96 100644 --- a/library/core/src/iter/adapters/peekable.rs +++ b/library/core/src/iter/adapters/peekable.rs @@ -250,7 +250,7 @@ pub fn peek(&mut self) -> Option<&I::Item> { /// assert_eq!(iter.collect::>(), vec![&5, &3]); /// ``` #[inline] - #[stable(feature = "peekable_peek_mut", since = "1.52.0")] + #[stable(feature = "peekable_peek_mut", since = "1.53.0")] 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() From 4c850f3783d1cb5f216da50e29aa2c9ed0164a93 Mon Sep 17 00:00:00 2001 From: lukaslueg Date: Wed, 7 Apr 2021 18:02:39 +0200 Subject: [PATCH 3/4] Update library/core/src/iter/traits/iterator.rs Co-authored-by: Yuki Okushi --- library/core/src/iter/traits/iterator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 057004f1912..8a89f72ca5c 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -944,7 +944,7 @@ fn enumerate(self) -> Enumerate /// 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 + /// side effects (i.e. anything other than fetching the next value) of /// the [`next`] method will occur. /// /// From cfe43f9733233a62026965338d8ee1f8e44fc8b0 Mon Sep 17 00:00:00 2001 From: lukaslueg Date: Wed, 7 Apr 2021 18:02:46 +0200 Subject: [PATCH 4/4] Update library/core/src/iter/traits/iterator.rs Co-authored-by: Yuki Okushi --- library/core/src/iter/traits/iterator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 8a89f72ca5c..abd44b47f98 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -982,7 +982,7 @@ fn enumerate(self) -> Enumerate /// /// let mut iter = xs.iter().peekable(); /// - /// // peek_mut() lets us see into the future + /// // `peek_mut()` lets us see into the future /// assert_eq!(iter.peek_mut(), Some(&mut &1)); /// assert_eq!(iter.peek_mut(), Some(&mut &1)); /// assert_eq!(iter.next(), Some(&1));