1 //! Asynchronous values.
4 use core::marker::Unpin;
6 use core::option::Option;
7 use core::ptr::NonNull;
8 use core::task::{LocalWaker, Poll};
9 use core::ops::{Drop, Generator, GeneratorState};
12 pub use core::future::*;
14 /// Wrap a generator in a future.
16 /// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
17 /// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
18 #[unstable(feature = "gen_future", issue = "50547")]
19 pub fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
23 /// A wrapper around generators used to implement `Future` for `async`/`await` code.
24 #[unstable(feature = "gen_future", issue = "50547")]
25 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
26 struct GenFuture<T: Generator<Yield = ()>>(T);
28 // We rely on the fact that async/await futures are immovable in order to create
29 // self-referential borrows in the underlying generator.
30 impl<T: Generator<Yield = ()>> !Unpin for GenFuture<T> {}
32 #[unstable(feature = "gen_future", issue = "50547")]
33 impl<T: Generator<Yield = ()>> Future for GenFuture<T> {
34 type Output = T::Return;
35 fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll<Self::Output> {
36 // Safe because we're !Unpin + !Drop mapping to a ?Unpin value
37 let gen = unsafe { Pin::map_unchecked_mut(self, |s| &mut s.0) };
38 set_task_waker(lw, || match gen.resume() {
39 GeneratorState::Yielded(()) => Poll::Pending,
40 GeneratorState::Complete(x) => Poll::Ready(x),
46 static TLS_WAKER: Cell<Option<NonNull<LocalWaker>>> = Cell::new(None);
49 struct SetOnDrop(Option<NonNull<LocalWaker>>);
51 impl Drop for SetOnDrop {
53 TLS_WAKER.with(|tls_waker| {
54 tls_waker.set(self.0.take());
59 #[unstable(feature = "gen_future", issue = "50547")]
60 /// Sets the thread-local task context used by async/await futures.
61 pub fn set_task_waker<F, R>(lw: &LocalWaker, f: F) -> R
65 let old_waker = TLS_WAKER.with(|tls_waker| {
66 tls_waker.replace(Some(NonNull::from(lw)))
68 let _reset_waker = SetOnDrop(old_waker);
72 #[unstable(feature = "gen_future", issue = "50547")]
73 /// Retrieves the thread-local task waker used by async/await futures.
75 /// This function acquires exclusive access to the task waker.
77 /// Panics if no waker has been set or if the waker has already been
78 /// retrieved by a surrounding call to get_task_waker.
79 pub fn get_task_waker<F, R>(f: F) -> R
81 F: FnOnce(&LocalWaker) -> R
83 let waker_ptr = TLS_WAKER.with(|tls_waker| {
84 // Clear the entry so that nested `get_task_waker` calls
85 // will fail or set their own value.
86 tls_waker.replace(None)
88 let _reset_waker = SetOnDrop(waker_ptr);
90 let waker_ptr = waker_ptr.expect(
91 "TLS LocalWaker not set. This is a rustc bug. \
92 Please file an issue on https://github.com/rust-lang/rust.");
93 unsafe { f(waker_ptr.as_ref()) }
96 #[unstable(feature = "gen_future", issue = "50547")]
97 /// Polls a future in the current thread-local task waker.
98 pub fn poll_with_tls_waker<F>(f: Pin<&mut F>) -> Poll<F::Output>
102 get_task_waker(|lw| F::poll(f, lw))