I am attempting to save the shared reference to a single memory location with Serde. I have a small struct container that contains two attributes of type Arc<RwLock<f64>>
. My goal is for both the attributes to share a single memory location so that when I change one, the other also changes. This works when container is created in main, but when I load in the serialized struct ref0 and ref1 no longer share the same memory location, and contain unique memory locations with the same value.
Is it possible to indicate to Serde that ref0 and ref1 are supposed to share a memory location and be "linked?"
#[derive(Serialize, Deserialize)]
struct Container {
pub(crate) ref0: Arc<RwLock<f64>>,
pub(crate) ref1: Arc<RwLock<f64>>
}
Saving Scenario (Works)
//makers ref0 and ref1, assign them to container.
let mut ref0 = Arc::new(RwLock::new(1.0));
let ref1 = ref0.clone();
let container = Container {
ref0,
ref1
};
// Checks that changing one ref will alter the other.
mem::replace(&mut *container.ref0.write().unwrap(), 2.0);
println!("{} {}",container.ref0.read().unwrap(), container.ref1.read().unwrap()); // should output "2.0 2.0" and does.
mem::replace(&mut *container.ref0.write().unwrap(), 3.0);
println!("{} {}",container.ref0.read().unwrap(), container.ref1.read().unwrap()); // should output "3.0 3.0" and does.
// Serializes the struct and save
let f_name = "SaveTest.test";
let mut file = File::create( f_name).unwrap();
let _result = file.write_all(&bincode::serialize(&container).unwrap());
Load Scenario (Does not Work)
//LOAD THE CONTAINER
let mut buffer : Vec<u8> = vec![];
BufReader::new(File::open("SaveTest.test").unwrap()).read_to_end(&mut buffer).unwrap();
let container : Container = bincode::deserialize(&buffer).unwrap();
//Verifies that each ref is the same value when it was saved
println!("{} {}",container.ref0.read().unwrap(), container.ref1.read().unwrap()); // Should output "3.0 3.0" and does
mem::replace(&mut *container.ref0.write().unwrap(), 4.0);
println!("{} {}",container.ref0.read().unwrap(), container.ref1.read().unwrap()); // Should output "4.0 4.0" but outputs "4.0 3.0"