1 use smallvec::SmallVec;
6 /// Like SmallVec but for strings.
8 pub struct SmallStr<const N: usize>(SmallVec<[u8; N]>);
10 impl<const N: usize> SmallStr<N> {
12 pub fn new() -> Self {
13 SmallStr(SmallVec::default())
17 pub fn push_str(&mut self, s: &str) {
18 self.0.extend_from_slice(s.as_bytes());
22 pub fn empty(&self) -> bool {
27 pub fn spilled(&self) -> bool {
32 pub fn as_str(&self) -> &str {
33 unsafe { std::str::from_utf8_unchecked(self.0.as_slice()) }
37 impl<const N: usize> std::ops::Deref for SmallStr<N> {
41 fn deref(&self) -> &str {
46 impl<const N: usize, A: AsRef<str>> FromIterator<A> for SmallStr<N> {
48 fn from_iter<T>(iter: T) -> Self
50 T: IntoIterator<Item = A>,
52 let mut s = SmallStr::default();
58 impl<const N: usize, A: AsRef<str>> Extend<A> for SmallStr<N> {
60 fn extend<T>(&mut self, iter: T)
62 T: IntoIterator<Item = A>,
64 for a in iter.into_iter() {
65 self.push_str(a.as_ref());