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
Copy file name to clipboardExpand all lines: posts/inside-rust/2024-08-09-async-closures-call-for-testing.md
+3-8Lines changed: 3 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -20,21 +20,16 @@ let x = || { async {} };
20
20
21
21
This had a fundamental limitation that it was impossible to express a closure that returns a future that borrows captured state.
22
22
23
-
Somewhat relatedly, on the callee side, when users want to take an async closure as an argument, they must express that as a bound of two different generic types, or use boxing:
23
+
Somewhat relatedly, on the callee side, when users want to take an async closure as an argument, they typically express that as a bound of two different generic types:
24
24
25
25
```rust
26
26
fnasync_callback<F, Fut>(callback:F)
27
27
where
28
28
F:FnOnce() ->Fut,
29
29
Fut:Future<Output=String>;
30
-
31
-
// Or:
32
-
fnasync_callback_boxed<F, Fut>(callback:F)
33
-
where
34
-
F:FnOnce() ->Pin<Box<dynFuture<Output=String>>>;
35
30
```
36
31
37
-
This led to an additional limitation, that it's impossible to express higher-ranked async fn bounds using this without boxing (since a higher-ranked trait bound on `F` cannot lead to a higher-ranked type for `Fut`), leading to unnecessary allocations:
32
+
This also led to an additional limitation that it's impossible to express higher-ranked async fn bounds using this without boxing (since a higher-ranked trait bound on `F` cannot lead to a higher-ranked type for `Fut`), leading to unnecessary allocations:
38
33
39
34
```rust
40
35
fnasync_callback<F, Fut>(callback:F)
@@ -146,7 +141,7 @@ We expect to improve async closure signature inference as we move forward.
146
141
147
142
Some libraries take their callbacks as function *pointers* (`fn()`) rather than generics. Async closures don't currently implement the same coercion from closure to `fn() -> ...`. Some libraries may mitigate this problem by adapting their API to take generic `impl Fn()` instead of `fn()` pointers as an argument.
148
143
149
-
We don't expect to implement this coercion unless there's a particularly good reason to support it, since this can usually be handled manually by the caller by using an inner function item, or by using an `Fn` bound instead: for example:
144
+
We don't expect to implement this coercion unless there's a particularly good reason to support it, since this can usually be handled manually by the caller by using an inner function item, or by using an `Fn` bound instead, for example:
0 commit comments