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
use std::error::Error as StdError;
use std::marker;
use std::result::Result as StdResult;
use crate::{Result, Error};
#[derive(Default)]
pub struct LazyDecode<C>(marker::PhantomData<C>);
impl<'a, C: 'static> heed_traits::BytesDecode<'a> for LazyDecode<C> {
type DItem = Lazy<'a, C>;
fn bytes_decode(bytes: &'a [u8]) -> StdResult<Self::DItem, Box<dyn StdError>> {
Ok(Lazy { data: bytes, _phantom: marker::PhantomData })
}
}
#[derive(Copy, Clone)]
pub struct Lazy<'a, C> {
data: &'a [u8],
_phantom: marker::PhantomData<C>,
}
impl<'a, C: heed_traits::BytesDecode<'a>> Lazy<'a, C> {
pub fn decode(&self) -> Result<C::DItem> {
C::bytes_decode(self.data).map_err(Error::Decoding)
}
}