-
Notifications
You must be signed in to change notification settings - Fork 339
Implement PeekingNext transitively over mutable references.
#643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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 <[email protected]>
jswrenn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bors r+
|
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.