use std::{
io::{self, Cursor},
pin::Pin,
sync::Arc,
};
use bytes::{Bytes, BytesMut};
use serde::{Deserialize, Serialize};
use tokio_serde::{Deserializer, Serializer};
use super::Message;
#[derive(Debug)]
pub struct MessagePackFormat;
impl<P> Serializer<Arc<Message<P>>> for MessagePackFormat
where
Message<P>: Serialize,
{
type Error = io::Error;
#[inline]
fn serialize(self: Pin<&mut Self>, item: &Arc<Message<P>>) -> Result<Bytes, Self::Error> {
rmp_serde::to_vec(item)
.map(Into::into)
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
}
}
impl<P> Deserializer<Message<P>> for MessagePackFormat
where
for<'de> Message<P>: Deserialize<'de>,
{
type Error = io::Error;
#[inline]
fn deserialize(self: Pin<&mut Self>, src: &BytesMut) -> Result<Message<P>, Self::Error> {
rmp_serde::from_read(Cursor::new(src))
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
}
}