Open
Description
I tried this code:
use tokio::sync::mpsc;
pub async fn main() {
tokio::spawn(async move { run().await });
}
pub async fn run() {
let (tx, _rx) = mpsc::channel(12);
let f = async || {
tx.send(()).await;
};
f().await; // ok
run_async_closure(f).await; // error
}
pub async fn run_async_closure(mut f: impl AsyncFnMut()) {
f().await;
}
I expected to see this happen: no error as this is a very normal use case of async closure.
Instead, this happened: compiler complaining about the lifetime of the tokio mpsc::Sender
.
Meta
rustc --version --verbose
:
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: aarch64-apple-darwin
release: 1.86.0
LLVM version: 19.1.7
Backtrace
error: implementation of `Send` is not general enough
--> src/lib.rs:4:5
|
4 | tokio::spawn(async move { run().await });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Send` is not general enough
|
= note: `Send` would have to be implemented for the type `&'0 tokio::sync::mpsc::Sender<()>`, for any lifetime `'0`...
= note: ...but `Send` is actually implemented for the type `&'1 tokio::sync::mpsc::Sender<()>`, for some specific lifetime `'1`
error: could not compile `test_async_closure` (lib) due to 1 previous error