I have some code that stores an object. I have a function that does the legwork of storing that object and returning an Arc of it.
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()
}
Elsewhere, I have code that sometimes needs to get a new make_something, letting the old Something
get stored elsewhere in make_something
's Self
. However, it gives me scoping problems:
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();
}
}
The borrow checker is telling me that I can't replace something_arc
because it's being borrowed by something_locked
.
How do I replace something_arc
and something_locked
with a new Arc
and associated write lock?