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
+42-7
Original file line number
Diff line number
Diff line change
@@ -46,9 +46,9 @@ Recent [work](https://github.com/rust-lang/rust/pull/120361) has focused on reim
46
46
fntest<F>(callback:F)
47
47
where
48
48
// Either:
49
-
asyncFn(Arg, Arg) ->Ret
49
+
asyncFn(Arg, Arg) ->Ret,
50
50
// Or:
51
-
AsyncFn(Arg, Arg) ->Ret
51
+
AsyncFn(Arg, Arg) ->Ret,
52
52
```
53
53
54
54
(It'scurrentlyan [openquestion](https://github.com/rust-lang/rust/issues/128129) exactly how to spell this bound, so both syntaxes are implemented in parallel.)
@@ -77,16 +77,31 @@ And on the callee side, write async fn trait bounds instead of writing "regular"
Or if you're emulating a higher-ranked async closure with boxing:
93
+
94
+
```rust
95
+
// Instead of writing:
96
+
fnhigher_ranked<F>(callback:F)
97
+
where
98
+
F:Fn(&Arg) ->Pin<Box<dynFuture<Output= ()> + '_>>
99
+
{ todo!() }
100
+
101
+
// Write this:
102
+
fnhigher_ranked<F:asyncFn(&Arg)> { todo!() }
103
+
// Or this:
104
+
fnhigher_ranked<F:AsyncFn(&Arg)> { todo!() }
90
105
```
91
106
92
107
## Shortcomings interacting with the async ecosystem
@@ -98,7 +113,7 @@ If you're going to try to rewrite your async projects, there are a few shortcomi
98
113
When you name an async callable bound with the *old* style, before first-class async fn trait bounds, then as a side-effect of needing to use two type parameters, you can put additional bounds (e.g. `+ Send` or `+ 'static`) on the `Future` part of the bound, like:
99
114
100
115
```rust
101
-
fnasync_callback<F, Fut>()
116
+
fnasync_callback<F, Fut>(callback:F)
102
117
where
103
118
F:FnOnce() ->Fut,
104
119
Fut:Future<Output=String> +Send+ 'static
@@ -119,6 +134,26 @@ We expect to improve async closure signature inference as we move forward.
119
134
120
135
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.
121
136
122
-
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.
137
+
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