use crate::{
codec::{Patch, Post, Put},
ContentType, Decodes, Encodes, Format, FormatType,
};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};
pub struct BitcodeSerdeEncoding;
impl ContentType for BitcodeSerdeEncoding {
const CONTENT_TYPE: &'static str = "application/x-bitcode-serde";
}
impl FormatType for BitcodeSerdeEncoding {
const FORMAT_TYPE: Format = Format::Binary;
}
impl<T> Encodes<T> for BitcodeSerdeEncoding
where
T: Serialize,
{
type Error = bitcode::Error;
fn encode(value: &T) -> Result<Bytes, Self::Error> {
bitcode::serialize(value).map(Bytes::from)
}
}
impl<T> Decodes<T> for BitcodeSerdeEncoding
where
T: DeserializeOwned,
{
type Error = bitcode::Error;
fn decode(bytes: Bytes) -> Result<T, Self::Error> {
bitcode::deserialize(bytes.as_ref())
}
}
pub type BitcodeSerde = Post<BitcodeSerdeEncoding>;
pub type PatchBitcodeSerde = Patch<BitcodeSerdeEncoding>;
pub type PutBitcodeSerde = Put<BitcodeSerdeEncoding>;