feat(proto): add protocol primitives
This commit is contained in:
parent
f70fd6250b
commit
aa649769d2
7 changed files with 173 additions and 0 deletions
22
proto/src/message/primitive/data.rs
Normal file
22
proto/src/message/primitive/data.rs
Normal 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())?)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue