2019-04-05 21:14:19 +00:00
|
|
|
#![stable(feature = "futures_api", since = "1.36.0")]
|
2018-06-30 19:16:44 +00:00
|
|
|
|
2022-02-19 16:17:40 +00:00
|
|
|
//! Asynchronous basic functionality.
|
|
|
|
//!
|
|
|
|
//! Please see the fundamental [`async`] and [`await`] keywords and the [async book]
|
|
|
|
//! for more information on asynchronous programming in Rust.
|
|
|
|
//!
|
|
|
|
//! [`async`]: ../../std/keyword.async.html
|
|
|
|
//! [`await`]: ../../std/keyword.await.html
|
|
|
|
//! [async book]: https://rust-lang.github.io/async-book/
|
2018-06-30 19:16:44 +00:00
|
|
|
|
2022-11-18 21:56:22 +00:00
|
|
|
use crate::ptr::NonNull;
|
|
|
|
use crate::task::Context;
|
2020-02-10 16:21:50 +00:00
|
|
|
|
2018-06-30 19:16:44 +00:00
|
|
|
mod future;
|
2020-05-22 08:07:46 +00:00
|
|
|
mod into_future;
|
2021-12-08 01:59:16 +00:00
|
|
|
mod join;
|
2020-04-06 07:24:44 +00:00
|
|
|
mod pending;
|
2020-05-17 17:44:56 +00:00
|
|
|
mod poll_fn;
|
2020-04-06 07:24:44 +00:00
|
|
|
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
|
|
|
|
2021-12-08 01:59:16 +00:00
|
|
|
#[unstable(feature = "future_join", issue = "91642")]
|
|
|
|
pub use self::join::join;
|
|
|
|
|
2022-06-30 14:54:03 +00:00
|
|
|
#[stable(feature = "into_future", since = "1.64.0")]
|
2020-05-22 08:07:46 +00:00
|
|
|
pub use into_future::IntoFuture;
|
|
|
|
|
2020-09-15 21:12:08 +00:00
|
|
|
#[stable(feature = "future_readiness_fns", since = "1.48.0")]
|
2020-04-06 07:24:44 +00:00
|
|
|
pub use pending::{pending, Pending};
|
2020-09-15 21:12:08 +00:00
|
|
|
#[stable(feature = "future_readiness_fns", since = "1.48.0")]
|
2020-04-06 07:24:44 +00:00
|
|
|
pub use ready::{ready, Ready};
|
|
|
|
|
2022-07-16 01:04:14 +00:00
|
|
|
#[stable(feature = "future_poll_fn", since = "1.64.0")]
|
2020-05-17 17:44:56 +00:00
|
|
|
pub use poll_fn::{poll_fn, PollFn};
|
|
|
|
|
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-11-05 13:33:23 +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`.
|
2022-12-04 11:32:15 +00:00
|
|
|
// FIXME(swatinem): This type can be removed when bumping the bootstrap compiler
|
2020-02-10 16:21:50 +00:00
|
|
|
#[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`
|
2022-12-04 11:32:15 +00:00
|
|
|
// FIXME(swatinem): This fn can be removed when bumping the bootstrap compiler
|
2022-11-18 21:56:22 +00:00
|
|
|
#[cfg_attr(bootstrap, lang = "from_generator")]
|
2020-02-10 16:21:50 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[unstable(feature = "gen_future", issue = "50547")]
|
2020-09-17 18:02:56 +00:00
|
|
|
#[rustc_const_unstable(feature = "gen_future", issue = "50547")]
|
2020-02-10 16:21:50 +00:00
|
|
|
#[inline]
|
|
|
|
pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
|
|
|
|
where
|
2022-11-18 21:56:22 +00:00
|
|
|
T: crate::ops::Generator<ResumeTy, Yield = ()>,
|
2020-02-10 16:21:50 +00:00
|
|
|
{
|
2022-11-18 21:56:22 +00:00
|
|
|
use crate::{
|
|
|
|
ops::{Generator, GeneratorState},
|
|
|
|
pin::Pin,
|
|
|
|
task::Poll,
|
|
|
|
};
|
|
|
|
|
2020-05-24 01:40:55 +00:00
|
|
|
#[rustc_diagnostic_item = "gen_future"]
|
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;
|
2022-11-08 23:45:55 +00:00
|
|
|
#[track_caller]
|
2020-02-10 16:21:50 +00:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2020-09-09 02:26:44 +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)
|
|
|
|
}
|
|
|
|
|
2022-12-04 11:32:15 +00:00
|
|
|
// FIXME(swatinem): This fn can be removed when bumping the bootstrap compiler
|
|
|
|
#[cfg_attr(bootstrap, lang = "get_context")]
|
2020-02-10 16:21:50 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[unstable(feature = "gen_future", issue = "50547")]
|
2021-10-14 22:54:55 +00:00
|
|
|
#[must_use]
|
2020-02-10 16:21:50 +00:00
|
|
|
#[inline]
|
2020-04-06 00:55:19 +00:00
|
|
|
pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> {
|
2020-06-30 17:10:22 +00:00
|
|
|
// SAFETY: the caller must guarantee that `cx.0` is a valid pointer
|
|
|
|
// that fulfills all the requirements for a mutable reference.
|
|
|
|
unsafe { &mut *cx.0.as_ptr().cast() }
|
2020-02-10 16:21:50 +00:00
|
|
|
}
|
2022-11-18 21:56:22 +00:00
|
|
|
|
2022-12-04 11:32:15 +00:00
|
|
|
// FIXME(swatinem): This fn is currently needed to work around shortcomings
|
|
|
|
// in type and lifetime inference.
|
|
|
|
// See the comment at the bottom of `LoweringContext::make_async_expr` and
|
|
|
|
// <https://github.com/rust-lang/rust/issues/104826>.
|
2022-11-18 21:56:22 +00:00
|
|
|
#[cfg_attr(not(bootstrap), lang = "identity_future")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[unstable(feature = "gen_future", issue = "50547")]
|
|
|
|
#[inline]
|
|
|
|
pub const fn identity_future<O, Fut: Future<Output = O>>(f: Fut) -> Fut {
|
|
|
|
f
|
|
|
|
}
|