oxigraph/storage/
small_string.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::str::{FromStr, Utf8Error};
use std::{fmt, str};

/// A small inline string
#[derive(Clone, Copy, Default)]
pub struct SmallString {
    inner: [u8; 16],
}

impl SmallString {
    #[inline]
    pub const fn new() -> Self {
        Self { inner: [0; 16] }
    }

    #[inline]
    pub fn from_utf8(bytes: &[u8]) -> Result<Self, BadSmallStringError> {
        Self::from_str(str::from_utf8(bytes).map_err(BadSmallStringError::BadUtf8)?)
    }

    #[inline]
    pub fn from_be_bytes(bytes: [u8; 16]) -> Result<Self, BadSmallStringError> {
        // We check that it is valid UTF-8
        str::from_utf8(&bytes.as_ref()[..bytes[15].into()])
            .map_err(BadSmallStringError::BadUtf8)?;
        Ok(Self { inner: bytes })
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.inner[15].into()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    #[allow(unsafe_code)]
    pub fn as_str(&self) -> &str {
        // SAFETY: safe because we ensured it in constructors
        unsafe { str::from_utf8_unchecked(self.as_bytes()) }
    }

    #[inline]
    pub fn as_bytes(&self) -> &[u8] {
        &self.inner[..self.len()]
    }

    #[inline]
    pub fn to_be_bytes(self) -> [u8; 16] {
        self.inner
    }
}

impl Deref for SmallString {
    type Target = str;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl AsRef<str> for SmallString {
    #[inline]
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for SmallString {
    #[inline]
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Debug for SmallString {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

impl fmt::Display for SmallString {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

impl PartialEq for SmallString {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl Eq for SmallString {}

impl PartialOrd for SmallString {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for SmallString {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl Hash for SmallString {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_str().hash(state)
    }
}

impl From<SmallString> for String {
    #[inline]
    fn from(value: SmallString) -> Self {
        value.as_str().into()
    }
}

impl<'a> From<&'a SmallString> for &'a str {
    #[inline]
    fn from(value: &'a SmallString) -> Self {
        value.as_str()
    }
}

impl FromStr for SmallString {
    type Err = BadSmallStringError;

    #[inline]
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if value.len() <= 15 {
            let mut inner = [0; 16];
            inner[..value.len()].copy_from_slice(value.as_bytes());
            inner[15] = value
                .len()
                .try_into()
                .map_err(|_| Self::Err::TooLong(value.len()))?;
            Ok(Self { inner })
        } else {
            Err(Self::Err::TooLong(value.len()))
        }
    }
}

impl<'a> TryFrom<&'a str> for SmallString {
    type Error = BadSmallStringError;

    #[inline]
    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        Self::from_str(value)
    }
}

#[derive(Debug, Clone, Copy, thiserror::Error)]
pub enum BadSmallStringError {
    #[error("small strings could only contain at most 15 characters, found {0}")]
    TooLong(usize),
    #[error(transparent)]
    BadUtf8(#[from] Utf8Error),
}