How to move a value out of tokio RwLock/Mutex #4047
Replies: 2 comments 1 reply
-
Try using use tokio::sync::RwLock;
use std::mem;
let rwlock = RwLock::new(Vec::new());
// ...
// use `mem::take` to move the `Vec` out of the `RwLock`.
let mut write = lock.write().await;
let the_vec = mem::take(&mut *write);
// now, we have the `Vec` and can pass it into whatever function we need
do_something_with_the_vec(the_vec);
Alternatively, if you want to replace the value inside the |
Beta Was this translation helpful? Give feedback.
-
For cases where you have ownership of the lock, you can call the |
Beta Was this translation helpful? Give feedback.
-
I'm using protobuf and it need the vec to construct bytes, but the vec is in RwLock and cannot be moved. Is there any way to move the data without cloning the vec?
Beta Was this translation helpful? Give feedback.
All reactions