diff --git a/src/aligned.rs b/src/aligned.rs index 46e2a9f..af2f1e8 100644 --- a/src/aligned.rs +++ b/src/aligned.rs @@ -1,6 +1,4 @@ -//! Helper for ensuring raw bytes are aligned - -use std::mem::size_of; +//! Helpers for ensuring raw bytes are aligned /// Wrapper for raw bytes that forces them to be aligned to `8`, the max alignment for SDK data /// types @@ -14,24 +12,6 @@ impl Aligned { } } -impl From() }>> for u32 { - fn from(wrapper: Aligned<{ size_of::() }>) -> Self { - *bytemuck::from_bytes(&wrapper.0) - } -} - -impl From() }>> for f32 { - fn from(wrapper: Aligned<{ size_of::() }>) -> Self { - *bytemuck::from_bytes(&wrapper.0) - } -} - -impl From() }>> for f64 { - fn from(wrapper: Aligned<{ size_of::() }>) -> Self { - *bytemuck::from_bytes(&wrapper.0) - } -} - /// Copy the raw data into an aligned array and reinterpret it as the given type pub fn align_cast(data: &[u8]) -> T where @@ -39,3 +19,17 @@ where { Aligned::new(data).into() } + +macro_rules! impl_from_aligned { + ($aligned:ident for [$($t:ty),+]) => { + $( + impl ::std::convert::From<$aligned<{ ::std::mem::size_of::<$t>() }>> for $t { + fn from(wrapper: $aligned<{ ::std::mem::size_of::<$t>() }>) -> Self { + *(::bytemuck::from_bytes(&wrapper.0)) + } + } + )+ + }; +} + +impl_from_aligned! { Aligned for [u32, f32, f64] }