-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
libcore: Add iter::from_generator
which is like iter::from_fn
, but for coroutines instead of functions
#96298
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
This comment was marked as resolved.
This comment was marked as resolved.
/// [`iter::from_fn`]: crate::iter::from_fn | ||
#[inline] | ||
#[unstable(feature = "iter_from_generator", issue = "none", reason = "generators are unstable")] | ||
pub fn from_generator<G: Generator<Return = ()> + Unpin>( |
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.
It might be reasonable to also add a version that accepts a Pin<&mut G>
, since that way immovable generators can be used as well.
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.
I'm intrigued if there's a way to support both from the same function (is a Pin<&mut Generator<Return = ()>>: Generator<Return = ()> + Unpin
?)
Either way, I think we can add that on a follow up PR.
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.
I'm not quite sure what you mean. Right now there isn't an easy way to accept both direct mutable references and pinned ones, but that would be a lovely thing.
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.
I'm just wondering what changes we could make so that Pin<&mut Generator<Return = ()>>
can satisfy Generator<Return = ()> + Unpin
, making this function work for both versions.
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.
Oh! Now I understand, I think. That's actually a good question...
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.
Pretty sure that's already the case for Future
(yup) so it shouldn't be hard to do it for Generator
.
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.
Wait, no, it has that already
The main cases I've seen of this pattern in the wild have been via the propane crate which mostly provides exactly what this does except via a macro instead. Not sure if there are more recent uses that folks can point to, though. |
This comment was marked as resolved.
This comment was marked as resolved.
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.
I'm r=me after addressing @jyn514's nitpicks. If we ever stabilize generators this API will likely be either redundant or changed, but while the whole feature is unstable, having these kind of affordances is important.
…t for coroutines instead of functions
@bors r=estebank |
📌 Commit 5bf23f6 has been approved by |
☀️ Test successful - checks-actions |
Finished benchmarking commit (4f68efa): comparison url. Instruction count
Max RSS (memory usage)Results
CyclesResults
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression Footnotes |
An equally useful little helper.
I didn't follow any of the async-wg work, so I don't know why something like this wasn't added before.