-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathbytes.rs
52 lines (37 loc) · 1.32 KB
/
bytes.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
use std::convert::Infallible;
use heed_traits::{BoxedError, BytesDecode, BytesEncode};
/// Describes a byte slice `[u8]` that is totally borrowed and doesn't depend on
/// any [memory alignment].
///
/// [memory alignment]: std::mem::align_of()
pub enum Bytes {}
impl<'a> BytesEncode<'a> for Bytes {
type EItem = [u8];
type ReturnBytes = &'a [u8];
type Error = Infallible;
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
Ok(item)
}
}
impl<'a> BytesDecode<'a> for Bytes {
type DItem = &'a [u8];
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
Ok(bytes)
}
}
/// Like [`Bytes`], but always contains exactly `N` (the generic parameter) bytes.
pub enum FixedSizeBytes<const N: usize> {}
impl<'a, const N: usize> BytesEncode<'a> for FixedSizeBytes<N> {
type EItem = [u8; N];
type ReturnBytes = [u8; N]; // TODO &'a [u8; N] or [u8; N]
type Error = Infallible;
fn bytes_encode(item: &'a Self::EItem) -> Result<Self::ReturnBytes, Self::Error> {
Ok(*item)
}
}
impl<'a, const N: usize> BytesDecode<'a> for FixedSizeBytes<N> {
type DItem = [u8; N]; // TODO &'a [u8; N] or [u8; N]
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError> {
bytes.try_into().map_err(Into::into)
}
}