use std::borrow::Cow;
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
use serde::{Deserialize, Serialize};
pub struct SerdeBincode<T>(std::marker::PhantomData<T>);
impl<'a, T: 'a> BytesEncode<'a> for SerdeBincode<T>
where
T: Serialize,
{
type EItem = T;
fn bytes_encode(item: &'a Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
bincode::serialize(item).map(Cow::Owned).map_err(Into::into)
}
}
impl<'a, T: 'a> BytesDecode<'a> for SerdeBincode<T>
where
T: Deserialize<'a>,
{
type DItem = T;
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
bincode::deserialize(bytes).map_err(Into::into)
}
}
unsafe impl<T> Send for SerdeBincode<T> {}
unsafe impl<T> Sync for SerdeBincode<T> {}