Why does &mut (*pointer)
create a reference to the original memory behind pointer
, while &mut { *pointer }
creates a reference to a copy?
#[derive(Copy, Clone)]
struct MyStruct {
num: u32,
}
fn main() {
let mut my_struct = MyStruct { num: 42 };
let pointer = &mut my_struct as *mut MyStruct;
let ref_to_orig = unsafe { &mut (*pointer) };
ref_to_orig.num += 100;
println!("{}", my_struct.num); // prints "142"
let ref_to_copy = unsafe { &mut { *pointer } };
ref_to_copy.num += 100;
println!("{}", my_struct.num); // still prints "142"
}
(Context: I need to understand this because I'm trying to work with Apple's CoreAudio C API, which uses callbacks. I need to write a callback in Rust conforming to a C API which takes a void*
as an argument. The void*
will be a pointer to a struct, whose fields the callback needs to modify. Updating the fields of a local copy would be pointless.)