It seems like this should work:
trait IntoBytes: Sized + Copy {
fn into_bytes(&self, bytes: &mut [u8; size_of::<Self>()]) {
let src = self as *const Self as *const u8;
let dst: *mut u8 = &mut bytes[0];
unsafe { std::ptr::copy_nonoverlapping::<u8>(src, dst, bytes.len()); }
}
}
impl IntoBytes for u64 {}
impl IntoBytes for i64 {}
Or at least this:
trait IntoBytes<T: Sized>: Sized + Copy {
fn into_bytes(&self, bytes: &mut [u8; size_of::<T>()]) {
let src = self as *const Self as *const u8;
let dst: *mut u8 = &mut bytes[0];
unsafe { std::ptr::copy_nonoverlapping::<u8>(src, dst, bytes.len()); }
}
}
impl IntoBytes<u8> for u64 {}
impl IntoBytes<u8> for i64 {}