use crate::codec::error::Error; use std::{io::Write, sync::Arc}; macro_rules! impl_encode { ($t: ty) => { impl Encode for $t { fn encode(self, buffer: &mut dyn Write) -> Result { let size = buffer.write(&self.to_be_bytes())?; Ok(size) } } }; } pub trait Encode { fn encode(self, buffer: &mut dyn Write) -> Result; } impl Encode for bool { fn encode(self, buffer: &mut dyn Write) -> Result { let size = buffer.write(&[self as u8])?; Ok(size) } } impl Encode for Arc<[u8]> { fn encode(self, buffer: &mut dyn Write) -> Result { let len = buffer.write(&self)?; Ok(len) } } impl Encode for [u8; S] { fn encode(self, buffer: &mut dyn Write) -> Result { let len = buffer.write(&self)?; Ok(len) } } impl_encode!(u8); impl_encode!(u16); impl_encode!(u32); impl_encode!(u64); impl_encode!(u128); impl_encode!(usize); impl_encode!(i8); impl_encode!(i16); impl_encode!(i32); impl_encode!(i64); impl_encode!(i128); impl_encode!(isize); impl_encode!(f32); impl_encode!(f64);