Hi, I'm trying to have a struct with multiple mutable fields at the same time, for example -
struct ToBeMutable {
a: Foo,
b: Bar,
}
where both a & b can be mutable at the same time, but I understand that it can't be done directly as I'll end up having multiple mutable reference of ToBeMutable
.
So I tried take help of RefCell and it works fine in general. But the problem comes when I tried RefCell with async/await.
error: future cannot be sent between threads safely
--> src/main.rs:10:5
|
10 | tokio::spawn(execute(imut));
| ^^^^^^^^^^^^ future returned by `execute` is not `Send`
|
::: /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.21/src/task/spawn.rs:127:21
|
127 | T: Future + Send + 'static,
| ---- required by this bound in `tokio::task::spawn::spawn`
|
= help: the trait `std::marker::Sync` is not implemented for `std::cell::Cell<isize>`
note: future is not `Send` as this value is used across an await
--> src/main.rs:16:5
|
15 | let a_foo = imut.get_a();
| ----- has type `std::cell::RefMut<'_, Foo>` which is not `Send`
16 | async {}.await;
| ^^^^^^^^^^^^^^ await occurs here, with `a_foo` maybe used later
17 | }
| - `a_foo` is later dropped here
Playground
Is there any workaround?