by deathanatos · 8y ago · 0 comments
#[derive(Copy, Clone, Debug)]
#[repr(C)]
struct Foo {
a: i32,
b: i32,
}
fn main() {
let mut array = [Foo { a: 0x01010101i32, b: 0x01010101i32 }; 256];
let foo = &mut array[0];
foo.a += 1;
}
The unsafe section isn't even required, and the effect is the same. And I don't think this violates the spirit of his example, either. Consider the author's first link to a real-world occurrence of this: let size = mem::size_of::<FILE_NAME_INFO>();
let mut name_info_bytes = vec![0u8; size + MAX_PATH];
let res = GetFileInformationByHandleEx(handle,
FileNameInfo,
&mut *name_info_bytes as *mut _ as *mut c_void,
name_info_bytes.len() as u32);
This is again, IMO, the wrong way to do this. You should just cast a pointer to an instance of the FILE_NAME_INFO struct into a c_void; the structure will need to use #[repr(C)] and the code will still be unsafe due to the C FFI, but it will be correct (and a lot simpler). This is the same thing that you would do in C, were you to call this function: FILE_NAME_INFO file_name_info;
GetFileInformationByHandleEx(
handle,
FileNameInfo,
&file_name_info,
sizeof(file_name_info),
)
just in Rust.[1]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa3...
So, it seems like this is relative easy to do on the stack, which is how the example does it presently. See the link below to my attempt; the stack allocation is still all safe code, still a single line. However, I presume that one will want to also create one on the heap, especially since in the example the author poses it would be a rather large stack allocation, and one might — quite reasonably — put that on the heap.
My attempt is here: https://play.rust-lang.org/?gist=1c50b35941506316372da860cae...
Couldn't avoid the unsafe for that, but, I was able to get rid of the transmute call, and transmute is a function where the warning on the tin is "this function is not just unsafe, it is radioactive". But the amount of code required still felt a bit lacking.
It seems these are an area of active work[1][2] currently.
I think there is still definitely a valid point that the author is hitting — that encoding more information into the program can allow the compiler to catch more classes of errors. (This is, after all, the very logic that gave us Rust.)