diff --git a/src/types/marshal.rs b/src/types/marshal.rs index 4acd863..cbad723 100644 --- a/src/types/marshal.rs +++ b/src/types/marshal.rs @@ -2,149 +2,33 @@ pub trait Marshal { fn marshal(&self, scratch: &mut [u8]); } -impl Marshal for u8 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self; - } -} - -impl Marshal for u16 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - scratch[1] = (self >> 8) as u8; - } -} - -impl Marshal for u32 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - scratch[1] = (self >> 8) as u8; - scratch[2] = (self >> 16) as u8; - scratch[3] = (self >> 24) as u8; - } -} - -impl Marshal for u64 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - scratch[1] = (self >> 8) as u8; - scratch[2] = (self >> 16) as u8; - scratch[3] = (self >> 24) as u8; - - scratch[4] = (self >> 32) as u8; - scratch[5] = (self >> 40) as u8; - scratch[6] = (self >> 48) as u8; - scratch[7] = (self >> 56) as u8; - } -} - -impl Marshal for u128 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - scratch[1] = (self >> 8) as u8; - scratch[2] = (self >> 16) as u8; - scratch[3] = (self >> 24) as u8; - - scratch[4] = (self >> 32) as u8; - scratch[5] = (self >> 40) as u8; - scratch[6] = (self >> 48) as u8; - scratch[7] = (self >> 56) as u8; - - scratch[8] = (self >> 64) as u8; - scratch[9] = (self >> 72) as u8; - scratch[10] = (self >> 80) as u8; - scratch[11] = (self >> 88) as u8; - - scratch[12] = (self >> 96) as u8; - scratch[13] = (self >> 104) as u8; - scratch[14] = (self >> 112) as u8; - scratch[15] = (self >> 120) as u8; - } -} - -impl Marshal for i8 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - } -} - -impl Marshal for i16 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - scratch[1] = (self >> 8) as u8; - } -} - -impl Marshal for i32 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - scratch[1] = (self >> 8) as u8; - scratch[2] = (self >> 16) as u8; - scratch[3] = (self >> 24) as u8; - } -} - -impl Marshal for i64 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - scratch[1] = (self >> 8) as u8; - scratch[2] = (self >> 16) as u8; - scratch[3] = (self >> 24) as u8; - - scratch[4] = (self >> 32) as u8; - scratch[5] = (self >> 40) as u8; - scratch[6] = (self >> 48) as u8; - scratch[7] = (self >> 56) as u8; - } +macro_rules! int_marshals { + ( $( $t:ident ),* ) => { + $( + impl Marshal for $t { + fn marshal(&self, scratch: &mut [u8]) { + scratch.clone_from_slice(&self.to_le_bytes()); + } + } + )* + }; } -impl Marshal for i128 { - fn marshal(&self, scratch: &mut [u8]) { - scratch[0] = *self as u8; - scratch[1] = (self >> 8) as u8; - scratch[2] = (self >> 16) as u8; - scratch[3] = (self >> 24) as u8; - - scratch[4] = (self >> 32) as u8; - scratch[5] = (self >> 40) as u8; - scratch[6] = (self >> 48) as u8; - scratch[7] = (self >> 56) as u8; - - scratch[8] = (self >> 64) as u8; - scratch[9] = (self >> 72) as u8; - scratch[10] = (self >> 80) as u8; - scratch[11] = (self >> 88) as u8; - - scratch[12] = (self >> 96) as u8; - scratch[13] = (self >> 104) as u8; - scratch[14] = (self >> 112) as u8; - scratch[15] = (self >> 120) as u8; - } +macro_rules! float_marshals { + ( $( $t:ident ),* ) => { + $( + impl Marshal for $t { + fn marshal(&self, scratch: &mut [u8]) { + let bits = self.to_bits(); + scratch.clone_from_slice(&bits.to_le_bytes()); + } + } + )* + }; } -impl Marshal for f32 { - fn marshal(&self, scratch: &mut [u8]) { - let bits = self.to_bits(); - scratch[0] = bits as u8; - scratch[1] = (bits >> 8) as u8; - scratch[2] = (bits >> 16) as u8; - scratch[3] = (bits >> 24) as u8; - } -} - -impl Marshal for f64 { - fn marshal(&self, scratch: &mut [u8]) { - let bits = self.to_bits(); - scratch[0] = bits as u8; - scratch[1] = (bits >> 8) as u8; - scratch[2] = (bits >> 16) as u8; - scratch[3] = (bits >> 24) as u8; - scratch[4] = (bits >> 32) as u8; - scratch[5] = (bits >> 40) as u8; - scratch[6] = (bits >> 48) as u8; - scratch[7] = (bits >> 56) as u8; - } -} +int_marshals! { u8, u16, u32, u64, u128, i8, i16, i32, i64, i128 } +float_marshals! { f32, f64 } impl Marshal for bool { fn marshal(&self, scratch: &mut [u8]) { diff --git a/src/types/unmarshal.rs b/src/types/unmarshal.rs index ec14ef3..04caef3 100644 --- a/src/types/unmarshal.rs +++ b/src/types/unmarshal.rs @@ -2,139 +2,37 @@ pub trait Unmarshal { fn unmarshal(scratch: &[u8]) -> T; } -impl Unmarshal for u8 { - fn unmarshal(scratch: &[u8]) -> Self { - scratch[0] - } -} - -impl Unmarshal for u16 { - fn unmarshal(scratch: &[u8]) -> Self { - Self::from(scratch[0]) | Self::from(scratch[1]) << 8 - } -} - -impl Unmarshal for u32 { - fn unmarshal(scratch: &[u8]) -> Self { - Self::from(scratch[0]) - | Self::from(scratch[1]) << 8 - | Self::from(scratch[2]) << 16 - | Self::from(scratch[3]) << 24 - } -} - -impl Unmarshal for u64 { - fn unmarshal(scratch: &[u8]) -> Self { - Self::from(scratch[0]) - | Self::from(scratch[1]) << 8 - | Self::from(scratch[2]) << 16 - | Self::from(scratch[3]) << 24 - | Self::from(scratch[4]) << 32 - | Self::from(scratch[5]) << 40 - | Self::from(scratch[6]) << 48 - | Self::from(scratch[7]) << 56 - } -} - -impl Unmarshal for u128 { - fn unmarshal(scratch: &[u8]) -> Self { - Self::from(scratch[0]) - | Self::from(scratch[1]) << 8 - | Self::from(scratch[2]) << 16 - | Self::from(scratch[3]) << 24 - | Self::from(scratch[4]) << 32 - | Self::from(scratch[5]) << 40 - | Self::from(scratch[6]) << 48 - | Self::from(scratch[7]) << 56 - | Self::from(scratch[8]) << 64 - | Self::from(scratch[9]) << 72 - | Self::from(scratch[10]) << 80 - | Self::from(scratch[11]) << 88 - | Self::from(scratch[12]) << 96 - | Self::from(scratch[13]) << 104 - | Self::from(scratch[14]) << 112 - | Self::from(scratch[15]) << 120 - } -} - -impl Unmarshal for i8 { - fn unmarshal(scratch: &[u8]) -> Self { - scratch[0] as Self - } -} - -impl Unmarshal for i16 { - fn unmarshal(scratch: &[u8]) -> Self { - Self::from(scratch[0]) | Self::from(scratch[1]) << 8 - } -} - -impl Unmarshal for i32 { - fn unmarshal(scratch: &[u8]) -> Self { - Self::from(scratch[0]) - | Self::from(scratch[1]) << 8 - | Self::from(scratch[2]) << 16 - | Self::from(scratch[3]) << 24 - } -} - -impl Unmarshal for i64 { - fn unmarshal(scratch: &[u8]) -> Self { - Self::from(scratch[0]) - | Self::from(scratch[1]) << 8 - | Self::from(scratch[2]) << 16 - | Self::from(scratch[3]) << 24 - | Self::from(scratch[4]) << 32 - | Self::from(scratch[5]) << 40 - | Self::from(scratch[6]) << 48 - | Self::from(scratch[7]) << 56 - } -} - -impl Unmarshal for i128 { - fn unmarshal(scratch: &[u8]) -> Self { - Self::from(scratch[0]) - | Self::from(scratch[1]) << 8 - | Self::from(scratch[2]) << 16 - | Self::from(scratch[3]) << 24 - | Self::from(scratch[4]) << 32 - | Self::from(scratch[5]) << 40 - | Self::from(scratch[6]) << 48 - | Self::from(scratch[7]) << 56 - | Self::from(scratch[8]) << 64 - | Self::from(scratch[9]) << 72 - | Self::from(scratch[10]) << 80 - | Self::from(scratch[11]) << 88 - | Self::from(scratch[12]) << 96 - | Self::from(scratch[13]) << 104 - | Self::from(scratch[14]) << 112 - | Self::from(scratch[15]) << 120 - } -} - -impl Unmarshal for f32 { - fn unmarshal(scratch: &[u8]) -> Self { - let bits = u32::from(scratch[0]) - | u32::from(scratch[1]) << 8 - | u32::from(scratch[2]) << 16 - | u32::from(scratch[3]) << 24; - Self::from_bits(bits) - } -} - -impl Unmarshal for f64 { - fn unmarshal(scratch: &[u8]) -> Self { - let bits = u64::from(scratch[0]) - | u64::from(scratch[1]) << 8 - | u64::from(scratch[2]) << 16 - | u64::from(scratch[3]) << 24 - | u64::from(scratch[4]) << 32 - | u64::from(scratch[5]) << 40 - | u64::from(scratch[6]) << 48 - | u64::from(scratch[7]) << 56; - Self::from_bits(bits) - } -} +macro_rules! int_unmarshals { + ( $( $t:ident ),* ) => { + $( + impl Unmarshal<$t> for $t { + fn unmarshal(scratch: &[u8]) -> Self { + let mut buffer = [0_u8; std::mem::size_of::()]; + buffer.clone_from_slice(scratch); + Self::from_le_bytes(buffer) + } + } + )* + }; +} + +macro_rules! float_unmarshals { + ( $( $t:ident: $b:ident ),* ) => { + $( + impl Unmarshal<$t> for $t { + fn unmarshal(scratch: &[u8]) -> Self { + let mut buffer = [0_u8; std::mem::size_of::()]; + buffer.clone_from_slice(scratch); + let bits = $b::from_le_bytes(buffer); + Self::from_bits(bits) + } + } + )* + }; +} + +int_unmarshals! { u8, u16, u32, u64, u128, i8, i16, i32, i64, i128 } +float_unmarshals! { f32: u32, f64: u64 } impl Unmarshal for bool { fn unmarshal(scratch: &[u8]) -> Self {