Skip to content

Commit 3e92550

Browse files
Merge #643
643: Implement `PeekingNext` transitively over mutable references. r=jswrenn a=olson-sean-k This PR allows `PeekingNext` to be used as a trait object just like `Iterator`, allowing code to accept types like `&mut dyn PeekingNext<Item = T>` for some item type `T`. To accomplish this, generic methods now require that `Self: Sized` and `PeekingNext` now has a transitive implementation over mutable references to types that implement `PeekingNext`. This mirrors the design of the `Iterator` trait ([here](https://doc.rust-lang.org/nightly/src/core/iter/traits/iterator.rs.html#776-782) and [here](https://doc.rust-lang.org/nightly/src/core/iter/traits/iterator.rs.html), for example). See also [this discussion on the Rust internals forum](https://internals.rust-lang.org/t/generic-methods-over-object-safe-traits/6774). Co-authored-by: Sean Olson <[email protected]>
2 parents 8184e4c + 590fdef commit 3e92550

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)