(no title)
yinser
|
1 year ago
Is it possible for the rust compiler to statically determine if two memory regions will overlap at compile-time, especially with complex pointer arithmetic or when pointers are passed as function arguments? It would be super impressive if so.
saghm|1 year ago
In practice, that means that when writing your own code, you can (and in the vast majority of cases should) just stick to safe Rust, and you can be sure that you won't ever make overlapping references if either one is mutable. Safe APIs can be built on top of unsafe APIs though, so you won't necessarily be able to assume your dependencies are doing the same (although there's tooling to help with that, e.g. requiring via the config to generate a build error if any dependency uses unsafe code)
yinser|1 year ago
// Use a mutable slice to represent the buffer:
let mut buffer = [0u8; 16];
// Read into the buffer:
let n = socket.read(&mut buffer)?;
// To move the partial second row to the beginning, you'd use safe operations like:
let second_row_start = 7; // Index where second row starts
buffer.copy_within(second_row_start.., 0);
// Then read more data into the remaining space:
let remaining_space = &mut buffer[9..];
let additional_bytes = socket.read(remaining_space)?;
Is this idiomatic for Rust?