rust/src/libcore/future/mod.rs

85 lines
2.9 KiB
Rust
Raw Normal View History

2019-04-05 21:14:19 +00:00
#![stable(feature = "futures_api", since = "1.36.0")]
2018-06-30 19:16:44 +00:00
//! Asynchronous values.
2020-02-10 16:21:50 +00:00
use crate::{
ops::{Generator, GeneratorState},
pin::Pin,
ptr::NonNull,
task::{Context, Poll},
};
2018-06-30 19:16:44 +00:00
mod future;
2020-04-06 07:24:44 +00:00
mod pending;
mod ready;
2019-04-05 21:14:19 +00:00
#[stable(feature = "futures_api", since = "1.36.0")]
2018-06-30 19:16:44 +00:00
pub use self::future::Future;
2020-02-10 16:21:50 +00:00
2020-04-06 07:24:44 +00:00
#[unstable(feature = "future_readiness_fns", issue = "70921")]
pub use pending::{pending, Pending};
#[unstable(feature = "future_readiness_fns", issue = "70921")]
pub use ready::{ready, Ready};
2020-02-10 16:21:50 +00:00
/// This type is needed because:
///
/// a) Generators cannot implement `for<'a, 'b> Generator<&'a mut Context<'b>>`, so we need to pass
2020-03-17 21:19:11 +00:00
/// a raw pointer (see https://github.com/rust-lang/rust/issues/68923).
2020-02-10 16:21:50 +00:00
/// b) Raw pointers and `NonNull` aren't `Send` or `Sync`, so that would make every single future
/// non-Send/Sync as well, and we don't want that.
///
/// It also simplifies the HIR lowering of `.await`.
#[doc(hidden)]
#[unstable(feature = "gen_future", issue = "50547")]
#[derive(Debug, Copy, Clone)]
2020-03-03 15:05:11 +00:00
pub struct ResumeTy(NonNull<Context<'static>>);
2020-02-10 16:21:50 +00:00
#[unstable(feature = "gen_future", issue = "50547")]
unsafe impl Send for ResumeTy {}
#[unstable(feature = "gen_future", issue = "50547")]
unsafe impl Sync for ResumeTy {}
/// Wrap a generator in a future.
///
/// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
/// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
// This is `const` to avoid extra errors after we recover from `const async fn`
#[doc(hidden)]
#[unstable(feature = "gen_future", issue = "50547")]
#[inline]
pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
where
2020-02-10 17:20:22 +00:00
T: Generator<ResumeTy, Yield = ()>,
2020-02-10 16:21:50 +00:00
{
struct GenFuture<T: Generator<ResumeTy, 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<ResumeTy, Yield = ()>> !Unpin for GenFuture<T> {}
impl<T: Generator<ResumeTy, Yield = ()>> Future for GenFuture<T> {
type Output = T::Return;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
2020-03-03 16:04:35 +00:00
// Safety: Safe because we're !Unpin + !Drop, and this is just a field projection.
2020-02-10 16:21:50 +00:00
let gen = unsafe { Pin::map_unchecked_mut(self, |s| &mut s.0) };
// Resume the generator, turning the `&mut Context` into a `NonNull` raw pointer. The
// `.await` lowering will safely cast that back to a `&mut Context`.
match gen.resume(ResumeTy(NonNull::from(cx).cast::<Context<'static>>())) {
GeneratorState::Yielded(()) => Poll::Pending,
GeneratorState::Complete(x) => Poll::Ready(x),
}
}
}
GenFuture(gen)
}
#[doc(hidden)]
#[unstable(feature = "gen_future", issue = "50547")]
#[inline]
2020-04-06 00:55:19 +00:00
pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> {
&mut *cx.0.as_ptr().cast()
2020-02-10 16:21:50 +00:00
}