error: implementation of `Send` is not general enough
--> src/main.rs:24:5
|
24 | / tokio::spawn(async move {
25 | | let mut my_struct = MyStruct {};
26 | | let my_obj = MyObj {};
27 | | my_obj.outer(&mut my_struct).await;
28 | | })
| |______^ implementation of `Send` is not general enough
|
= note: `Send` would have to be implemented for the type `&'0 MyStruct`, for any lifetime `'0`...
= note: ...but `Send` is actually implemented for the type `&'1 MyStruct`, for some specific lifetime `'1`
The error message is wrong. The problem is not anything to do with MyStruct, but rather that,
tokio::spawn() requires the provided future to be Send, but
my_closure: &mut impl AsyncFnMut() does not constrain the future it produces to be Send.
Unfortunately, there is no way to write such a bound using the newly introduced AsyncFnMut trait. You can do it, however, with pre-1.85 techniques like the async_fn_traits library; this compiles: