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 Ready::into_inner() #101189

Merged
merged 1 commit into from
Oct 4, 2022
Merged

Conversation

daxpedda
Copy link
Contributor

@daxpedda daxpedda commented Aug 30, 2022

Tracking issue: #101196.

This implements a method to unwrap the value inside a Ready outside an async context.
See https://docs.rs/futures/0.3.24/futures/future/struct.Ready.html#method.into_inner for previous work.

This was discussed in Zulip beforehand:

An example I'm hitting right now:
I have a cross-platform library that provides a functions that returns a Future. The only reason why it returns a Future is because the WASM platform requires it, but the native doesn't, to make a cross-platform API that is equal for all I just return a Ready on the native targets.

Now I would like to expose native-only functions that aren't async, that users can use to avoid having to deal with async when they are targeting native. With into_inner that's easily solvable now.

I want to point out that some internal restructuring could be used to solve that problem too, but in this case it's not that simple, the library uses internal traits that return the Future already and playing around with that would introduce unnecessary cfg in a lot more places. So it is really only a quality-of-life feature.

@rust-highfive
Copy link
Collaborator

r? @scottmcm

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

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Aug 30, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 30, 2022
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member

Something named into_inner that can panic is surprising enough that I'll spin this over to someone from libs-api even for nightly. (And I'm generally not a good reviewer for async-related stuff.)

r? libs-api

@scottmcm scottmcm 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 Aug 30, 2022
@scottmcm scottmcm assigned joshtriplett and unassigned scottmcm Aug 30, 2022
@daxpedda daxpedda requested a review from joshtriplett August 31, 2022 04:01
@conradludgate
Copy link
Contributor

Ah that's a very good point. I forgot that Ready was Option based. I would offer up the term take to return an option

@daxpedda
Copy link
Contributor Author

daxpedda commented Aug 31, 2022

I modeled this very much after futures::future::Ready::into_inner().

Generally speaking in the async world I have encountered the attitude that doing anything with a Future that was already polled to completion by returning Poll::Ready will panic. You can see this as well in the Future implementation of Ready, it will panic if polled after it already returned Poll::Ready. I'm not in favor of this mechanic, but I would argue that this is a weakness of the Future trait that we can't fix anymore (not that I have an idea how to fix this).

Either way, personally I would prefer a method that will actually panic in this case. I don't oppose adding a take method that returns an Option, but I would argue that it will probably remain unused. I would propose if we introduce a take method, that we might want to rename into_inner into unwrap?

@robjtede
Copy link
Contributor

robjtede commented Sep 25, 2022

Firstly, in my experience of this particular future, I've never needed a take method but into_inner is used frequently.

Given the panic behavior, unwrap does seems appropriate; though, I don't see any precedent in std apart outside of Result/Option. One might presume that if an unwrap is provided then expect, unwrap_or, etc. would also exist. I believe this is an argument in favor of take so that Option methods are not duplicated. End of the day, .take().unwrap() is not much longer than .into_inner() to be any bother.

@joshtriplett
Copy link
Member

Seems relatively reasonable to me. It does seem like few cases will know in advance they have a Ready rather than an impl Future, apart from internal usage within a library. But nonetheless, as an unstable API this seems fine to experiment with. And I agree that expect seems right here.

@bors r+

@bors
Copy link
Contributor

bors commented Oct 3, 2022

📌 Commit 5ed1787 has been approved by joshtriplett

It is now in the queue for this repository.

@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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 3, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 3, 2022
…riplett

Implement `Ready::into_inner()`

Tracking issue: rust-lang#101196.

This implements a method to unwrap the value inside a `Ready` outside an async context.
See https://docs.rs/futures/0.3.24/futures/future/struct.Ready.html#method.into_inner for previous work.

This was discussed in [Zulip beforehand](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/.60Ready.3A.3Ainto_inner.28.29.60):
> An example I'm hitting right now:
I have a cross-platform library that provides a functions that returns a `Future`. The only reason why it returns a `Future` is because the WASM platform requires it, but the native doesn't, to make a cross-platform API that is equal for all I just return a `Ready` on the native targets.
>
> Now I would like to expose native-only functions that aren't async, that users can use to avoid having to deal with async when they are targeting native. With `into_inner` that's easily solvable now.
>
> I want to point out that some internal restructuring could be used to solve that problem too, but in this case it's not that simple, the library uses internal traits that return the `Future` already and playing around with that would introduce unnecessary `cfg` in a lot more places. So it is really only a quality-of-life feature.
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 4, 2022
Rollup of 6 pull requests

Successful merges:

 - rust-lang#101189 (Implement `Ready::into_inner()`)
 - rust-lang#101642 (Fix in-place collection leak when remaining element destructor panic)
 - rust-lang#102489 (Normalize substs before resolving instance in `NoopMethodCall` lint)
 - rust-lang#102559 (Don't ICE when trying to copy unsized value in const prop)
 - rust-lang#102568 (Lint against nested opaque types that don't satisfy associated type bounds)
 - rust-lang#102633 (Fix rustdoc ICE in invalid_rust_codeblocks lint)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c1d4003 into rust-lang:master Oct 4, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 4, 2022
@traviscross traviscross added the WG-async Working group: Async & await label Nov 7, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 16, 2024
…r, r=dtolnay

Stabilize `Ready::into_inner()`

This PR stabilizes `Ready::into_inner()`.

Tracking issue: rust-lang#101196.
Implementation PR: rust-lang#101189.

Closes rust-lang#101196.
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 16, 2024
… r=dtolnay

Stabilize `Ready::into_inner()`

This PR stabilizes `Ready::into_inner()`.

Tracking issue: rust-lang#101196.
Implementation PR: rust-lang#101189.

Closes rust-lang#101196.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. WG-async Working group: Async & await
Projects
None yet
Development

Successfully merging this pull request may close these issues.