Skip to content

Commit

Permalink
Implement PeekingNext transitively over mutable references.
Browse files Browse the repository at this point in the history
This change applies patterns used for the standard `Iterator` trait to
the `PeekingNext` trait. Generic methods require `Self: Sized` and
`PeekingNext` is now transitively implemented over mutable references.
This allows generic code to easily accept owned and mutably borrowed
types that implement `PeekingNext`. This also makes `PeekingNext`
object-safe (though this has little utility today).
  • Loading branch information
olson-sean-k committed Sep 12, 2022
1 parent 7883f59 commit 590fdef
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 590fdef

Please sign in to comment.