Skip to content

Commit c1d4003

Browse files
authored
Rollup merge of #101189 - daxpedda:ready-into-inner, r=joshtriplett
Implement `Ready::into_inner()` 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](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.
2 parents f111209 + 5ed1787 commit c1d4003

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

library/core/src/future/ready.rs

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ impl<T> Future for Ready<T> {
2424
}
2525
}
2626

27+
impl<T> Ready<T> {
28+
/// Consumes the `Ready`, returning the wrapped value.
29+
///
30+
/// # Panics
31+
///
32+
/// Will panic if this [`Ready`] was already polled to completion.
33+
///
34+
/// # Examples
35+
///
36+
/// ```
37+
/// #![feature(ready_into_inner)]
38+
/// use std::future;
39+
///
40+
/// let a = future::ready(1);
41+
/// assert_eq!(a.into_inner(), 1);
42+
/// ```
43+
#[unstable(feature = "ready_into_inner", issue = "101196")]
44+
#[must_use]
45+
#[inline]
46+
pub fn into_inner(self) -> T {
47+
self.0.expect("Called `into_inner()` on `Ready` after completion")
48+
}
49+
}
50+
2751
/// Creates a future that is immediately ready with a value.
2852
///
2953
/// Futures created through this function are functionally similar to those

0 commit comments

Comments
 (0)