itertools
itertools copied to clipboard
PeekingNext has a somewhat strange API
peeking_next(&mut self, accept: impl FnOnce(..) ->bool) can be implemented in terms of peek(&mut self) -> Option<&Item> and next. Conversely, peek can be implemented in terms of peeking_next. So, they are equivalent.
However, peeking_next is a more complicated API. So IMO ideally,
- The trait's required method ought to be
peek. (This has a name clash withstd::iter::Peekable::peek, but I think that's OK because the inherent method will take priority when the type is justPeekable.) peeking_nextought to be a provioded method.- Probably, then, the name is then wrong.
Peekseems like it would be right.
These would be breaking changes, but I think implementations of PeekingNext are going to be tolerably rare, and the fix will be obvious. The key thing will be to avoid breaking all trait bounds. I suggest, in a maor revision,
- Abolish
PeekingNextand replace it withPeek, withpeekas required method andpeeking_nextas provided. - Additionally export
PeekasPeekingNextwith a#[deprecated]. - Possibly provide
#[deprecated] pub fn impl_peek_via_peeking_next(self_: &mut impl Peek, accept: ...) -> ... - Put appropriate notes in the docs about the migration.
I think the effect is that:
- Downstreams that don't
impl PeekingNextwill see no actual break, although they'll see a new deprecation warning. - Downstreams that
impl PeekingNextwill get an error saying "you need to implement the required methodpeek". Turning an implementation ofpeeking_nextinto an implementation ofpeekwill almost always be easy, just deleting code. Alternatively, the downstream could just addfn peek(&mut self) -> ... { itertools::impl_peek_via_peeking_next..., if we chose to provide it.
This should probably be done at the same time as #678, which becomes impl Peek for &mut Peek.
See also #583