blob: 800acb1b03e5ae2a5b624cbca155052adebe0da1 (
plain)
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
|
use smallvec::SmallVec;
#[cfg(test)]
mod tests;
/// Like SmallVec but for strings.
#[derive(Default)]
pub struct SmallStr<const N: usize>(SmallVec<[u8; N]>);
impl<const N: usize> SmallStr<N> {
#[inline]
pub fn new() -> Self {
SmallStr(SmallVec::default())
}
#[inline]
pub fn push_str(&mut self, s: &str) {
self.0.extend_from_slice(s.as_bytes());
}
#[inline]
pub fn empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn spilled(&self) -> bool {
self.0.spilled()
}
#[inline]
pub fn as_str(&self) -> &str {
unsafe { std::str::from_utf8_unchecked(self.0.as_slice()) }
}
}
impl<const N: usize> std::ops::Deref for SmallStr<N> {
type Target = str;
#[inline]
fn deref(&self) -> &str {
self.as_str()
}
}
impl<const N: usize, A: AsRef<str>> FromIterator<A> for SmallStr<N> {
#[inline]
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = A>,
{
let mut s = SmallStr::default();
s.extend(iter);
s
}
}
impl<const N: usize, A: AsRef<str>> Extend<A> for SmallStr<N> {
#[inline]
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = A>,
{
for a in iter.into_iter() {
self.push_str(a.as_ref());
}
}
}
|