pub trait OffsetToBytes<const LEN: usize> {
fn offset_to_bytes(&self) -> [u8; LEN];
}
impl OffsetToBytes<1> for u8 {
fn offset_to_bytes(&self) -> [u8; 1] {
[*self]
}
}
impl OffsetToBytes<1> for i8 {
fn offset_to_bytes(&self) -> [u8; 1] {
let shifted = self.wrapping_sub(i8::MIN);
shifted.to_le_bytes()
}
}
impl OffsetToBytes<2> for i16 {
fn offset_to_bytes(&self) -> [u8; 2] {
let shifted = self.wrapping_sub(i16::MIN);
shifted.to_le_bytes()
}
}
impl OffsetToBytes<4> for i32 {
fn offset_to_bytes(&self) -> [u8; 4] {
let shifted = self.wrapping_sub(i32::MIN);
shifted.to_le_bytes()
}
}
impl OffsetToBytes<8> for i64 {
fn offset_to_bytes(&self) -> [u8; 8] {
let shifted = self.wrapping_sub(i64::MIN);
shifted.to_le_bytes()
}
}
impl OffsetToBytes<16> for i128 {
fn offset_to_bytes(&self) -> [u8; 16] {
let shifted = self.wrapping_sub(i128::MIN);
shifted.to_le_bytes()
}
}
impl OffsetToBytes<1> for bool {
fn offset_to_bytes(&self) -> [u8; 1] {
[u8::from(*self)]
}
}
impl OffsetToBytes<8> for u64 {
fn offset_to_bytes(&self) -> [u8; 8] {
self.to_le_bytes()
}
}
impl OffsetToBytes<32> for [u64; 4] {
fn offset_to_bytes(&self) -> [u8; 32] {
bytemuck::cast(*self)
}
}