use crate::codec::{
Decode,
Encode,
};
use std::borrow::Cow;
pub struct Postcard;
impl<T> Encode<T> for Postcard
where
T: ?Sized + serde::Serialize,
{
type Encoder<'a> = Cow<'a, [u8]> where T: 'a;
fn encode(value: &T) -> Self::Encoder<'_> {
Cow::Owned(postcard::to_allocvec(value).expect(
"It should be impossible to fail unless serialization is not implemented, which is not true for our types.",
))
}
}
impl<T> Decode<T> for Postcard
where
T: serde::de::DeserializeOwned,
{
fn decode(bytes: &[u8]) -> anyhow::Result<T> {
Ok(postcard::from_bytes(bytes)?)
}
}