reth_libmdbx/
codec.rs

1use crate::{Error, TransactionKind};
2use derive_more::{Debug, Deref, DerefMut};
3use std::{borrow::Cow, slice};
4
5/// Implement this to be able to decode data values
6pub trait TableObject: Sized {
7    /// Decodes the object from the given bytes.
8    fn decode(data_val: &[u8]) -> Result<Self, Error>;
9
10    /// Decodes the value directly from the given MDBX_val pointer.
11    ///
12    /// # Safety
13    ///
14    /// This should only in the context of an MDBX transaction.
15    #[doc(hidden)]
16    unsafe fn decode_val<K: TransactionKind>(
17        _: *const ffi::MDBX_txn,
18        data_val: ffi::MDBX_val,
19    ) -> Result<Self, Error> {
20        let s = slice::from_raw_parts(data_val.iov_base as *const u8, data_val.iov_len);
21        Self::decode(s)
22    }
23}
24
25impl TableObject for Cow<'_, [u8]> {
26    fn decode(_: &[u8]) -> Result<Self, Error> {
27        unreachable!()
28    }
29
30    #[doc(hidden)]
31    unsafe fn decode_val<K: TransactionKind>(
32        _txn: *const ffi::MDBX_txn,
33        data_val: ffi::MDBX_val,
34    ) -> Result<Self, Error> {
35        let s = slice::from_raw_parts(data_val.iov_base as *const u8, data_val.iov_len);
36
37        #[cfg(feature = "return-borrowed")]
38        {
39            Ok(Cow::Borrowed(s))
40        }
41
42        #[cfg(not(feature = "return-borrowed"))]
43        {
44            let is_dirty = (!K::IS_READ_ONLY) &&
45                crate::error::mdbx_result(ffi::mdbx_is_dirty(_txn, data_val.iov_base))?;
46
47            Ok(if is_dirty { Cow::Owned(s.to_vec()) } else { Cow::Borrowed(s) })
48        }
49    }
50}
51
52impl TableObject for Vec<u8> {
53    fn decode(data_val: &[u8]) -> Result<Self, Error> {
54        Ok(data_val.to_vec())
55    }
56}
57
58impl TableObject for () {
59    fn decode(_: &[u8]) -> Result<Self, Error> {
60        Ok(())
61    }
62
63    unsafe fn decode_val<K: TransactionKind>(
64        _: *const ffi::MDBX_txn,
65        _: ffi::MDBX_val,
66    ) -> Result<Self, Error> {
67        Ok(())
68    }
69}
70
71/// If you don't need the data itself, just its length.
72#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deref, DerefMut)]
73pub struct ObjectLength(pub usize);
74
75impl TableObject for ObjectLength {
76    fn decode(data_val: &[u8]) -> Result<Self, Error> {
77        Ok(Self(data_val.len()))
78    }
79}
80
81impl<const LEN: usize> TableObject for [u8; LEN] {
82    fn decode(data_val: &[u8]) -> Result<Self, Error> {
83        if data_val.len() != LEN {
84            return Err(Error::DecodeErrorLenDiff)
85        }
86        let mut a = [0; LEN];
87        a[..].copy_from_slice(data_val);
88        Ok(a)
89    }
90}