Skip to main content

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_boxed_slice(self) -> Box<[u8]> {
13        match self {
14            ValueBuffer::Borrowed(b) => b.into(),
15            ValueBuffer::Vec(v) => v.into_boxed_slice(),
16            ValueBuffer::SmallVec(sv) => sv.into_vec().into_boxed_slice(),
17        }
18    }
19}
20
21impl<'l> From<&'l [u8]> for ValueBuffer<'l> {
22    fn from(b: &'l [u8]) -> Self {
23        ValueBuffer::Borrowed(b)
24    }
25}
26
27impl From<Vec<u8>> for ValueBuffer<'_> {
28    fn from(v: Vec<u8>) -> Self {
29        ValueBuffer::Vec(v)
30    }
31}
32
33impl From<Box<[u8]>> for ValueBuffer<'_> {
34    fn from(v: Box<[u8]>) -> Self {
35        ValueBuffer::Vec(v.into_vec())
36    }
37}
38
39impl From<SmallVec<[u8; 16]>> for ValueBuffer<'_> {
40    fn from(sv: SmallVec<[u8; 16]>) -> Self {
41        ValueBuffer::SmallVec(sv)
42    }
43}
44
45impl<'l> From<Cow<'l, [u8]>> for ValueBuffer<'l> {
46    fn from(c: Cow<'l, [u8]>) -> Self {
47        match c {
48            Cow::Borrowed(b) => ValueBuffer::Borrowed(b),
49            Cow::Owned(o) => ValueBuffer::Vec(o),
50        }
51    }
52}
53
54impl Deref for ValueBuffer<'_> {
55    type Target = [u8];
56
57    fn deref(&self) -> &Self::Target {
58        match self {
59            ValueBuffer::Borrowed(b) => b,
60            ValueBuffer::Vec(v) => v,
61            ValueBuffer::SmallVec(sv) => sv,
62        }
63    }
64}