1 | use crate::future::Future; |
2 | use crate::marker::Tuple; |
3 | |
4 | /// An async-aware version of the [`Fn`](crate::ops::Fn) trait. |
5 | /// |
6 | /// All `async fn` and functions returning futures implement this trait. |
7 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
8 | #[rustc_paren_sugar ] |
9 | #[fundamental ] |
10 | #[must_use = "async closures are lazy and do nothing unless called" ] |
11 | #[cfg_attr (not(bootstrap), lang = "async_fn" )] |
12 | pub trait AsyncFn<Args: Tuple>: AsyncFnMut<Args> { |
13 | /// Future returned by [`AsyncFn::async_call`]. |
14 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
15 | type CallFuture<'a>: Future<Output = Self::Output> |
16 | where |
17 | Self: 'a; |
18 | |
19 | /// Call the [`AsyncFn`], returning a future which may borrow from the called closure. |
20 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
21 | extern "rust-call" fn async_call(&self, args: Args) -> Self::CallFuture<'_>; |
22 | } |
23 | |
24 | /// An async-aware version of the [`FnMut`](crate::ops::FnMut) trait. |
25 | /// |
26 | /// All `async fn` and functions returning futures implement this trait. |
27 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
28 | #[rustc_paren_sugar ] |
29 | #[fundamental ] |
30 | #[must_use = "async closures are lazy and do nothing unless called" ] |
31 | #[cfg_attr (not(bootstrap), lang = "async_fn_mut" )] |
32 | pub trait AsyncFnMut<Args: Tuple>: AsyncFnOnce<Args> { |
33 | /// Future returned by [`AsyncFnMut::async_call_mut`]. |
34 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
35 | type CallMutFuture<'a>: Future<Output = Self::Output> |
36 | where |
37 | Self: 'a; |
38 | |
39 | /// Call the [`AsyncFnMut`], returning a future which may borrow from the called closure. |
40 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
41 | extern "rust-call" fn async_call_mut(&mut self, args: Args) -> Self::CallMutFuture<'_>; |
42 | } |
43 | |
44 | /// An async-aware version of the [`FnOnce`](crate::ops::FnOnce) trait. |
45 | /// |
46 | /// All `async fn` and functions returning futures implement this trait. |
47 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
48 | #[rustc_paren_sugar ] |
49 | #[fundamental ] |
50 | #[must_use = "async closures are lazy and do nothing unless called" ] |
51 | #[cfg_attr (not(bootstrap), lang = "async_fn_once" )] |
52 | pub trait AsyncFnOnce<Args: Tuple> { |
53 | /// Future returned by [`AsyncFnOnce::async_call_once`]. |
54 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
55 | type CallOnceFuture: Future<Output = Self::Output>; |
56 | |
57 | /// Output type of the called closure's future. |
58 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
59 | type Output; |
60 | |
61 | /// Call the [`AsyncFnOnce`], returning a future which may move out of the called closure. |
62 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
63 | extern "rust-call" fn async_call_once(self, args: Args) -> Self::CallOnceFuture; |
64 | } |
65 | |
66 | mod impls { |
67 | use super::{AsyncFn, AsyncFnMut, AsyncFnOnce}; |
68 | use crate::future::Future; |
69 | use crate::marker::Tuple; |
70 | |
71 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
72 | impl<F: Fn<A>, A: Tuple> AsyncFn<A> for F |
73 | where |
74 | <F as FnOnce<A>>::Output: Future, |
75 | { |
76 | type CallFuture<'a> = <F as FnOnce<A>>::Output where Self: 'a; |
77 | |
78 | extern "rust-call" fn async_call(&self, args: A) -> Self::CallFuture<'_> { |
79 | self.call(args) |
80 | } |
81 | } |
82 | |
83 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
84 | impl<F: FnMut<A>, A: Tuple> AsyncFnMut<A> for F |
85 | where |
86 | <F as FnOnce<A>>::Output: Future, |
87 | { |
88 | type CallMutFuture<'a> = <F as FnOnce<A>>::Output where Self: 'a; |
89 | |
90 | extern "rust-call" fn async_call_mut(&mut self, args: A) -> Self::CallMutFuture<'_> { |
91 | self.call_mut(args) |
92 | } |
93 | } |
94 | |
95 | #[unstable (feature = "async_fn_traits" , issue = "none" )] |
96 | impl<F: FnOnce<A>, A: Tuple> AsyncFnOnce<A> for F |
97 | where |
98 | <F as FnOnce<A>>::Output: Future, |
99 | { |
100 | type CallOnceFuture = <F as FnOnce<A>>::Output; |
101 | |
102 | type Output = <<F as FnOnce<A>>::Output as Future>::Output; |
103 | |
104 | extern "rust-call" fn async_call_once(self, args: A) -> Self::CallOnceFuture { |
105 | self.call_once(args) |
106 | } |
107 | } |
108 | } |
109 | |