-
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
Rename core::future::poll_fn
to core::future::from_fn
#76789
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
☔ The latest upstream changes (presumably #76964) made this pull request unmergeable. Please resolve the merge conflicts. Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:
|
I find |
@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author |
@carllerche told me he was -1 on this change but can't post right now for technical reasons. He asked me to post this on his behalf:
|
I agree with @carllerche on this one, I think |
Based on my own experience and others I've asked, I think a lot of people find the At the same time, I can see that A modest proposal: What about |
To be honest, I don't think
I think this is even more confusing :D Looking at the signatures it looks like In the end, I don't have a super strong opinion but ambiguity is a problem here imo. |
What about the name do you find confusing? The type of the future we can name however we want. People don't usually write the name of the type directly, so it's not as important as making the function name clear in my opinion. |
I think its just longer than it needs to be, but maybe this is just me being used to using
Right, my point was that the name |
Usually, it's the other way around, especially around iterators: the return value is named after the combinator function. This reflects in the docs where the documentation is on the function, and I'd like to add that Late thought: This may come from the habit that we rarely talk about Futures being a thing we construct, mainly about that "something is a future". |
@tmandry I think that's a reasonable suggestion; it clears up what kind of function we're expecting, and feels consistent enough with what we have. As @skade correctly points out the most pressing concern is that |
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 wouldn't otherwise weigh in, but since I am the assigned reviewer: my preference would be to keep poll_fn.
I think the "idiom to use from
and into
when you create a value from another one" is overstated. What does this mean? Functions with 1 argument (or 2, like ptr+len?) which return non-()
and do not perform I/O? There are plenty of counterexamples with a clearer name, e.g. iter::repeat I don't think would be clearer as iter::from / from_val / from_repeat / from_repeated_val / into_repeating / into_repeater / ...
future::poll_fn
makes a future which polls a fn (poll as a verb) or you give it the implementation for its Future's poll fn (poll as a noun), so there are two sensible ways to internalize what this does that both draw exactly the correct distinction from other ways of treating a fn as a future.
I think |
I'm noticing I'm low on energy and don't have the energy to engage in the dialog needed to see this change through to completion. If someone else wants to see this through, by all means feel free to pick this up. |
Tracking issue: #72302.
This patch renames
core::future::poll_fn
tocore::future::from_fn
as suggested by @Koxiaet in #72303 (comment). This makes the API more consistent with similar functions already in the stdlib, for examplestd::iter::from_fn
, and removes some awkwardness from the naming (I remember being confused by whatpoll_fn
meant when I first encountered it).Drawbacks
The main rationale not to name this
core::future::from_fn
is that thefutures
crate also exposesfutures::future::lazy
which also takes a closure. That would mean we'd have two functions that take a closure at top level. However the stdlib exposesstd::iter::once
, which exists next tostd::iter::from_fn
with both functions creating an iterator from a function; meaning there would be precedent for having both functions in the same hierarchy.However I don't think
futures::future::lazy
is a good fit for the stdlib, and I'd be surprised if we saw it stabilized. It was designed before we hadasync/.await
in the language, as a convenience function to create "lazy values" . But most of its uses have been replaced by async blocks since:With the expected addition of async closures to the language, it's likely the uses for
futures::future::lazy
will only decrease further. Async closures are expected to be able to take closures, take borrows as arguments, and support.await
inside of the closure body. Which makes it seem quite likely we'll only ever expose a single "create a future from a closure" method in thecore::future
hierarchy.cc/ @rust-lang/wg-async-foundations