hello, im probably bringing up a common question here but ill ask anyways.
how do i deal with the need of having multiple mutable references to some data?
when i say module i dont mean a rust module but instead a struct with its impl, just to be clear.
example:
lets say i have ModuleA and ModuleB, they both need a mutable reference to a ModuleC in my architecture.
struct ModuleA<'a> {
mod_c: &'a mut ModuleC
}
impl<'a> ModuleA<'a> {
fn delegate(&mut self) {
self.mod_c.say_hello();
}
}
struct ModuleB<'a> {
mod_c: &'a mut ModuleC
}
impl<'a> ModuleB<'a> {
fn delegate(&mut self) {
self.mod_c.say_hello();
}
}
#[derive(Default)]
struct ModuleC {}
impl ModuleC {
// just an example ofc, not needed mutable
pub fn say_hello(&mut self) {
println!("hello");
}
}
fn main() {
let mut module_c = ModuleC::default();
let mut module_a = ModuleA { mod_c: &mut module_c };
let mut module_b = ModuleB { mod_c: &mut module_c };
module_a.delegate();
}
the borrow checker will complain.
i could wrap it in a refcell but that brings in additional runtime overhead in this simple single-threaded context.
i could wrap it in a arc mutex but that also brings additional runtime overhead and just feels wrong (if its not multithreaded, i dont expect to be using mutexes).
im used to first writing a single threaded version of my code and then write and wrap around it the multithreaded one.
so my conclusion is that if you want to share rw access to something in rust, you must use arc mutex basically all the time even when it doesnt make sense lol? just to prove the borrow checker im not doing anything sketchy...
i dont like the fact that the borrow checker has any power over my architectural decisions, exceptionally for something simple like this for example. i see it as a huge limitation.
if you guys have any advice, pattern or whatever. id appriciate.