use bincode::Encode;
use bytey_byte_buffer::{
byte_buffer::ByteBuffer,
error::{ByteBufferError, Result},
};
pub trait BincodeEncode {
fn encode<T>(&mut self, source: T) -> Result<&mut Self>
where
T: Encode;
fn encode_le<T>(&mut self, source: T) -> Result<&mut Self>
where
T: Encode;
fn encode_be<T>(&mut self, source: T) -> Result<&mut Self>
where
T: Encode;
}
impl BincodeEncode for ByteBuffer {
fn encode<T>(&mut self, source: T) -> Result<&mut Self>
where
T: Encode,
{
let bytes = bincode::encode_to_vec(&source, bincode::config::standard()).map_err(|e| {
ByteBufferError::OtherError {
error: e.to_string(),
}
})?;
self.write(bytes.len() as u64)?;
self.write_slice(&bytes[..])
}
fn encode_le<T>(&mut self, source: T) -> Result<&mut Self>
where
T: Encode,
{
let bytes =
bincode::encode_to_vec(&source, bincode::config::standard().with_little_endian())
.map_err(|e| ByteBufferError::OtherError {
error: e.to_string(),
})?;
self.write_le(bytes.len() as u64)?;
self.write_slice(&bytes[..])
}
fn encode_be<T>(&mut self, source: T) -> Result<&mut Self>
where
T: Encode,
{
let bytes = bincode::encode_to_vec(&source, bincode::config::standard().with_big_endian())
.map_err(|e| ByteBufferError::OtherError {
error: e.to_string(),
})?;
self.write_be(bytes.len() as u64)?;
self.write_slice(&bytes[..])
}
}