1 #![stable(feature = "futures_api", since = "1.36.0")]
3 //! Asynchronous values.
6 ops::{Generator, GeneratorState},
19 #[stable(feature = "futures_api", since = "1.36.0")]
20 pub use self::future::Future;
22 #[unstable(feature = "future_join", issue = "91642")]
23 pub use self::join::join;
25 #[unstable(feature = "into_future", issue = "67644")]
26 pub use into_future::IntoFuture;
28 #[stable(feature = "future_readiness_fns", since = "1.48.0")]
29 pub use pending::{pending, Pending};
30 #[stable(feature = "future_readiness_fns", since = "1.48.0")]
31 pub use ready::{ready, Ready};
33 #[unstable(feature = "future_poll_fn", issue = "72302")]
34 pub use poll_fn::{poll_fn, PollFn};
36 /// This type is needed because:
38 /// a) Generators cannot implement `for<'a, 'b> Generator<&'a mut Context<'b>>`, so we need to pass
39 /// a raw pointer (see <https://github.com/rust-lang/rust/issues/68923>).
40 /// b) Raw pointers and `NonNull` aren't `Send` or `Sync`, so that would make every single future
41 /// non-Send/Sync as well, and we don't want that.
43 /// It also simplifies the HIR lowering of `.await`.
45 #[unstable(feature = "gen_future", issue = "50547")]
46 #[derive(Debug, Copy, Clone)]
47 pub struct ResumeTy(NonNull<Context<'static>>);
49 #[unstable(feature = "gen_future", issue = "50547")]
50 unsafe impl Send for ResumeTy {}
52 #[unstable(feature = "gen_future", issue = "50547")]
53 unsafe impl Sync for ResumeTy {}
55 /// Wrap a generator in a future.
57 /// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
58 /// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
59 // This is `const` to avoid extra errors after we recover from `const async fn`
60 #[lang = "from_generator"]
62 #[unstable(feature = "gen_future", issue = "50547")]
63 #[rustc_const_unstable(feature = "gen_future", issue = "50547")]
65 pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
67 T: Generator<ResumeTy, Yield = ()>,
69 #[rustc_diagnostic_item = "gen_future"]
70 struct GenFuture<T: Generator<ResumeTy, Yield = ()>>(T);
72 // We rely on the fact that async/await futures are immovable in order to create
73 // self-referential borrows in the underlying generator.
74 impl<T: Generator<ResumeTy, Yield = ()>> !Unpin for GenFuture<T> {}
76 impl<T: Generator<ResumeTy, Yield = ()>> Future for GenFuture<T> {
77 type Output = T::Return;
78 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
79 // SAFETY: Safe because we're !Unpin + !Drop, and this is just a field projection.
80 let gen = unsafe { Pin::map_unchecked_mut(self, |s| &mut s.0) };
82 // Resume the generator, turning the `&mut Context` into a `NonNull` raw pointer. The
83 // `.await` lowering will safely cast that back to a `&mut Context`.
84 match gen.resume(ResumeTy(NonNull::from(cx).cast::<Context<'static>>())) {
85 GeneratorState::Yielded(()) => Poll::Pending,
86 GeneratorState::Complete(x) => Poll::Ready(x),
94 #[lang = "get_context"]
96 #[unstable(feature = "gen_future", issue = "50547")]
99 pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> {
100 // SAFETY: the caller must guarantee that `cx.0` is a valid pointer
101 // that fulfills all the requirements for a mutable reference.
102 unsafe { &mut *cx.0.as_ptr().cast() }