feat(proto): add protocol primitives

This commit is contained in:
Jindřich Moravec 2023-12-11 16:36:52 +01:00
parent f70fd6250b
commit aa649769d2
7 changed files with 173 additions and 0 deletions

View file

@ -0,0 +1,22 @@
use crate::message::primitive::config::pg_proto_config;
use bincode::{Decode, Encode};
pub trait MessageData: Sized {
fn deserialize(data: &[u8]) -> anyhow::Result<Self>;
fn serialize(&self) -> anyhow::Result<Vec<u8>>;
}
impl<T> MessageData for T
where
T: Encode + Decode,
{
#[inline]
fn deserialize(data: &[u8]) -> anyhow::Result<Self> {
Ok(bincode::decode_from_slice(data, pg_proto_config())?.0)
}
#[inline]
fn serialize(&self) -> anyhow::Result<Vec<u8>> {
Ok(bincode::encode_to_vec(self, pg_proto_config())?)
}
}