Skip to content

Add core::iter::adapters::PeekMap and core::iter::Peekable::peek_map #118474

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions library/core/src/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod intersperse;
mod map;
mod map_while;
mod map_windows;
mod peek_map;
mod peekable;
mod rev;
mod scan;
Expand All @@ -31,8 +32,9 @@ mod zip;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::{
chain::Chain, cycle::Cycle, enumerate::Enumerate, filter::Filter, filter_map::FilterMap,
flatten::FlatMap, fuse::Fuse, inspect::Inspect, map::Map, peekable::Peekable, rev::Rev,
scan::Scan, skip::Skip, skip_while::SkipWhile, take::Take, take_while::TakeWhile, zip::Zip,
flatten::FlatMap, fuse::Fuse, inspect::Inspect, map::Map, peek_map::PeekMap,
peekable::Peekable, rev::Rev, scan::Scan, skip::Skip, skip_while::SkipWhile, take::Take,
take_while::TakeWhile, zip::Zip,
};

#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")]
Expand Down
74 changes: 74 additions & 0 deletions library/core/src/iter/adapters/peek_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::iter::{FusedIterator, Peekable};

/// An iterator that maps the values of `iter` with `f`.
///
/// This struct is created by the [`peek_map`] method on [`Peekable`]. See its
/// documentation for more.
///
/// [`peek_map`]: Peekable::peek_map
/// [`Peekable`]: struct.Peekable.html
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(peek_map)]
///
/// let a = [1, 2, 3];
/// let mut iter = a.into_iter().peekable().peek_map(|x, next| x * *next.unwrap_or(&1));
///
/// assert_eq!(iter.next(), Some(2));
/// assert_eq!(iter.next(), Some(6));
/// assert_eq!(iter.next(), Some(3));
/// assert_eq!(iter.next(), None);
/// ```
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[unstable(feature = "peek_map", issue = "118474")]
#[derive(Debug)]
pub struct PeekMap<T, F> {
pub(crate) t: T,
f: F,
}

impl<I: Iterator, F> PeekMap<Peekable<I>, F> {
pub(in crate::iter) fn new(peekable: Peekable<I>, f: F) -> PeekMap<Peekable<I>, F> {
PeekMap { t: peekable, f }
}
}

#[unstable(feature = "peek_map", issue = "118474")]
impl<B, I: Iterator, F> Iterator for PeekMap<Peekable<I>, F>
where
F: FnMut(I::Item, Option<&I::Item>) -> B,
{
type Item = B;

#[inline]
fn next(&mut self) -> Option<B> {
Some((&mut self.f)(self.t.next()?, self.t.peek()))
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.t.size_hint()
}
}
#[unstable(feature = "peek_map", issue = "118474")]
impl<B, I: ExactSizeIterator, F> ExactSizeIterator for PeekMap<Peekable<I>, F>
where
F: FnMut(I::Item, Option<&I::Item>) -> B,
{
fn len(&self) -> usize {
self.t.len()
}

fn is_empty(&self) -> bool {
self.t.is_empty()
}
}
#[unstable(feature = "peek_map", issue = "118474")]
impl<B, I: FusedIterator, F> FusedIterator for PeekMap<Peekable<I>, F> where
F: FnMut(I::Item, Option<&I::Item>) -> B
{
}
11 changes: 10 additions & 1 deletion library/core/src/iter/adapters/peekable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::iter::{adapters::SourceIter, FusedIterator, TrustedLen};
use crate::iter::{adapters::PeekMap, adapters::SourceIter, FusedIterator, TrustedLen};
use crate::ops::{ControlFlow, Try};

/// An iterator with a `peek()` that returns an optional reference to the next
Expand Down Expand Up @@ -316,6 +316,15 @@ impl<I: Iterator> Peekable<I> {
{
self.next_if(|next| next == expected)
}

/// Honestly I'm adding this doc to get rid of the error just to make sure everything else actually works.
#[unstable(feature = "peek_map", issue = "118474")]
pub fn peek_map<B, F>(self, f: F) -> PeekMap<Self, F>
where
F: FnMut(I::Item, Option<&I::Item>) -> B,
{
PeekMap::new(self, f)
}
}

#[unstable(feature = "trusted_len", issue = "37572")]
Expand Down