use ethnum::{I256, U256};
use ordered_float::OrderedFloat;
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]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for u32 {
fn marshal(&self, scratch: &mut [u8]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for u64 {
fn marshal(&self, scratch: &mut [u8]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for u128 {
fn marshal(&self, scratch: &mut [u8]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for U256 {
fn marshal(&self, scratch: &mut [u8]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
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]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for i32 {
fn marshal(&self, scratch: &mut [u8]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for i64 {
fn marshal(&self, scratch: &mut [u8]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for i128 {
fn marshal(&self, scratch: &mut [u8]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for I256 {
fn marshal(&self, scratch: &mut [u8]) {
let bytes = self.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for f32 {
fn marshal(&self, scratch: &mut [u8]) {
let bits = self.to_bits();
let bytes = bits.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for f64 {
fn marshal(&self, scratch: &mut [u8]) {
let bits = self.to_bits();
let bytes = bits.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for OrderedFloat<f32> {
fn marshal(&self, scratch: &mut [u8]) {
let bits = self.to_bits();
let bytes = bits.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for OrderedFloat<f64> {
fn marshal(&self, scratch: &mut [u8]) {
let bits = self.to_bits();
let bytes = bits.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}
impl Marshal for bool {
fn marshal(&self, scratch: &mut [u8]) {
scratch[0] = *self as u8;
}
}
impl Marshal for char {
fn marshal(&self, scratch: &mut [u8]) {
let bits = *self as u32;
let bytes = bits.to_le_bytes();
scratch.copy_from_slice(&bytes);
}
}