-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcodec.rs
117 lines (101 loc) · 2.92 KB
/
codec.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::{error::mdbx_result, Error, TransactionKind};
use derive_more::{Deref, DerefMut, Display};
use std::{borrow::Cow, slice};
use thiserror::Error;
/// Implement this to be able to decode data values
pub trait Decodable<'tx> {
fn decode(data_val: &[u8]) -> Result<Self, Error>
where
Self: Sized;
#[doc(hidden)]
unsafe fn decode_val<K: TransactionKind>(
_: *const ffi::MDBX_txn,
data_val: &ffi::MDBX_val,
) -> Result<Self, Error>
where
Self: Sized,
{
let s = slice::from_raw_parts(data_val.iov_base as *const u8, data_val.iov_len);
Decodable::decode(s)
}
}
impl<'tx> Decodable<'tx> for Cow<'tx, [u8]> {
fn decode(_: &[u8]) -> Result<Self, Error> {
unreachable!()
}
#[doc(hidden)]
unsafe fn decode_val<K: TransactionKind>(
txn: *const ffi::MDBX_txn,
data_val: &ffi::MDBX_val,
) -> Result<Self, Error> {
let is_dirty = (!K::ONLY_CLEAN) && mdbx_result(ffi::mdbx_is_dirty(txn, data_val.iov_base))?;
let s = slice::from_raw_parts(data_val.iov_base as *const u8, data_val.iov_len);
Ok(if is_dirty {
Cow::Owned(s.to_vec())
} else {
Cow::Borrowed(s)
})
}
}
#[cfg(feature = "lifetimed-bytes")]
impl<'tx> Decodable<'tx> for lifetimed_bytes::Bytes<'tx> {
fn decode(_: &[u8]) -> Result<Self, Error> {
unreachable!()
}
#[doc(hidden)]
unsafe fn decode_val<K: TransactionKind>(
txn: *const ffi::MDBX_txn,
data_val: &ffi::MDBX_val,
) -> Result<Self, Error> {
Cow::<'tx, [u8]>::decode_val::<K>(txn, data_val).map(From::from)
}
}
impl Decodable<'_> for Vec<u8> {
fn decode(data_val: &[u8]) -> Result<Self, Error>
where
Self: Sized,
{
Ok(data_val.to_vec())
}
}
impl Decodable<'_> for () {
fn decode(_: &[u8]) -> Result<Self, Error> {
Ok(())
}
unsafe fn decode_val<K: TransactionKind>(
_: *const ffi::MDBX_txn,
_: &ffi::MDBX_val,
) -> Result<Self, Error> {
Ok(())
}
}
/// If you don't need the data itself, just its length.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Deref, DerefMut)]
pub struct ObjectLength(pub usize);
impl Decodable<'_> for ObjectLength {
fn decode(data_val: &[u8]) -> Result<Self, Error>
where
Self: Sized,
{
Ok(Self(data_val.len()))
}
}
impl<const LEN: usize> Decodable<'_> for [u8; LEN] {
fn decode(data_val: &[u8]) -> Result<Self, Error>
where
Self: Sized,
{
#[derive(Clone, Debug, Display, Error)]
struct InvalidSize<const LEN: usize> {
got: usize,
}
if data_val.len() != LEN {
return Err(Error::DecodeError(Box::new(InvalidSize::<LEN> {
got: data_val.len(),
})));
}
let mut a = [0; LEN];
a[..].copy_from_slice(data_val);
Ok(a)
}
}