This commit is contained in:
griffi-gh 2023-02-06 20:18:16 +01:00
parent 44088f76e5
commit dc06305956
2 changed files with 0 additions and 23 deletions

View file

@ -2,7 +2,6 @@ pub mod client;
pub mod server; pub mod server;
pub(crate) mod packet; pub(crate) mod packet;
pub(crate) mod common; pub(crate) mod common;
pub(crate) mod serializable;
pub use common::ClientId; pub use common::ClientId;
//pub(crate) trait Serializable: bincode::Encode + bincode::Decode {} //pub(crate) trait Serializable: bincode::Encode + bincode::Decode {}

View file

@ -1,22 +0,0 @@
use anyhow::Result;
use crate::BINCODE_CONFIG;
pub trait Serializable: bincode::Encode + bincode::Decode {
fn serialize(&self, buf: &mut [u8]) -> Result<()>;
fn deserialize(buf: &[u8]) -> Result<Self>;
fn serialize_to_vec(&self) -> Result<Vec<u8>> {
let mut buf = Vec::new();
self.serialize(&mut buf)?;
Ok(buf)
}
}
impl<T: bincode::Encode + bincode::Decode> Serializable for T {
fn serialize(&self, buf: &mut [u8]) -> Result<()> {
bincode::encode_into_slice(self, buf, BINCODE_CONFIG)?;
Ok(())
}
fn deserialize(buf: &[u8]) -> Result<Self> {
bincode::decode_from_slice(buf, BINCODE_CONFIG)?.0
}
}