Skip to content

Commit 54ad568

Browse files
committed
Implement PeekableIterator for Peekable
1 parent 6fc70f2 commit 54ad568

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

library/core/src/iter/adapters/peekable.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::iter::adapters::SourceIter;
2-
use crate::iter::{FusedIterator, TrustedLen};
2+
use crate::iter::{FusedIterator, PeekableIterator, TrustedLen};
33
use crate::ops::{ControlFlow, Try};
44

55
/// An iterator with a `peek()` that returns an optional reference to the next
@@ -322,6 +322,14 @@ impl<I: Iterator> Peekable<I> {
322322
#[unstable(feature = "trusted_len", issue = "37572")]
323323
unsafe impl<I> TrustedLen for Peekable<I> where I: TrustedLen {}
324324

325+
#[unstable(feature = "peekable_iterator", issue = "132973")]
326+
impl<I: Iterator> PeekableIterator for Peekable<I> {
327+
fn peek_with<T>(&mut self, func: impl for<'a> FnOnce(Option<&'a Self::Item>) -> T) -> T {
328+
let tmp = self.peek();
329+
func(tmp.as_ref())
330+
}
331+
}
332+
325333
#[unstable(issue = "none", feature = "inplace_iteration")]
326334
unsafe impl<I: Iterator> SourceIter for Peekable<I>
327335
where

0 commit comments

Comments
 (0)