Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does `write` mode on a `RwLock` work if it isn't mutable?

So, I was writing some code and apparently R.A. didn’t warn me about some erroneous stuff I had written in regards to how ownership works with lambdas.
So, a friend helped me rewrite some of my code, and this is just a play example, but their new code boils down to this:

let vec = Rc::new(RwLock::new( Vec::new() ));
let vec_rc = vec.clone();
let my_lambda = || -> () {
  vec_rc.write().unwrap().push(/* ... */);
}

But what I don’t understand is how this works if vec_rc isn’t mut.
From my prior knowledge, mutable in Rust cascades; in other words, if the "master-containing" object is immutable the rest will have to be too.

Could I please get some clarity as to what goes on under the hood?
Or is their code erroneous too?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From my prior knowledge, mutable in Rust cascades; in other words, if the "master-containing" object is immutable the rest will have to be too.

This is almost always true… Until we consider interior mutability.

Interior mutability is exactly about that: changing a value through a shared reference. Moreover, while there are other shared references to it. The basic interior mutability primitive is UnsafeCell, but there are multiple abstractions built on top of it – one of them is RwLock (you can see it secretly contains an UnsafeCell).

As a side note, Rc<RwLock<T>> is almost always wrong: Rc is non thread safe, which defeats the whole purpose of RwLock. If you just need shared mutable stated over one thread, use Rc<RefCell<T>>. It is much more performant and can’t block (so no deadlock debugging, just a simple panic if something went wrong).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading