Skip to content

Commit 590fdef

Browse files
committed
Implement PeekingNext transitively over mutable references.
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).
1 parent 7883f59 commit 590fdef

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/peeking_take_while.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ pub trait PeekingNext : Iterator {
1616
/// if `accept` returns true, return it as the next element,
1717
/// else None.
1818
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item>
19-
where F: FnOnce(&Self::Item) -> bool;
19+
where Self: Sized,
20+
F: FnOnce(&Self::Item) -> bool;
21+
}
22+
23+
impl<'a, I> PeekingNext for &'a mut I
24+
where I: PeekingNext,
25+
{
26+
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item>
27+
where F: FnOnce(&Self::Item) -> bool
28+
{
29+
(*self).peeking_next(accept)
30+
}
2031
}
2132

2233
impl<I> PeekingNext for Peekable<I>

0 commit comments

Comments
 (0)