AsyncFnMut capturing the environment is not Send

⚓ Rust    📅 2025-09-10    👤 surdeus    👁️ 9      

surdeus

Warning

This post was published 64 days ago. The information described in this article may have changed.

When an async closure mutably capturing the environment is passed as AsyncFnMut, the Send bound is removed.

The following code does not compile, but it works when either removing the Send bound in trait Foo, or when removing the exec call. Can this example be fixed keeping Send and still passing an AsyncFnMut?

trait Foo {
    fn foo() -> impl Future<Output = ()> + Send;
}

async fn exec<F: AsyncFnMut() -> ()>(mut f: F) {
    f().await
}

impl Foo for () {
    async fn foo() {
        let mut counter = 0;
        let mut f = async || {
            counter += 1;
        };

        f().await;
        f().await;
        exec(f).await;
    }
}

4 posts - 2 participants

Read full topic

🏷️ Rust_feed