1use crate::future::Future;
2use crate::marker::Tuple;
34/// An async-aware version of the [`Fn`](crate::ops::Fn) trait.
5///
6/// All `async fn` and functions returning futures implement this trait.
7#[stable(feature = "async_closure", since = "1.85.0")]
8#[rustc_paren_sugar]
9#[must_use = "async closures are lazy and do nothing unless called"]
10#[lang = "async_fn"]
11pub trait AsyncFn<Args: Tuple>: AsyncFnMut<Args> {
12/// Call the [`AsyncFn`], returning a future which may borrow from the called closure.
13#[unstable(feature = "async_fn_traits", issue = "none")]
14extern "rust-call" fn async_call(&self, args: Args) -> Self::CallRefFuture<'_>;
15}
1617/// An async-aware version of the [`FnMut`](crate::ops::FnMut) trait.
18///
19/// All `async fn` and functions returning futures implement this trait.
20#[stable(feature = "async_closure", since = "1.85.0")]
21#[rustc_paren_sugar]
22#[must_use = "async closures are lazy and do nothing unless called"]
23#[lang = "async_fn_mut"]
24pub trait AsyncFnMut<Args: Tuple>: AsyncFnOnce<Args> {
25/// Future returned by [`AsyncFnMut::async_call_mut`] and [`AsyncFn::async_call`].
26#[unstable(feature = "async_fn_traits", issue = "none")]
27 #[lang = "call_ref_future"]
28type CallRefFuture<'a>: Future<Output = Self::Output>
29where
30Self: 'a;
3132/// Call the [`AsyncFnMut`], returning a future which may borrow from the called closure.
33#[unstable(feature = "async_fn_traits", issue = "none")]
34extern "rust-call" fn async_call_mut(&mut self, args: Args) -> Self::CallRefFuture<'_>;
35}
3637/// An async-aware version of the [`FnOnce`](crate::ops::FnOnce) trait.
38///
39/// All `async fn` and functions returning futures implement this trait.
40#[stable(feature = "async_closure", since = "1.85.0")]
41#[rustc_paren_sugar]
42#[must_use = "async closures are lazy and do nothing unless called"]
43#[lang = "async_fn_once"]
44pub trait AsyncFnOnce<Args: Tuple> {
45/// Future returned by [`AsyncFnOnce::async_call_once`].
46#[unstable(feature = "async_fn_traits", issue = "none")]
47 #[lang = "call_once_future"]
48type CallOnceFuture: Future<Output = Self::Output>;
4950/// Output type of the called closure's future.
51#[unstable(feature = "async_fn_traits", issue = "none")]
52 #[lang = "async_fn_once_output"]
53type Output;
5455/// Call the [`AsyncFnOnce`], returning a future which may move out of the called closure.
56#[unstable(feature = "async_fn_traits", issue = "none")]
57extern "rust-call" fn async_call_once(self, args: Args) -> Self::CallOnceFuture;
58}
5960mod impls {
61use super::{AsyncFn, AsyncFnMut, AsyncFnOnce};
62use crate::marker::Tuple;
6364#[stable(feature = "async_closure", since = "1.85.0")]
65impl<A: Tuple, F: ?Sized> AsyncFn<A> for &F
66where
67F: AsyncFn<A>,
68 {
69extern "rust-call" fn async_call(&self, args: A) -> Self::CallRefFuture<'_> {
70 F::async_call(*self, args)
71 }
72 }
7374#[stable(feature = "async_closure", since = "1.85.0")]
75impl<A: Tuple, F: ?Sized> AsyncFnMut<A> for &F
76where
77F: AsyncFn<A>,
78 {
79type CallRefFuture<'a>
80 = F::CallRefFuture<'a>
81where
82Self: 'a;
8384extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallRefFuture<'_> {
85 F::async_call(*self, args)
86 }
87 }
8889#[stable(feature = "async_closure", since = "1.85.0")]
90impl<'a, A: Tuple, F: ?Sized> AsyncFnOnce<A> for &'a F
91where
92F: AsyncFn<A>,
93 {
94type Output = F::Output;
95type CallOnceFuture = F::CallRefFuture<'a>;
9697extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture {
98 F::async_call(self, args)
99 }
100 }
101102#[stable(feature = "async_closure", since = "1.85.0")]
103impl<A: Tuple, F: ?Sized> AsyncFnMut<A> for &mut F
104where
105F: AsyncFnMut<A>,
106 {
107type CallRefFuture<'a>
108 = F::CallRefFuture<'a>
109where
110Self: 'a;
111112extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallRefFuture<'_> {
113 F::async_call_mut(*self, args)
114 }
115 }
116117#[stable(feature = "async_closure", since = "1.85.0")]
118impl<'a, A: Tuple, F: ?Sized> AsyncFnOnce<A> for &'a mut F
119where
120F: AsyncFnMut<A>,
121 {
122type Output = F::Output;
123type CallOnceFuture = F::CallRefFuture<'a>;
124125extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture {
126 F::async_call_mut(self, args)
127 }
128 }
129}
130131mod internal_implementation_detail {
132/// A helper trait that is used to enforce that the `ClosureKind` of a goal
133 /// is within the capabilities of a `CoroutineClosure`, and which allows us
134 /// to delay the projection of the tupled upvar types until after upvar
135 /// analysis is complete.
136 ///
137 /// The `Self` type is expected to be the `kind_ty` of the coroutine-closure,
138 /// and thus either `?0` or `i8`/`i16`/`i32` (see docs for `ClosureKind`
139 /// for an explanation of that). The `GoalKind` is also the same type, but
140 /// representing the kind of the trait that the closure is being called with.
141#[lang = "async_fn_kind_helper"]
142trait AsyncFnKindHelper<GoalKind> {
143// Projects a set of closure inputs (arguments), a region, and a set of upvars
144 // (by move and by ref) to the upvars that we expect the coroutine to have
145 // according to the `GoalKind` parameter above.
146 //
147 // The `Upvars` parameter should be the upvars of the parent coroutine-closure,
148 // and the `BorrowedUpvarsAsFnPtr` will be a function pointer that has the shape
149 // `for<'env> fn() -> (&'env T, ...)`. This allows us to represent the binder
150 // of the closure's self-capture, and these upvar types will be instantiated with
151 // the `'closure_env` region provided to the associated type.
152#[lang = "async_fn_kind_upvars"]
153type Upvars<'closure_env, Inputs, Upvars, BorrowedUpvarsAsFnPtr>;
154 }
155}