Skip to content

Commit

Permalink
Refactoring marshal and unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
suharev7 committed Nov 13, 2023
1 parent 1bffb32 commit f224fed
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 272 deletions.
162 changes: 23 additions & 139 deletions src/types/marshal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
164 changes: 31 additions & 133 deletions src/types/unmarshal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,139 +2,37 @@ pub trait Unmarshal<T: Copy> {
fn unmarshal(scratch: &[u8]) -> T;
}

impl Unmarshal<u8> for u8 {
fn unmarshal(scratch: &[u8]) -> Self {
scratch[0]
}
}

impl Unmarshal<u16> for u16 {
fn unmarshal(scratch: &[u8]) -> Self {
Self::from(scratch[0]) | Self::from(scratch[1]) << 8
}
}

impl Unmarshal<u32> 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<u64> 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<u128> 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<i8> for i8 {
fn unmarshal(scratch: &[u8]) -> Self {
scratch[0] as Self
}
}

impl Unmarshal<i16> for i16 {
fn unmarshal(scratch: &[u8]) -> Self {
Self::from(scratch[0]) | Self::from(scratch[1]) << 8
}
}

impl Unmarshal<i32> 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<i64> 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<i128> 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<f32> 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<f64> 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::<Self>()];
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::<Self>()];
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<bool> for bool {
fn unmarshal(scratch: &[u8]) -> Self {
Expand Down

0 comments on commit f224fed

Please sign in to comment.