Skip to content

Commit 86fc14a

Browse files
Motivate why non-async closures should implement AsyncFn
1 parent 3938f65 commit 86fc14a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

text/3668-async-closure.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,28 @@ let _: Box<dyn async Fn()> = todo!();
274274

275275
All currently-stable callable types (i.e., closures, function items, function pointers, and `dyn Fn*` trait objects) automatically implement `async Fn*() -> T` if they implement `Fn*() -> Fut` for some output type `Fut`, and `Fut` implements `Future<Output = T>`.
276276

277-
Async closures also implement `async Fn*()`, but their relationship to this trait is detailed later in the RFC.
277+
This is to make sure that `async Fn*()` trait bounds have maximum compatibility with existing callable types which return futures, such as async function items and closures which return boxed futures. These implementations are built-in, but can conceptually be understood as:
278278

279-
Some stable types that would implement `async Fn()` today include, e.g.:
279+
```rust
280+
impl<F, Args, Fut, T> AsyncFnOnce<Args> for F
281+
where
282+
F: FnOnce<A, Output = Fut>,
283+
Fut: Future<Output = T>,
284+
{
285+
type Output = T;
286+
type CallOnceFuture = Fut;
287+
288+
fn async_call_once(self, args: Args) -> Self::CallOnceFuture {
289+
FnOnce::call_once(self, args)
290+
}
291+
}
292+
```
293+
294+
And similarly for `AsyncFnMut` and `AsyncFn`, with the appropriate `FnMut` and `Fn` trait bounds, respectively.
295+
296+
Async closures also implement `async Fn*()`, but their relationship to this trait is detailed later in the RFC. The reason that all of these bounds (for regular callable types *and* async closures) are built-in is because these blanket impls would overlap with the built-in implementation of `AsyncFn*` for async closures, which must have distinct implementations to support self-borrowing futures.
297+
298+
Some stable types that implement `async Fn()` today include, e.g.:
280299

281300
```rust!
282301
// Async functions:

0 commit comments

Comments
 (0)