I have a use-case for which I want to interpret a slice of bytes as a slice of structs.
For that I was thinking to use a chain of transformations:
- [u8] -> [[u8; SIZE]]
- [[u8; SIZE]] to [A] (transparent struct with [u8; SIZE] member)
Is this overall transformation safe to do? I know that individual transparent structs are safe to transmute (thanks to transparent), but I could not find anything documented about slices of transparent structs, nor about the safety of converting a slice into a nested slice of arrays.
Example (Playground)
#[repr(transparent)]
struct A {
data: [u8; 16],
}
impl A {
fn set_x(&mut self, value: u8) {
self.data[3] = value;
}
fn x(&self) -> u8 {
self.data[3]
}
}
fn reinterpret(data: &mut [u8]) -> &mut [A] {
assert!(data.len() % 16 == 0);
let ptr : *mut u8 = data.as_mut_ptr();
let a_ptr = ptr as *mut A;
unsafe { std::slice::from_raw_parts_mut(a_ptr, data.len() / 16) }
}
fn main() {
let mut data_bytes = vec![0; 10 * 16];
let data = reinterpret(&mut data_bytes);
for (i, a) in data.iter_mut().enumerate() { a.set_x(i as u8); }
for a in data { println!("{}", a.x( )); }
}