1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Asynchronous values.
14 use core::marker::Unpin;
15 use core::pin::PinMut;
16 use core::option::Option;
17 use core::ptr::NonNull;
18 use core::task::{self, Poll};
19 use core::ops::{Drop, Generator, GeneratorState};
22 pub use core::future::*;
24 /// Wrap a future in a generator.
26 /// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
27 /// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
28 #[unstable(feature = "gen_future", issue = "50547")]
29 pub fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
33 /// A wrapper around generators used to implement `Future` for `async`/`await` code.
34 #[unstable(feature = "gen_future", issue = "50547")]
35 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
36 struct GenFuture<T: Generator<Yield = ()>>(T);
38 // We rely on the fact that async/await futures are immovable in order to create
39 // self-referential borrows in the underlying generator.
40 impl<T: Generator<Yield = ()>> !Unpin for GenFuture<T> {}
42 #[unstable(feature = "gen_future", issue = "50547")]
43 impl<T: Generator<Yield = ()>> Future for GenFuture<T> {
44 type Output = T::Return;
45 fn poll(self: PinMut<Self>, cx: &mut task::Context) -> Poll<Self::Output> {
46 set_task_cx(cx, || match unsafe { PinMut::get_mut_unchecked(self).0.resume() } {
47 GeneratorState::Yielded(()) => Poll::Pending,
48 GeneratorState::Complete(x) => Poll::Ready(x),
54 static TLS_CX: Cell<Option<NonNull<task::Context<'static>>>> = Cell::new(None);
57 struct SetOnDrop(Option<NonNull<task::Context<'static>>>);
59 impl Drop for SetOnDrop {
61 TLS_CX.with(|tls_cx| {
62 tls_cx.set(self.0.take());
67 #[unstable(feature = "gen_future", issue = "50547")]
68 /// Sets the thread-local task context used by async/await futures.
69 pub fn set_task_cx<F, R>(cx: &mut task::Context, f: F) -> R
73 let old_cx = TLS_CX.with(|tls_cx| {
74 tls_cx.replace(NonNull::new(
78 as *mut task::Context<'static>
81 let _reset_cx = SetOnDrop(old_cx);
85 #[unstable(feature = "gen_future", issue = "50547")]
86 /// Retrieves the thread-local task context used by async/await futures.
88 /// This function acquires exclusive access to the task context.
90 /// Panics if no task has been set or if the task context has already been
91 /// retrieved by a surrounding call to get_task_cx.
92 pub fn get_task_cx<F, R>(f: F) -> R
94 F: FnOnce(&mut task::Context) -> R
96 let cx_ptr = TLS_CX.with(|tls_cx| {
97 // Clear the entry so that nested `with_get_cx` calls
98 // will fail or set their own value.
101 let _reset_cx = SetOnDrop(cx_ptr);
103 let mut cx_ptr = cx_ptr.expect(
104 "TLS task::Context not set. This is a rustc bug. \
105 Please file an issue on https://github.com/rust-lang/rust.");
106 unsafe { f(cx_ptr.as_mut()) }
109 #[unstable(feature = "gen_future", issue = "50547")]
110 /// Polls a future in the current thread-local task context.
111 pub fn poll_in_task_cx<F>(f: PinMut<F>) -> Poll<F::Output>
115 get_task_cx(|cx| f.poll(cx))