Skip to content
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

Implement PeekingNext transitively over mutable references. #643

Merged
merged 1 commit into from Jun 15, 2023

Conversation

olson-sean-k
Copy link
Contributor

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 and here, for example). See also this discussion on the Rust internals forum.

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).
@olson-sean-k
Copy link
Contributor Author

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 PeekingNext object-safe is well and good, but has far less utility today, as it has no methods that can be invoked through such an object and its super-trait Iterator can be used for dynamic dispatch instead.

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 types by value, but it is not uncommon to use such types through a mutable borrow. Note that PeekingNext enables the use of functions like Itertools::peeking_take_while, which may be important to both the caller of sprockets and the sprockets implementation.

@olson-sean-k olson-sean-k changed the title Allow PeekingNext to be used as a trait object. Implement PeekingNext transitively over mutable references. Sep 12, 2022
@olson-sean-k
Copy link
Contributor Author

See also #644, which implements PeekingNext for PeekingTakeWhile. 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).

@jswrenn jswrenn added this to the next milestone Jun 14, 2023
bors bot added a commit that referenced this pull request Jun 15, 2023
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>
Copy link
Member

@jswrenn jswrenn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bors r+

@bors
Copy link
Contributor

bors bot commented Jun 15, 2023

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.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 3e92550 into rust-itertools:master Jun 15, 2023
1 check passed
@jswrenn jswrenn modified the milestones: next, v11.0.0 Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants