1use std::marker;
23use heed_traits::BoxedError;
45/// Lazily decodes the data bytes.
6///
7/// It can be used to avoid CPU-intensive decoding before making sure that it
8/// actually needs to be decoded (e.g. based on the key).
9#[derive(Default)]
10pub struct LazyDecode<C>(marker::PhantomData<C>);
1112impl<'a, C: 'static> heed_traits::BytesDecode<'a> for LazyDecode<C> {
13type DItem = Lazy<'a, C>;
1415fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
16Ok(Lazy { data: bytes, _phantom: marker::PhantomData })
17 }
18}
1920/// Owns bytes that can be decoded on demand.
21#[derive(Copy, Clone)]
22pub struct Lazy<'a, C> {
23 data: &'a [u8],
24 _phantom: marker::PhantomData<C>,
25}
2627impl<'a, C: heed_traits::BytesDecode<'a>> Lazy<'a, C> {
28/// Change the codec type of the given bytes, specifying the new codec.
29pub fn remap<NC>(&self) -> Lazy<'a, NC> {
30 Lazy { data: self.data, _phantom: marker::PhantomData }
31 }
3233/// Decode the given bytes as `DItem`.
34pub fn decode(&self) -> Result<C::DItem, BoxedError> {
35 C::bytes_decode(self.data)
36 }
37}