core/future/
mod.rs

1#![stable(feature = "futures_api", since = "1.36.0")]
2
3//! Asynchronous basic functionality.
4//!
5//! Please see the fundamental [`async`] and [`await`] keywords and the [async book]
6//! for more information on asynchronous programming in Rust.
7//!
8//! [`async`]: ../../std/keyword.async.html
9//! [`await`]: ../../std/keyword.await.html
10//! [async book]: https://rust-lang.github.io/async-book/
11
12use crate::ptr::NonNull;
13use crate::task::Context;
14
15mod async_drop;
16mod future;
17mod into_future;
18mod join;
19mod pending;
20mod poll_fn;
21mod ready;
22
23#[cfg(not(bootstrap))]
24#[unstable(feature = "async_drop", issue = "126482")]
25pub use async_drop::{AsyncDrop, async_drop_in_place};
26#[stable(feature = "into_future", since = "1.64.0")]
27pub use into_future::IntoFuture;
28#[stable(feature = "future_readiness_fns", since = "1.48.0")]
29pub use pending::{Pending, pending};
30#[stable(feature = "future_poll_fn", since = "1.64.0")]
31pub use poll_fn::{PollFn, poll_fn};
32#[stable(feature = "future_readiness_fns", since = "1.48.0")]
33pub use ready::{Ready, ready};
34
35#[stable(feature = "futures_api", since = "1.36.0")]
36pub use self::future::Future;
37#[unstable(feature = "future_join", issue = "91642")]
38pub use self::join::join;
39
40/// This type is needed because:
41///
42/// a) Coroutines cannot implement `for<'a, 'b> Coroutine<&'a mut Context<'b>>`, so we need to pass
43///    a raw pointer (see <https://github.com/rust-lang/rust/issues/68923>).
44/// b) Raw pointers and `NonNull` aren't `Send` or `Sync`, so that would make every single future
45///    non-Send/Sync as well, and we don't want that.
46///
47/// It also simplifies the HIR lowering of `.await`.
48#[lang = "ResumeTy"]
49#[doc(hidden)]
50#[unstable(feature = "gen_future", issue = "none")]
51#[derive(Debug, Copy, Clone)]
52pub struct ResumeTy(NonNull<Context<'static>>);
53
54#[unstable(feature = "gen_future", issue = "none")]
55unsafe impl Send for ResumeTy {}
56
57#[unstable(feature = "gen_future", issue = "none")]
58unsafe impl Sync for ResumeTy {}
59
60#[lang = "get_context"]
61#[doc(hidden)]
62#[unstable(feature = "gen_future", issue = "none")]
63#[must_use]
64#[inline]
65pub unsafe fn get_context<'a, 'b>(cx: ResumeTy) -> &'a mut Context<'b> {
66    // SAFETY: the caller must guarantee that `cx.0` is a valid pointer
67    // that fulfills all the requirements for a mutable reference.
68    unsafe { &mut *cx.0.as_ptr().cast() }
69}