summaryrefslogtreecommitdiffstats
path: root/vendor/itertools/src/peek_nth.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/itertools/src/peek_nth.rs
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/itertools/src/peek_nth.rs')
-rw-r--r--vendor/itertools/src/peek_nth.rs84
1 files changed, 76 insertions, 8 deletions
diff --git a/vendor/itertools/src/peek_nth.rs b/vendor/itertools/src/peek_nth.rs
index bcca45838..e8546030d 100644
--- a/vendor/itertools/src/peek_nth.rs
+++ b/vendor/itertools/src/peek_nth.rs
@@ -5,6 +5,7 @@ use std::iter::Fuse;
/// See [`peek_nth()`] for more information.
#[derive(Clone, Debug)]
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct PeekNth<I>
where
I: Iterator,
@@ -39,25 +40,30 @@ where
self.peek_nth(0)
}
+ /// Works exactly like the `peek_mut` method in `std::iter::Peekable`
+ pub fn peek_mut(&mut self) -> Option<&mut I::Item> {
+ self.peek_nth_mut(0)
+ }
+
/// Returns a reference to the `nth` value without advancing the iterator.
///
/// # Examples
///
/// Basic usage:
///
- /// ```rust
+ /// ```
/// use itertools::peek_nth;
///
- /// let xs = vec![1,2,3];
- /// let mut iter = peek_nth(xs.iter());
+ /// let xs = vec![1, 2, 3];
+ /// let mut iter = peek_nth(xs.into_iter());
///
- /// assert_eq!(iter.peek_nth(0), Some(&&1));
- /// assert_eq!(iter.next(), Some(&1));
+ /// assert_eq!(iter.peek_nth(0), Some(&1));
+ /// assert_eq!(iter.next(), Some(1));
///
/// // The iterator does not advance even if we call `peek_nth` multiple times
- /// assert_eq!(iter.peek_nth(0), Some(&&2));
- /// assert_eq!(iter.peek_nth(1), Some(&&3));
- /// assert_eq!(iter.next(), Some(&2));
+ /// assert_eq!(iter.peek_nth(0), Some(&2));
+ /// assert_eq!(iter.peek_nth(1), Some(&3));
+ /// assert_eq!(iter.next(), Some(2));
///
/// // Calling `peek_nth` past the end of the iterator will return `None`
/// assert_eq!(iter.peek_nth(1), None);
@@ -69,6 +75,68 @@ where
self.buf.get(n)
}
+
+ /// Returns a mutable reference to the `nth` value without advancing the iterator.
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// use itertools::peek_nth;
+ ///
+ /// let xs = vec![1, 2, 3, 4, 5];
+ /// let mut iter = peek_nth(xs.into_iter());
+ ///
+ /// assert_eq!(iter.peek_nth_mut(0), Some(&mut 1));
+ /// assert_eq!(iter.next(), Some(1));
+ ///
+ /// // The iterator does not advance even if we call `peek_nth_mut` multiple times
+ /// assert_eq!(iter.peek_nth_mut(0), Some(&mut 2));
+ /// assert_eq!(iter.peek_nth_mut(1), Some(&mut 3));
+ /// assert_eq!(iter.next(), Some(2));
+ ///
+ /// // Peek into the iterator and set the value behind the mutable reference.
+ /// if let Some(p) = iter.peek_nth_mut(1) {
+ /// assert_eq!(*p, 4);
+ /// *p = 9;
+ /// }
+ ///
+ /// // The value we put in reappears as the iterator continues.
+ /// assert_eq!(iter.next(), Some(3));
+ /// assert_eq!(iter.next(), Some(9));
+ ///
+ /// // Calling `peek_nth_mut` past the end of the iterator will return `None`
+ /// assert_eq!(iter.peek_nth_mut(1), None);
+ /// ```
+ pub fn peek_nth_mut(&mut self, n: usize) -> Option<&mut I::Item> {
+ let unbuffered_items = (n + 1).saturating_sub(self.buf.len());
+
+ self.buf.extend(self.iter.by_ref().take(unbuffered_items));
+
+ self.buf.get_mut(n)
+ }
+
+ /// Works exactly like the `next_if` method in `std::iter::Peekable`
+ pub fn next_if(&mut self, func: impl FnOnce(&I::Item) -> bool) -> Option<I::Item> {
+ match self.next() {
+ Some(item) if func(&item) => Some(item),
+ Some(item) => {
+ self.buf.push_front(item);
+ None
+ }
+ _ => None,
+ }
+ }
+
+ /// Works exactly like the `next_if_eq` method in `std::iter::Peekable`
+ pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<I::Item>
+ where
+ T: ?Sized,
+ I::Item: PartialEq<T>,
+ {
+ self.next_if(|next| next == expected)
+ }
}
impl<I> Iterator for PeekNth<I>