I believe that rust should add some form of safe shared mutability (without the need of RefCell)
let mut idk = 5;
let shared_one = &shr mut idk;
let shared_two = &shr mut idk;
*shared_one += 5;
*shared_two += 5;
// no errors, because &shr mut is used, not &mut
let shared_two = &mut idk;
// error, because u shouldnt be able to make an unshared mut reference where a shared mut reference or immutable reference exists
How will this work?
there will be a trait called Shareable (or something else, but lets just use this name), which structs can implement. if a struct implements it, all its members and itself can be shared mutably, and the &shr mut keyword would work on it's instances. a struct cannot implement it if at least one of its members is not Shareable to ensure memory safety. For example Vec won't implement it, since resizing it may delete memory that may still be accessed if its shared mutably. and any struct that has a field of Vec won't be able to be Shareable.
Programmers can bypass this with unsafe, but it should be encapsulated in a struct and using the struct normally should not cause any invalid memory access.
How will this be useful?
Vec would definitely lead to issues if shared mutably, so of course this wont implement Shareable
So the rust standard library can then add another Vec like say, SharedVec that is indeed Shareable. it's implementation would work in a way that shared mutability on it would NOT lead to invalid memory access. it will of course have more overhead, but u would only use this when u need it.
of course, you can't have both &mut and &shr mut to the same reference. so the idea that there can be multiple references but only one &mut applies (but with this concept, mutiple &shr mut).
How about in a multi-threaded environment?
In a multi threaded environment you would still need stuff like RwLock and Mutex because this concept only works with single threaded environments
Why make this? shared mutability is the root of all evil!
- It opens more ways to program things that are still memory safe, while still maintaining the "pay only for what you use" idea that rust follows.
- It helps new rust devs easily blend into the rust ecosystem, since they would be able to code things more easily like they did in other programming languages, and possibly optimize it later.
- It helps with quicker prototyping.
- For stuff like game dev, making video games in rust can be frustrating sometimes, and part of it is because of the lack of shared mutability. this concept can help with it.
I know that rust does not seem to like shared mutability, but honestly I think sometimes it's necessary. and having something like &shr mut, the shr part will make programmers know that "oh yeah the value could be changed somewhere cuz it has the shr part"
Why not just RefCell?
RefCell has to do runtime checks, which has overhead, and can even throw errors in scenarios where shared mut wont lead to any issues. So that's: runtime overhead and, sometimes, unnecessary errors
Thoughts? do u think this is a good idea? do you think this is possible? please share your thoughts.