Closed zxch3n closed 1 year ago
Currently, the following code causes undefined behavior.
#[test] fn test_inner_mut() { let five = Somr::new(5); fn add(a: &Somr<i32>, b: &Somr<i32>) { a.get_mut().map(|x| *x += *b.get().unwrap()).unwrap(); } add(&five, &five); assert_eq!(five.get().copied().unwrap(), 10); }
The original fn get_mut(&self) -> Option<&mut T> is error-prone, as the compiler cannot prevent programmers from creating multiple mut ref to the same element. Making it unsafe could reduce the scope that needs to be audited.
fn get_mut(&self) -> Option<&mut T>
unsafe
Currently, the following code causes undefined behavior.
The original
fn get_mut(&self) -> Option<&mut T>
is error-prone, as the compiler cannot prevent programmers from creating multiple mut ref to the same element. Making itunsafe
could reduce the scope that needs to be audited.