Shared ownership of mutable data in beta/stable rust


#1

I am having a member of data_: Arc<Mutex<Data>> to hold some mutable data in a struct. The data_ may be accessed or modified by a number threads.
Later in a function the attempt to obtain a weak pointer using

let mut wd = self.data.downgrade(); results in the following obvious error:

use of unstable library feature 'arc_weak': Weak pointers may not belong in this module.

which complains about using unstable library feature.

I am interested to have the code compile using Beta/Stable version of rust.

Are there any stable alternatives to achieve above? I tried using stabe_rc, which offers stable weak pointer when immutable data is shared using Rc, but I did not find a replacement for Arc there.


#2

Did you try using RefCell? I suspect Mutex <RefCell <T>> may do what you are looking for.


#3

Mutex takes care of the locking and mutability, so that’s like adding another lock to your lock. What @mmoadeli asks for is a way to use weak Arc references in stable/beta Rust.

It seems like nobody has released a copy of the Arc code on crates.io, as far as I can find. There is always the option make your own copy :stuck_out_tongue_winking_eye: