turbo_persistence/
value_buf.rs

1use std::{borrow::Cow, ops::Deref};
2
3use smallvec::SmallVec;
4
5pub enum ValueBuffer<'l> {
6    Borrowed(&'l [u8]),
7    Vec(Vec<u8>),
8    SmallVec(SmallVec<[u8; 16]>),
9}
10
11impl ValueBuffer<'_> {
12    pub fn into_vec(self) -> Vec<u8> {
13        match self {
14            ValueBuffer::Borrowed(b) => b.to_vec(),
15            ValueBuffer::Vec(v) => v,
16            ValueBuffer::SmallVec(sv) => sv.into_vec(),
17        }
18    }
19
20    pub fn into_small_vec(self) -> SmallVec<[u8; 16]> {
21        match self {
22            ValueBuffer::Borrowed(b) => SmallVec::from_slice(b),
23            ValueBuffer::Vec(v) => SmallVec::from_vec(v),
24            ValueBuffer::SmallVec(sv) => sv,
25        }
26    }
27}
28
29impl<'l> From<&'l [u8]> for ValueBuffer<'l> {
30    fn from(b: &'l [u8]) -> Self {
31        ValueBuffer::Borrowed(b)
32    }
33}
34
35impl From<Vec<u8>> for ValueBuffer<'_> {
36    fn from(v: Vec<u8>) -> Self {
37        ValueBuffer::Vec(v)
38    }
39}
40
41impl From<SmallVec<[u8; 16]>> for ValueBuffer<'_> {
42    fn from(sv: SmallVec<[u8; 16]>) -> Self {
43        ValueBuffer::SmallVec(sv)
44    }
45}
46
47impl<'l> From<Cow<'l, [u8]>> for ValueBuffer<'l> {
48    fn from(c: Cow<'l, [u8]>) -> Self {
49        match c {
50            Cow::Borrowed(b) => ValueBuffer::Borrowed(b),
51            Cow::Owned(o) => ValueBuffer::Vec(o),
52        }
53    }
54}
55
56impl Deref for ValueBuffer<'_> {
57    type Target = [u8];
58
59    fn deref(&self) -> &Self::Target {
60        match self {
61            ValueBuffer::Borrowed(b) => b,
62            ValueBuffer::Vec(v) => v,
63            ValueBuffer::SmallVec(sv) => sv,
64        }
65    }
66}