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

libcore: Add iter::from_generator which is like iter::from_fn, but for coroutines instead of functions #96298

Merged
merged 1 commit into from
May 27, 2022

Conversation

petrochenkov
Copy link
Contributor

@petrochenkov petrochenkov commented Apr 21, 2022

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.

@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 21, 2022
@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 21, 2022
@petrochenkov petrochenkov added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 21, 2022
@rust-log-analyzer

This comment was marked as resolved.

library/core/src/iter/mod.rs Outdated Show resolved Hide resolved
library/core/src/iter/sources.rs Outdated Show resolved Hide resolved
library/core/tests/iter/sources.rs Outdated Show resolved Hide 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>(
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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...

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

@clarfonthey
Copy link
Contributor

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.

@bors

This comment was marked as resolved.

Copy link
Contributor

@estebank estebank left a 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.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 26, 2022
@petrochenkov
Copy link
Contributor Author

@bors r=estebank

@bors
Copy link
Contributor

bors commented May 26, 2022

📌 Commit 5bf23f6 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 26, 2022
@bors
Copy link
Contributor

bors commented May 27, 2022

⌛ Testing commit 5bf23f6 with merge 4f68efa...

@bors
Copy link
Contributor

bors commented May 27, 2022

☀️ Test successful - checks-actions
Approved by: estebank
Pushing 4f68efa to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 27, 2022
@bors bors merged commit 4f68efa into rust-lang:master May 27, 2022
@rustbot rustbot added this to the 1.63.0 milestone May 27, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4f68efa): comparison url.

Instruction count

  • Primary benchmarks: no relevant changes found
  • Secondary benchmarks: 🎉 relevant improvement found
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
N/A N/A 0
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
-0.9% -0.9% 1
All 😿🎉 (primary) N/A N/A 0

Max RSS (memory usage)

Results
  • Primary benchmarks: no relevant changes found
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
1.5% 1.8% 2
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
-1.6% -1.6% 1
All 😿🎉 (primary) N/A N/A 0

Cycles

Results
  • Primary benchmarks: 😿 relevant regression found
  • Secondary benchmarks: no relevant changes found
mean1 max count2
Regressions 😿
(primary)
2.7% 2.7% 1
Regressions 😿
(secondary)
N/A N/A 0
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
N/A N/A 0
All 😿🎉 (primary) 2.7% 2.7% 1

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet