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