23 lines
701 B
Rust
23 lines
701 B
Rust
use crate::message::primitive::config::pg_proto_config;
|
|
use bincode::{Decode, Encode};
|
|
use crate::message::errors::{ProtoDeserializeError, ProtoSerializeError};
|
|
|
|
pub trait MessageData: Sized {
|
|
fn serialize(&self) -> Result<Vec<u8>, ProtoSerializeError>;
|
|
fn deserialize(data: &[u8]) -> Result<Self, ProtoDeserializeError>;
|
|
}
|
|
|
|
impl<T> MessageData for T
|
|
where
|
|
T: Encode + Decode,
|
|
{
|
|
#[inline]
|
|
fn serialize(&self) -> Result<Vec<u8>, ProtoSerializeError> {
|
|
Ok(bincode::encode_to_vec(self, pg_proto_config())?)
|
|
}
|
|
|
|
#[inline]
|
|
fn deserialize(data: &[u8]) -> Result<Self, ProtoDeserializeError> {
|
|
Ok(bincode::decode_from_slice(data, pg_proto_config())?.0)
|
|
}
|
|
}
|