-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add Future::map
#111347
Add Future::map
#111347
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
This comment has been minimized.
This comment has been minimized.
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 is not great to see such complex unsafe pin projection in the standard library. (And it is actually unsound.)
match pin.poll(cx) { | ||
Poll::Ready(value) => { | ||
// The future must be dropped in-place since it is pinned. | ||
ptr::drop_in_place(future); |
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.
If the future panics here, self.inner
will not be set to None
and will cause use-after-free on the second poll. (similar case to async-rs/async-std#903)
Polling the future after panic is a bit of an odd case, but since poll
is a safe function, it must not cause unsoundness in such a case.
The efficient, correct, and safe pattern here is using pin-project{,-lite}
's project_replace
as done in futures
.
If adding dependencies to core is difficult, I personally recommend vendoring pin-project-lite
and doing the same thing as futures
. (see also previous discussion)
Adding methods to Also, this can be written very easily using async-await. (I remember that when stjepang created (I personally don't think we should piecemeal import combinators like this from ecosystems, as cramertj said in the past.) |
cc @rust-lang/wg-async |
…inators, r=compiler-errors Fix miscompilation when calling default methods on `Future` In rust-lang#111264 I discovered a lingering miscompilation when calling a default method on `Future` (none currently exist). rust-lang#111279 added a debug assertion, which sadly doesn't help much since to my knowledge stage0 is not built with them enabled, and it still doesn't make default methods work like they should. This PR fixes `resolve_instance` to resolve default methods on `Future` correctly, allowing library contributors to add `Future` combinators without running into ICEs or miscompilations. I've tested this as part of rust-lang#111347, but no test is included here (assuming that future methods include their own tests that would cover this sufficiently). r? `@compiler-errors`
I wouldn't mind it if added some form of edit: On second thought, I actually don't know whether it should take an async closure or not. Maybe we should instead just expose a My understanding of why we haven't added this yet is that is because adding methods which are commonly added via extension traits would lead to name resolution conflicts. This can cause issues when upgrading Rust versions, which we'd like to avoid. So in order to avoid that we've been waiting for either #64797 or #64796 to stabilize first. |
@rustbot label +I-async-nominated +A-async-await |
☔ The latest upstream changes (presumably #111358) made this pull request unmergeable. Please resolve the merge conflicts. |
Note that the named future is not really available except in cases where the closure can be converted to a function pointer since map takes a closure. |
I guess I can switch the review flag for now until after the meeting and the following design decisions @rustbot label -S-waiting-on-review +S-waiting-on-team |
We discussed this at today's wg-async meeting (notes). We came to the conclusion that we do want this, but it would be a blocker if it breaks existing code that uses futures-rs, or regresses error messages significantly. I expect we will encounter these problems, though maybe not while the method is unstable, since I think we taught the resolver to deprioritize unstable items at some point. One way or another, we'll have to solve the ambiguity problem between this trait and There were also concerns raised about whether this would impact performance by adding a vtable entry, or whether we should wait for sealed methods (not RFC'd). The conclusion was that as long as the signature continues to return a concrete type that's not constructible from outside the crate, we can tackle that optimization at the same time as the |
However, since I'm not currently employed to work on Rust, I'll close this PR regardless. |
This adds the simplest
Future
combinator to the standard library, mostly to act as a sort of "icebreaker" for adding more of them in the future. A compiler change to fix the miscompilation in #111264 is also included.