Rust 替换借用的弧<;RwLock>;
我有一些存储对象的代码。我有一个函数,用于存储该对象并返回其圆弧Rust 替换借用的弧<;RwLock>;,rust,Rust,我有一些存储对象的代码。我有一个函数,用于存储该对象并返回其圆弧 struct Something { // ... } // create a something, returning a locked Arc of it. fn make_something(&mut self) -> Arc<RwLock<Something>> { let x = Something{}; let stored = Arc::new(RwLo
struct Something {
// ...
}
// create a something, returning a locked Arc of it.
fn make_something(&mut self) -> Arc<RwLock<Something>>
{
let x = Something{};
let stored = Arc::new(RwLock::new(x));
// stored is cloned and put into a container in self
stored.clone()
}
借阅检查器告诉我,我不能替换某个东西,因为它正被某个锁定的东西借来
如何用新的arc
和相关的写锁替换something\u-arc
和something\u-lock
我不能std::mem::replace
Arc,因为它被锁借用了。放下锁没有任何作用。如果你已经将某个东西锁定为某个东西,为什么要尝试某个东西arc.write()
?你能澄清一下这个问题与你的问题有什么不同吗?这个问题有arc,还有一个返回(并保留引用)的函数我不能std::mem::replace
Arc,因为它被锁借用了。放下锁没有任何作用。如果你已经将某个东西锁定为某个东西,为什么还要尝试某个东西。write()
?
fn elsewhere() {
let mut something_arc = obj.make_something();
let mut something_locked = something_arc.write().unwrap();
loop {
// something_lock is mutated as a Something
// create a new make something, and start a "transaction"
something_arc = obj.make_something();
something_locked = something_arc.write().unwrap();
}
}