// Copyright 2018 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! Asynchronous values. use core::cell::Cell; use core::marker::Unpin; use core::pin::Pin; use core::option::Option; use core::ptr::NonNull; use core::task::{LocalWaker, Poll}; use core::ops::{Drop, Generator, GeneratorState}; #[doc(inline)] pub use core::future::*; /// Wrap a future in a generator. /// /// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give /// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`). #[unstable(feature = "gen_future", issue = "50547")] pub fn from_generator>(x: T) -> impl Future { GenFuture(x) } /// A wrapper around generators used to implement `Future` for `async`/`await` code. #[unstable(feature = "gen_future", issue = "50547")] #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] struct GenFuture>(T); // We rely on the fact that async/await futures are immovable in order to create // self-referential borrows in the underlying generator. impl> !Unpin for GenFuture {} #[unstable(feature = "gen_future", issue = "50547")] impl> Future for GenFuture { type Output = T::Return; fn poll(self: Pin<&mut Self>, lw: &LocalWaker) -> Poll { set_task_waker(lw, || match unsafe { Pin::get_mut_unchecked(self).0.resume() } { GeneratorState::Yielded(()) => Poll::Pending, GeneratorState::Complete(x) => Poll::Ready(x), }) } } thread_local! { static TLS_WAKER: Cell>> = Cell::new(None); } struct SetOnDrop(Option>); impl Drop for SetOnDrop { fn drop(&mut self) { TLS_WAKER.with(|tls_waker| { tls_waker.set(self.0.take()); }); } } #[unstable(feature = "gen_future", issue = "50547")] /// Sets the thread-local task context used by async/await futures. pub fn set_task_waker(lw: &LocalWaker, f: F) -> R where F: FnOnce() -> R { let old_waker = TLS_WAKER.with(|tls_waker| { tls_waker.replace(Some(NonNull::from(lw))) }); let _reset_waker = SetOnDrop(old_waker); f() } #[unstable(feature = "gen_future", issue = "50547")] /// Retrieves the thread-local task waker used by async/await futures. /// /// This function acquires exclusive access to the task waker. /// /// Panics if no waker has been set or if the waker has already been /// retrieved by a surrounding call to get_task_waker. pub fn get_task_waker(f: F) -> R where F: FnOnce(&LocalWaker) -> R { let waker_ptr = TLS_WAKER.with(|tls_waker| { // Clear the entry so that nested `get_task_waker` calls // will fail or set their own value. tls_waker.replace(None) }); let _reset_waker = SetOnDrop(waker_ptr); let mut waker_ptr = waker_ptr.expect( "TLS LocalWaker not set. This is a rustc bug. \ Please file an issue on https://github.com/rust-lang/rust."); unsafe { f(waker_ptr.as_mut()) } } #[unstable(feature = "gen_future", issue = "50547")] /// Polls a future in the current thread-local task waker. pub fn poll_with_tls_waker(f: Pin<&mut F>) -> Poll where F: Future { get_task_waker(|lw| F::poll(f, lw)) }