use zerocopy::Immutable;
pub trait WriteTo: Immutable + Sized {
unsafe fn write_to(&self, target: *mut Self);
unsafe fn write_to_volatile(&self, target: *mut Self);
}
macro_rules! impl_primitive {
{$ty:ty} => {
static_assertions::assert_impl_all!($ty: Copy);
impl $crate::WriteTo for $ty {
#[inline]
unsafe fn write_to(&self, target: *mut Self) {
unsafe { *target = *self };
}
#[inline]
unsafe fn write_to_volatile(&self, target: *mut Self) {
unsafe { target.write_volatile(*self) };
}
}
}
}
impl_primitive!(bool);
impl_primitive!(i8);
impl_primitive!(u8);
impl_primitive!(i16);
impl_primitive!(u16);
impl_primitive!(i32);
impl_primitive!(u32);
impl_primitive!(i64);
impl_primitive!(u64);
impl_primitive!(f32);
impl_primitive!(f64);
impl<T, const N: usize> WriteTo for [T; N]
where
T: WriteTo,
{
#[inline]
unsafe fn write_to(&self, target: *mut Self) {
for (i, elem) in self.iter().enumerate() {
unsafe { elem.write_to(target.cast::<T>().add(i)) };
}
}
#[inline]
unsafe fn write_to_volatile(&self, target: *mut Self) {
for (i, elem) in self.iter().enumerate() {
unsafe { elem.write_to_volatile(target.cast::<T>().add(i)) };
}
}
}