]> git.lizzy.rs Git - rust.git/blob - src/libcore/future/mod.rs
9dcb2cea2ea716fec3fe90ebae734db9466143e4
[rust.git] / src / libcore / future / mod.rs
1 #![stable(feature = "futures_api", since = "1.36.0")]
2
3 //! Asynchronous values.
4
5 #[cfg(not(bootstrap))]
6 use crate::{
7     ops::{Generator, GeneratorState},
8     pin::Pin,
9     ptr::NonNull,
10     task::{Context, Poll},
11 };
12
13 mod future;
14 #[stable(feature = "futures_api", since = "1.36.0")]
15 pub use self::future::Future;
16
17 /// This type is needed because:
18 ///
19 /// a) Generators cannot implement `for<'a, 'b> Generator<&'a mut Context<'b>>`, so we need to pass
20 ///    a raw pointer.
21 /// b) Raw pointers and `NonNull` aren't `Send` or `Sync`, so that would make every single future
22 ///    non-Send/Sync as well, and we don't want that.
23 ///
24 /// It also simplifies the HIR lowering of `.await`.
25 #[doc(hidden)]
26 #[unstable(feature = "gen_future", issue = "50547")]
27 #[cfg(not(bootstrap))]
28 #[derive(Debug, Copy, Clone)]
29 pub struct ResumeTy(pub NonNull<Context<'static>>);
30
31 #[unstable(feature = "gen_future", issue = "50547")]
32 #[cfg(not(bootstrap))]
33 unsafe impl Send for ResumeTy {}
34
35 #[unstable(feature = "gen_future", issue = "50547")]
36 #[cfg(not(bootstrap))]
37 unsafe impl Sync for ResumeTy {}
38
39 /// Wrap a generator in a future.
40 ///
41 /// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
42 /// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
43 // This is `const` to avoid extra errors after we recover from `const async fn`
44 #[doc(hidden)]
45 #[unstable(feature = "gen_future", issue = "50547")]
46 #[cfg(not(bootstrap))]
47 #[inline]
48 pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
49 where
50     T: Generator<ResumeTy, Yield = ()>,
51 {
52     #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
53     struct GenFuture<T: Generator<ResumeTy, Yield = ()>>(T);
54
55     // We rely on the fact that async/await futures are immovable in order to create
56     // self-referential borrows in the underlying generator.
57     impl<T: Generator<ResumeTy, Yield = ()>> !Unpin for GenFuture<T> {}
58
59     impl<T: Generator<ResumeTy, Yield = ()>> Future for GenFuture<T> {
60         type Output = T::Return;
61         fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
62             // Safety: Safe because we're !Unpin + !Drop mapping to a ?Unpin value
63             let gen = unsafe { Pin::map_unchecked_mut(self, |s| &mut s.0) };
64
65             // Resume the generator, turning the `&mut Context` into a `NonNull` raw pointer. The
66             // `.await` lowering will safely cast that back to a `&mut Context`.
67             match gen.resume(ResumeTy(NonNull::from(cx).cast::<Context<'static>>())) {
68                 GeneratorState::Yielded(()) => Poll::Pending,
69                 GeneratorState::Complete(x) => Poll::Ready(x),
70             }
71         }
72     }
73
74     GenFuture(gen)
75 }
76
77 #[doc(hidden)]
78 #[unstable(feature = "gen_future", issue = "50547")]
79 #[cfg(not(bootstrap))]
80 #[inline]
81 pub unsafe fn poll_with_context<F>(f: Pin<&mut F>, mut cx: ResumeTy) -> Poll<F::Output>
82 where
83     F: Future,
84 {
85     F::poll(f, cx.0.as_mut())
86 }