Implement PeekingNext transitively over mutable references.#643
Implement PeekingNext transitively over mutable references.#643bors[bot] merged 1 commit intorust-itertools:masterfrom
PeekingNext transitively over mutable references.#643Conversation
e6c6bd0 to
590fdef
Compare
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).
|
I've changed the commit message to emphasize the transitive implementation over mutable references, as this is the most important part of this change. Making I think the most compelling reason to land this is that code like this can easily accept both owned and mutably borrowed iterators: pub fn sprockets(widgets: impl PeekingNext<Item = Widget>) -> impl Iterator<Item = Sprocket> { ... }Today, this only accepts |
PeekingNext to be used as a trait object.PeekingNext transitively over mutable references.
|
See also #644, which implements |
644: Implement `PeekingNext` for `PeekingTakeWhile`. r=jswrenn a=olson-sean-k This PR implements `PeekingNext` for `PeekingTakeWhile` by composing its predicate with the predicate given to `PeekingNext::peeking_next`. This allows `Itertools::peeking_take_while` to be chained and for subsequent calls, including those across function boundaries, to function as expected while restoring items in the originating iterator. See also #643, which implements `PeekingNext` for mutable references. In combination, these changes allow code to generically accept types implementing `PeekingNext` where `Itertools::peeking_take_while` can be used by the caller to prepare an iterator and subsequently by a function where restoring items in the originating iterator is important (i.e., the function cannot simply use `Iterator::peekable` etc., because `Iterator::next` would unconditionally be called on the originating iterator). Co-authored-by: Sean Olson <olson.sean.k@gmail.com>
|
Build succeeded! The publicly hosted instance of bors-ng is deprecated and will go away soon. If you want to self-host your own instance, instructions are here. If you want to switch to GitHub's built-in merge queue, visit their help page. |
This PR allows
PeekingNextto be used as a trait object just likeIterator, allowing code to accept types like&mut dyn PeekingNext<Item = T>for some item typeT. To accomplish this, generic methods now require thatSelf: SizedandPeekingNextnow has a transitive implementation over mutable references to types that implementPeekingNext.This mirrors the design of the
Iteratortrait (here and here, for example). See also this discussion on the Rust internals forum.