2018-06-22 18:36:01 +00:00
|
|
|
//! Asynchronous values.
|
2018-06-06 22:50:59 +00:00
|
|
|
|
|
|
|
use core::cell::Cell;
|
|
|
|
use core::marker::Unpin;
|
2018-09-01 04:12:10 +00:00
|
|
|
use core::pin::Pin;
|
2018-06-06 22:50:59 +00:00
|
|
|
use core::option::Option;
|
|
|
|
use core::ptr::NonNull;
|
2019-03-11 23:56:00 +00:00
|
|
|
use core::task::{Context, Poll};
|
2018-06-06 22:50:59 +00:00
|
|
|
use core::ops::{Drop, Generator, GeneratorState};
|
|
|
|
|
2018-06-22 18:36:01 +00:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use core::future::*;
|
2018-06-06 22:50:59 +00:00
|
|
|
|
2019-01-23 22:39:26 +00:00
|
|
|
/// Wrap a generator in a future.
|
2018-06-06 22:50:59 +00:00
|
|
|
///
|
|
|
|
/// 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")]
|
2018-06-22 23:08:07 +00:00
|
|
|
pub fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
|
2018-06-06 22:50:59 +00:00
|
|
|
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: Generator<Yield = ()>>(T);
|
|
|
|
|
|
|
|
// We rely on the fact that async/await futures are immovable in order to create
|
|
|
|
// self-referential borrows in the underlying generator.
|
|
|
|
impl<T: Generator<Yield = ()>> !Unpin for GenFuture<T> {}
|
|
|
|
|
|
|
|
#[unstable(feature = "gen_future", issue = "50547")]
|
|
|
|
impl<T: Generator<Yield = ()>> Future for GenFuture<T> {
|
|
|
|
type Output = T::Return;
|
2019-03-11 23:56:00 +00:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2018-10-04 18:49:38 +00:00
|
|
|
// Safe because we're !Unpin + !Drop mapping to a ?Unpin value
|
|
|
|
let gen = unsafe { Pin::map_unchecked_mut(self, |s| &mut s.0) };
|
2019-03-11 23:56:00 +00:00
|
|
|
set_task_context(cx, || match gen.resume() {
|
2018-06-06 22:50:59 +00:00
|
|
|
GeneratorState::Yielded(()) => Poll::Pending,
|
|
|
|
GeneratorState::Complete(x) => Poll::Ready(x),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_local! {
|
2019-03-11 23:56:00 +00:00
|
|
|
static TLS_CX: Cell<Option<NonNull<Context<'static>>>> = Cell::new(None);
|
2018-06-06 22:50:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 23:56:00 +00:00
|
|
|
struct SetOnDrop(Option<NonNull<Context<'static>>>);
|
2018-06-06 22:50:59 +00:00
|
|
|
|
|
|
|
impl Drop for SetOnDrop {
|
|
|
|
fn drop(&mut self) {
|
2019-03-11 23:56:00 +00:00
|
|
|
TLS_CX.with(|tls_cx| {
|
|
|
|
tls_cx.set(self.0.take());
|
2018-06-06 22:50:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "gen_future", issue = "50547")]
|
2018-06-22 18:36:01 +00:00
|
|
|
/// Sets the thread-local task context used by async/await futures.
|
2019-03-11 23:56:00 +00:00
|
|
|
pub fn set_task_context<F, R>(cx: &mut Context<'_>, f: F) -> R
|
2018-06-06 22:50:59 +00:00
|
|
|
where
|
|
|
|
F: FnOnce() -> R
|
|
|
|
{
|
2019-03-11 23:56:00 +00:00
|
|
|
// transmute the context's lifetime to 'static so we can store it.
|
|
|
|
let cx = unsafe {
|
|
|
|
core::mem::transmute::<&mut Context<'_>, &mut Context<'static>>(cx)
|
|
|
|
};
|
|
|
|
let old_cx = TLS_CX.with(|tls_cx| {
|
|
|
|
tls_cx.replace(Some(NonNull::from(cx)))
|
2018-06-06 22:50:59 +00:00
|
|
|
});
|
2019-03-11 23:56:00 +00:00
|
|
|
let _reset = SetOnDrop(old_cx);
|
2018-06-22 23:08:07 +00:00
|
|
|
f()
|
2018-06-06 22:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "gen_future", issue = "50547")]
|
2019-03-11 23:56:00 +00:00
|
|
|
/// Retrieves the thread-local task context used by async/await futures.
|
2018-06-22 18:36:01 +00:00
|
|
|
///
|
2019-03-11 23:56:00 +00:00
|
|
|
/// This function acquires exclusive access to the task context.
|
2018-06-22 23:08:07 +00:00
|
|
|
///
|
2019-03-11 23:56:00 +00:00
|
|
|
/// Panics if no context has been set or if the context has already been
|
|
|
|
/// retrieved by a surrounding call to get_task_context.
|
|
|
|
pub fn get_task_context<F, R>(f: F) -> R
|
2018-06-06 22:50:59 +00:00
|
|
|
where
|
2019-03-11 23:56:00 +00:00
|
|
|
F: FnOnce(&mut Context<'_>) -> R
|
2018-06-06 22:50:59 +00:00
|
|
|
{
|
2019-03-11 23:56:00 +00:00
|
|
|
let cx_ptr = TLS_CX.with(|tls_cx| {
|
2018-09-19 00:50:32 +00:00
|
|
|
// Clear the entry so that nested `get_task_waker` calls
|
2018-06-06 22:50:59 +00:00
|
|
|
// will fail or set their own value.
|
2019-03-11 23:56:00 +00:00
|
|
|
tls_cx.replace(None)
|
2018-06-06 22:50:59 +00:00
|
|
|
});
|
2019-03-11 23:56:00 +00:00
|
|
|
let _reset = SetOnDrop(cx_ptr);
|
2018-06-06 22:50:59 +00:00
|
|
|
|
2019-03-11 23:56:00 +00:00
|
|
|
let mut cx_ptr = cx_ptr.expect(
|
|
|
|
"TLS Context not set. This is a rustc bug. \
|
2018-06-06 22:50:59 +00:00
|
|
|
Please file an issue on https://github.com/rust-lang/rust.");
|
2019-03-11 23:56:00 +00:00
|
|
|
|
|
|
|
// Safety: we've ensured exclusive access to the context by
|
|
|
|
// removing the pointer from TLS, only to be replaced once
|
|
|
|
// we're done with it.
|
|
|
|
//
|
|
|
|
// The pointer that was inserted came from an `&mut Context<'_>`,
|
|
|
|
// so it is safe to treat as mutable.
|
|
|
|
unsafe { f(cx_ptr.as_mut()) }
|
2018-06-06 22:50:59 +00:00
|
|
|
}
|
2018-06-22 23:08:07 +00:00
|
|
|
|
|
|
|
#[unstable(feature = "gen_future", issue = "50547")]
|
2018-09-19 00:50:32 +00:00
|
|
|
/// Polls a future in the current thread-local task waker.
|
2019-03-11 23:56:00 +00:00
|
|
|
pub fn poll_with_tls_context<F>(f: Pin<&mut F>) -> Poll<F::Output>
|
2018-06-22 23:08:07 +00:00
|
|
|
where
|
|
|
|
F: Future
|
|
|
|
{
|
2019-03-11 23:56:00 +00:00
|
|
|
get_task_context(|cx| F::poll(f, cx))
|
2018-06-22 23:08:07 +00:00
|
|
|
}
|