1

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?

njaard
  • 529
  • 4
  • 15
  • Could you clarify how this question is different from your [previous one](http://stackoverflow.com/q/38776577/155423)? – Shepmaster Aug 05 '16 at 03:11
  • this one has the Arc, and a function that returns (and keeps reference) to the Arc's contents. – njaard Aug 05 '16 at 11:04
  • I can't `std::mem::replace` the Arc because it's being borrowed by the lock. Dropping the lock does nothing. – njaard Aug 05 '16 at 16:07
  • Why are you try to `something_arc.write()` if you already have that as `something_locked` ? – dpc.pw Aug 15 '16 at 18:03

0 Answers0