use core::mem::{self};
use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
};
use crate::integer::{Isize, Usize, I128, I16, I32, I64, I8, U128, U16, U32, U64, U8};
use crate::{Abi, Chunk, Slice};
pub unsafe trait BytesOf {
fn bytes_of(&self) -> &[u8] {
unsafe {
let data = <*const Self>::from(self).cast::<u8>();
core::slice::from_raw_parts(data, mem::size_of_val(self))
}
}
}
unsafe impl<const N: usize> BytesOf for Chunk<N> {}
unsafe impl BytesOf for Slice<'_> {}
unsafe impl<T> BytesOf for [T] where T: Abi {}
unsafe impl<'a, T> BytesOf for &'a [T] where T: Abi {}
unsafe impl<T, const N: usize> BytesOf for [T; N] where T: Abi {}
macro_rules! impl_bytes_of {
($($ty:ty),* $(,)?) => {
$(
unsafe impl BytesOf for $ty {}
)*
};
}
impl_bytes_of! {
i8, i16, i32, i64, i128, isize, I8, I16, I32, I64, I128, Isize,
u8, u16, u32, u64, u128, usize, U8, U16, U32, U64, U128, Usize,
NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize,
}
pub unsafe trait BytesOfMut {
fn bytes_of_mut(&mut self) -> &mut [u8] {
unsafe {
let bptr = <*mut Self>::from(self).cast::<u8>();
core::slice::from_raw_parts_mut(bptr, mem::size_of_val(self))
}
}
}
unsafe impl<T: ?Sized> BytesOfMut for *mut T {}
unsafe impl<'a, T: Abi> BytesOfMut for &'a mut [T] {}