heed_types/
serde_bincode.rs1use std::borrow::Cow;
2
3use heed_traits::{BoxedError, BytesDecode, BytesEncode};
4use serde::{Deserialize, Serialize};
5
6pub struct SerdeBincode<T>(std::marker::PhantomData<T>);
10
11impl<'a, T: 'a> BytesEncode<'a> for SerdeBincode<T>
12where
13 T: Serialize,
14{
15 type EItem = T;
16
17 fn bytes_encode(item: &'a Self::EItem) -> Result<Cow<[u8]>, BoxedError> {
18 bincode::serialize(item).map(Cow::Owned).map_err(Into::into)
19 }
20}
21
22impl<'a, T: 'a> BytesDecode<'a> for SerdeBincode<T>
23where
24 T: Deserialize<'a>,
25{
26 type DItem = T;
27
28 fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
29 bincode::deserialize(bytes).map_err(Into::into)
30 }
31}
32
33unsafe impl<T> Send for SerdeBincode<T> {}
34
35unsafe impl<T> Sync for SerdeBincode<T> {}