There's some useful methods on core::iter::Peekable, but those require making an adapter around your trait and then you can't ever get the original iterator back. Since many iterators could peek at their next element, it seems to me like it would be useful to make a trait that allows calling those methods without needing to use the wrapper.
I envision the trait looking pretty similar to the current Peekable struct, minus the methods that require mutating (peek_mut, next_if_map, next_if_map_mut):
trait PeekableIterator: Iterator {
// Required method
fn peek(&mut self) -> Option<&Self::Item>;
// Optional methods
pub fn next_if(
&mut self,
func: impl FnOnce(&Self::Item) -> bool,
) -> Option<<I as Iterator>::Item> { ... }
fn next_if_eq(&mut self, expected: &Self::Item) -> Option<Self::Item>
where
Self::Item: PartialEq<Self::Item>,
{ ... }
}
I think most base iterators in the stdlib would be able to implement this, and some combinators would be able to implement this if the iterator(s) they wrap do (the biggest exception would be anything Iterator::map-like, since the function can't be called multiple times and there's no space to store the mapped value).
The current Peekable iterator would also implement this trait but otherwise be unchanged by this PR (it keeps inherent methods . I envision it having a similar status to the Fuse type and the FusedIterator trait, where it exists as a wrapper to make a normally-non-peekable iterator implement PeekableIterator, but also with the mutating methods.
The remaining methods I didn't include from Peekable could probably be put in a PeekableMutIterator trait, but imo that can be a future item if there's demand, since I've never needed to use any of those.