-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathserde_bincode.rs
49 lines (38 loc) · 1.16 KB
/
serde_bincode.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
use serde::{Deserialize, Serialize};
/// Describes a type that is [`Serialize`]/[`Deserialize`] and uses `bincode` to do so.
///
/// It can borrow bytes from the original slice.
pub struct SerdeBincode<T>(std::marker::PhantomData<T>);
impl<'a, T: 'a> BytesEncode<'a> for SerdeBincode<T>
where
T: Serialize,
{
type EItem = T;
type ReturnBytes = Vec<u8>;
type Error = bincode::Error;
fn zero_copy(_item: &Self::EItem) -> bool {
false
}
fn bytes_encode(item: &Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
bincode::serialize(item)
}
fn bytes_encode_into_writer<W: std::io::Write>(
item: &'a Self::EItem,
writer: W,
) -> Result<(), BoxedError> {
bincode::serialize_into(writer, item)?;
Ok(())
}
}
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> {}