I hope I got this right (safety-proprty), i.e. so that references are enforced to have equal life-time:
impl<'a> From<&'a Header> for &'a [u8] {
fn from(value: &Header) -> Self {
// SAFETY: out-of-boundary is not possible, given that the size constraint
// exists in the struct definition. The lifetime parameter links the lifetime
// of the header reference to the slice.
unsafe { from_raw_parts((value as *const Header) as *const u8, size_of::<Header>()) }
}
}
@jarkko I believe that's safe, provided Header has no padding, otherwise, you could have potentially uninitialized values for some of the bytes
You might be interested in `#[derive(AsBytes)]` from the zerocopy crate: https://docs.rs/zerocopy/latest/zerocopy/trait.AsBytes.html, which checks for that at compile time, and exposes a safe API