Skip to content

Commit

Permalink
Allow PeekingNext to be used as a trait object.
Browse files Browse the repository at this point in the history
This change makes `PeekingNext` object-safe and object-ergonomic by
requiring `Self: Sized` on generic methods and implementing
`PeekingNext` transitively over mutable references. This allows
`PeekingNext` to be used as a trait object just like `Iterator`.
  • Loading branch information
olson-sean-k committed Sep 9, 2022
1 parent 7883f59 commit e6c6bd0
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/peeking_take_while.rs
Expand Up @@ -16,7 +16,18 @@ pub trait PeekingNext : Iterator {
/// if `accept` returns true, return it as the next element,
/// else None.
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item>
where F: FnOnce(&Self::Item) -> bool;
where Self: Sized,
F: FnOnce(&Self::Item) -> bool;
}

impl<'a, I> PeekingNext for &'a mut I
where I: PeekingNext,
{
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item>
where F: FnOnce(&Self::Item) -> bool
{
(*self).peeking_next(accept)
}
}

impl<I> PeekingNext for Peekable<I>
Expand Down

0 comments on commit e6c6bd0

Please sign in to comment.