我有一些存储对象的代码。我有一个功能,可以存储该对象并返回它的弧。
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()
}
在其他地方,我的代码有时需要获取新的make_something,让旧的Something
存储在make_something
的{{1}}的其他位置。但是,它给了我范围问题:
Self
借阅检查员告诉我,我无法替换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();
}
}
,因为它是something_arc
借用的。
如何使用新的something_locked
和相关的写锁定替换something_arc
和something_locked
?