File size: 1,552 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::{borrow::Cow, ops::Deref};

use smallvec::SmallVec;

pub enum ValueBuffer<'l> {
    Borrowed(&'l [u8]),
    Vec(Vec<u8>),
    SmallVec(SmallVec<[u8; 16]>),
}

impl ValueBuffer<'_> {
    pub fn into_vec(self) -> Vec<u8> {
        match self {
            ValueBuffer::Borrowed(b) => b.to_vec(),
            ValueBuffer::Vec(v) => v,
            ValueBuffer::SmallVec(sv) => sv.into_vec(),
        }
    }

    pub fn into_small_vec(self) -> SmallVec<[u8; 16]> {
        match self {
            ValueBuffer::Borrowed(b) => SmallVec::from_slice(b),
            ValueBuffer::Vec(v) => SmallVec::from_vec(v),
            ValueBuffer::SmallVec(sv) => sv,
        }
    }
}

impl<'l> From<&'l [u8]> for ValueBuffer<'l> {
    fn from(b: &'l [u8]) -> Self {
        ValueBuffer::Borrowed(b)
    }
}

impl From<Vec<u8>> for ValueBuffer<'_> {
    fn from(v: Vec<u8>) -> Self {
        ValueBuffer::Vec(v)
    }
}

impl From<SmallVec<[u8; 16]>> for ValueBuffer<'_> {
    fn from(sv: SmallVec<[u8; 16]>) -> Self {
        ValueBuffer::SmallVec(sv)
    }
}

impl<'l> From<Cow<'l, [u8]>> for ValueBuffer<'l> {
    fn from(c: Cow<'l, [u8]>) -> Self {
        match c {
            Cow::Borrowed(b) => ValueBuffer::Borrowed(b),
            Cow::Owned(o) => ValueBuffer::Vec(o),
        }
    }
}

impl Deref for ValueBuffer<'_> {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        match self {
            ValueBuffer::Borrowed(b) => b,
            ValueBuffer::Vec(v) => v,
            ValueBuffer::SmallVec(sv) => sv,
        }
    }
}