From b75327cec6ab9d6446ad49a467324991df7ad92c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 14 Aug 2024 13:58:28 -0400 Subject: [PATCH] Add an example of a higher ranked closure returning boxed future --- .../2024-08-09-async-closures-call-for-testing.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/posts/inside-rust/2024-08-09-async-closures-call-for-testing.md b/posts/inside-rust/2024-08-09-async-closures-call-for-testing.md index 0e77b9dbd..94f94ff62 100644 --- a/posts/inside-rust/2024-08-09-async-closures-call-for-testing.md +++ b/posts/inside-rust/2024-08-09-async-closures-call-for-testing.md @@ -34,7 +34,19 @@ where F: FnOnce() -> Pin>>; ``` -This led to an additional limitation, that it's impossible to express higher-ranked async fn bounds without boxing, since a higher-ranked trait bound on `F` cannot lead to a higher-ranked type for `Fut`. +This led to an additional limitation, that it's impossible to express higher-ranked async fn bounds using this without boxing, leading to unnecessary allocations: + +```rust +fn async_callback(callback: F) +where + F: FnOnce(&str) -> Pin + '_>>; + +async fn do_something(name: &str) {} + +async_callback(|name| Box::pin(async { + do_something(name).await; +})); +``` These limitations were detailed in [Niko's blog post on async closures and lending](https://smallcultfollowing.com/babysteps/blog/2023/05/09/giving-lending-and-async-closures/#async-closures-are-a-lending-pattern), and later in compiler-errors's blog post on [why async closures are the way they are](https://hackmd.io/@compiler-errors/async-closures).