You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We recommend using `async Fn` and `async ||` for async closures. This is more flexible than a closure returning a future for the reasons described elsewhere in this RFC.
128
+
We recommend using `async Fn()`/`async FnMut()`/`async FnOnce()` and `async ||` for async closures. This is more flexible than a closure returning a future for the reasons described elsewhere in this RFC.
129
129
130
130
Async closures act similarly to closures, and can have parts of their their signatures specified:
When called, they return an anonymous future type corresponding to the (not-yet-executed) body of the closure. These can be awaited like any other future.
144
144
145
+
The `async Fn` trait bound syntax can be used anywhere a trait bound is allowed, such as:
146
+
147
+
```rust
148
+
/// In return-position impl trait:
149
+
fnclosure() ->implasyncFn() { async|| {} }
150
+
151
+
/// In trait bounds:
152
+
traitFoo<F>:Sized
153
+
where
154
+
F:asyncFn()
155
+
{
156
+
fnnew(f:F) ->Self;
157
+
}
158
+
159
+
/// in GATs:
160
+
traitGat {
161
+
typeAsyncHasher<T>:asyncFn(T) ->i32;
162
+
}
163
+
```
164
+
145
165
# Detailed Explanation
146
166
147
167
### `AsyncFn*`
@@ -409,14 +429,18 @@ error[E0277]: cannot be sent between threads safely
409
429
| required by a bound introduced by this call
410
430
```
411
431
412
-
When the RTN RFC is accepted, this RFC specifies that users will be allowed to add RTN-like bounds to type parameters that are also bounded by `async Fn()`, like so:
432
+
With the acceptance of the RTN (return-type notation) [RFC 3654](https://github.com/rust-lang/rfcs/pull/3654), this RFC specifies that users will be allowed to add RTN-like bounds to type parameters that are also bounded by `async Fn()`. Concretely, this bound expands to bound both `CallOnceFuture` and `CallRefFuture` (if the latter exists):
413
433
414
434
```rust
415
435
asyncfnfoo(x:F) ->Result<()>
416
436
where
417
437
F:asyncFn(&str) ->Result<()>,
418
438
// The future from calling `F` is `Send` and `'static`.
419
439
F(..):Send+ 'static,
440
+
// Which expands to two bounds:
441
+
// `for<'a> <F as AsyncFnMut>::CallRefFuture<'a>: Send`
442
+
// `<F as AsyncFnOnce>::CallOnceFuture: Send`
443
+
// the latter is only if `F` is bounded with `async Fn` or `async FnMut`.
0 commit comments