smol_str/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3
4extern crate alloc;
5
6use alloc::{borrow::Cow, boxed::Box, string::String, sync::Arc};
7use core::{
8    borrow::Borrow,
9    cmp::{self, Ordering},
10    convert::Infallible,
11    fmt, hash, iter, mem, ops,
12    str::FromStr,
13};
14
15/// A `SmolStr` is a string type that has the following properties:
16///
17/// * `size_of::<SmolStr>() == 24` (therefor `== size_of::<String>()` on 64 bit platforms)
18/// * `Clone` is `O(1)`
19/// * Strings are stack-allocated if they are:
20///     * Up to 23 bytes long
21///     * Longer than 23 bytes, but substrings of `WS` (see below). Such strings consist
22///       solely of consecutive newlines, followed by consecutive spaces
23/// * If a string does not satisfy the aforementioned conditions, it is heap-allocated
24/// * Additionally, a `SmolStr` can be explicitly created from a `&'static str` without allocation
25///
26/// Unlike `String`, however, `SmolStr` is immutable. The primary use case for
27/// `SmolStr` is a good enough default storage for tokens of typical programming
28/// languages. Strings consisting of a series of newlines, followed by a series of
29/// whitespace are a typical pattern in computer programs because of indentation.
30/// Note that a specialized interner might be a better solution for some use cases.
31///
32/// `WS`: A string of 32 newlines followed by 128 spaces.
33pub struct SmolStr(Repr);
34
35impl SmolStr {
36    /// Constructs an inline variant of `SmolStr`.
37    ///
38    /// This never allocates.
39    ///
40    /// # Panics
41    ///
42    /// Panics if `text.len() > 23`.
43    #[inline]
44    pub const fn new_inline(text: &str) -> SmolStr {
45        assert!(text.len() <= INLINE_CAP); // avoids bounds checks in loop
46
47        let text = text.as_bytes();
48        let mut buf = [0; INLINE_CAP];
49        let mut i = 0;
50        while i < text.len() {
51            buf[i] = text[i];
52            i += 1
53        }
54        SmolStr(Repr::Inline {
55            // SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
56            // as we asserted it.
57            len: unsafe { InlineSize::transmute_from_u8(text.len() as u8) },
58            buf,
59        })
60    }
61
62    /// Constructs a `SmolStr` from a statically allocated string.
63    ///
64    /// This never allocates.
65    #[inline(always)]
66    pub const fn new_static(text: &'static str) -> SmolStr {
67        // NOTE: this never uses the inline storage; if a canonical
68        // representation is needed, we could check for `len() < INLINE_CAP`
69        // and call `new_inline`, but this would mean an extra branch.
70        SmolStr(Repr::Static(text))
71    }
72
73    /// Constructs a `SmolStr` from a `str`, heap-allocating if necessary.
74    #[inline(always)]
75    pub fn new(text: impl AsRef<str>) -> SmolStr {
76        SmolStr(Repr::new(text.as_ref()))
77    }
78
79    /// Returns a `&str` slice of this `SmolStr`.
80    #[inline(always)]
81    pub fn as_str(&self) -> &str {
82        self.0.as_str()
83    }
84
85    /// Returns the length of `self` in bytes.
86    #[inline(always)]
87    pub fn len(&self) -> usize {
88        self.0.len()
89    }
90
91    /// Returns `true` if `self` has a length of zero bytes.
92    #[inline(always)]
93    pub fn is_empty(&self) -> bool {
94        self.0.is_empty()
95    }
96
97    /// Returns `true` if `self` is heap-allocated.
98    #[inline(always)]
99    pub const fn is_heap_allocated(&self) -> bool {
100        matches!(self.0, Repr::Heap(..))
101    }
102}
103
104impl Clone for SmolStr {
105    #[inline]
106    fn clone(&self) -> Self {
107        if !self.is_heap_allocated() {
108            // SAFETY: We verified that the payload of `Repr` is a POD
109            return unsafe { core::ptr::read(self as *const SmolStr) };
110        }
111        Self(self.0.clone())
112    }
113}
114
115impl Default for SmolStr {
116    #[inline(always)]
117    fn default() -> SmolStr {
118        SmolStr(Repr::Inline {
119            len: InlineSize::_V0,
120            buf: [0; INLINE_CAP],
121        })
122    }
123}
124
125impl ops::Deref for SmolStr {
126    type Target = str;
127
128    #[inline(always)]
129    fn deref(&self) -> &str {
130        self.as_str()
131    }
132}
133
134// region: PartialEq implementations
135
136impl Eq for SmolStr {}
137impl PartialEq<SmolStr> for SmolStr {
138    fn eq(&self, other: &SmolStr) -> bool {
139        self.0.ptr_eq(&other.0) || self.as_str() == other.as_str()
140    }
141}
142
143impl PartialEq<str> for SmolStr {
144    #[inline(always)]
145    fn eq(&self, other: &str) -> bool {
146        self.as_str() == other
147    }
148}
149
150impl PartialEq<SmolStr> for str {
151    #[inline(always)]
152    fn eq(&self, other: &SmolStr) -> bool {
153        other == self
154    }
155}
156
157impl<'a> PartialEq<&'a str> for SmolStr {
158    #[inline(always)]
159    fn eq(&self, other: &&'a str) -> bool {
160        self == *other
161    }
162}
163
164impl<'a> PartialEq<SmolStr> for &'a str {
165    #[inline(always)]
166    fn eq(&self, other: &SmolStr) -> bool {
167        *self == other
168    }
169}
170
171impl PartialEq<String> for SmolStr {
172    #[inline(always)]
173    fn eq(&self, other: &String) -> bool {
174        self.as_str() == other
175    }
176}
177
178impl PartialEq<SmolStr> for String {
179    #[inline(always)]
180    fn eq(&self, other: &SmolStr) -> bool {
181        other == self
182    }
183}
184
185impl<'a> PartialEq<&'a String> for SmolStr {
186    #[inline(always)]
187    fn eq(&self, other: &&'a String) -> bool {
188        self == *other
189    }
190}
191
192impl<'a> PartialEq<SmolStr> for &'a String {
193    #[inline(always)]
194    fn eq(&self, other: &SmolStr) -> bool {
195        *self == other
196    }
197}
198// endregion: PartialEq implementations
199
200impl Ord for SmolStr {
201    fn cmp(&self, other: &SmolStr) -> Ordering {
202        self.as_str().cmp(other.as_str())
203    }
204}
205
206impl PartialOrd for SmolStr {
207    fn partial_cmp(&self, other: &SmolStr) -> Option<Ordering> {
208        Some(self.cmp(other))
209    }
210}
211
212impl hash::Hash for SmolStr {
213    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
214        self.as_str().hash(hasher);
215    }
216}
217
218impl fmt::Debug for SmolStr {
219    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
220        fmt::Debug::fmt(self.as_str(), f)
221    }
222}
223
224impl fmt::Display for SmolStr {
225    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
226        fmt::Display::fmt(self.as_str(), f)
227    }
228}
229
230impl iter::FromIterator<char> for SmolStr {
231    fn from_iter<I: iter::IntoIterator<Item = char>>(iter: I) -> SmolStr {
232        from_char_iter(iter.into_iter())
233    }
234}
235
236fn from_char_iter(mut iter: impl Iterator<Item = char>) -> SmolStr {
237    let (min_size, _) = iter.size_hint();
238    if min_size > INLINE_CAP {
239        let heap: String = iter.collect();
240        if heap.len() <= INLINE_CAP {
241            // size hint lied
242            return SmolStr::new_inline(&heap);
243        }
244        return SmolStr(Repr::Heap(heap.into_boxed_str().into()));
245    }
246    let mut len = 0;
247    let mut buf = [0u8; INLINE_CAP];
248    while let Some(ch) = iter.next() {
249        let size = ch.len_utf8();
250        if size + len > INLINE_CAP {
251            let (min_remaining, _) = iter.size_hint();
252            let mut heap = String::with_capacity(size + len + min_remaining);
253            heap.push_str(core::str::from_utf8(&buf[..len]).unwrap());
254            heap.push(ch);
255            heap.extend(iter);
256            return SmolStr(Repr::Heap(heap.into_boxed_str().into()));
257        }
258        ch.encode_utf8(&mut buf[len..]);
259        len += size;
260    }
261    SmolStr(Repr::Inline {
262        // SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
263        // as we otherwise return early.
264        len: unsafe { InlineSize::transmute_from_u8(len as u8) },
265        buf,
266    })
267}
268
269fn build_from_str_iter<T>(mut iter: impl Iterator<Item = T>) -> SmolStr
270where
271    T: AsRef<str>,
272    String: iter::Extend<T>,
273{
274    let mut len = 0;
275    let mut buf = [0u8; INLINE_CAP];
276    while let Some(slice) = iter.next() {
277        let slice = slice.as_ref();
278        let size = slice.len();
279        if size + len > INLINE_CAP {
280            let mut heap = String::with_capacity(size + len);
281            heap.push_str(core::str::from_utf8(&buf[..len]).unwrap());
282            heap.push_str(slice);
283            heap.extend(iter);
284            return SmolStr(Repr::Heap(heap.into_boxed_str().into()));
285        }
286        buf[len..][..size].copy_from_slice(slice.as_bytes());
287        len += size;
288    }
289    SmolStr(Repr::Inline {
290        // SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
291        // as we otherwise return early.
292        len: unsafe { InlineSize::transmute_from_u8(len as u8) },
293        buf,
294    })
295}
296
297impl iter::FromIterator<String> for SmolStr {
298    fn from_iter<I: iter::IntoIterator<Item = String>>(iter: I) -> SmolStr {
299        build_from_str_iter(iter.into_iter())
300    }
301}
302
303impl<'a> iter::FromIterator<&'a String> for SmolStr {
304    fn from_iter<I: iter::IntoIterator<Item = &'a String>>(iter: I) -> SmolStr {
305        SmolStr::from_iter(iter.into_iter().map(|x| x.as_str()))
306    }
307}
308
309impl<'a> iter::FromIterator<&'a str> for SmolStr {
310    fn from_iter<I: iter::IntoIterator<Item = &'a str>>(iter: I) -> SmolStr {
311        build_from_str_iter(iter.into_iter())
312    }
313}
314
315impl AsRef<str> for SmolStr {
316    #[inline(always)]
317    fn as_ref(&self) -> &str {
318        self.as_str()
319    }
320}
321
322impl AsRef<[u8]> for SmolStr {
323    #[inline(always)]
324    fn as_ref(&self) -> &[u8] {
325        self.as_str().as_bytes()
326    }
327}
328
329#[cfg(feature = "std")]
330impl AsRef<std::ffi::OsStr> for SmolStr {
331    #[inline(always)]
332    fn as_ref(&self) -> &std::ffi::OsStr {
333        AsRef::<std::ffi::OsStr>::as_ref(self.as_str())
334    }
335}
336
337#[cfg(feature = "std")]
338impl AsRef<std::path::Path> for SmolStr {
339    #[inline(always)]
340    fn as_ref(&self) -> &std::path::Path {
341        AsRef::<std::path::Path>::as_ref(self.as_str())
342    }
343}
344
345impl From<&str> for SmolStr {
346    #[inline]
347    fn from(s: &str) -> SmolStr {
348        SmolStr::new(s)
349    }
350}
351
352impl From<&mut str> for SmolStr {
353    #[inline]
354    fn from(s: &mut str) -> SmolStr {
355        SmolStr::new(s)
356    }
357}
358
359impl From<&String> for SmolStr {
360    #[inline]
361    fn from(s: &String) -> SmolStr {
362        SmolStr::new(s)
363    }
364}
365
366impl From<String> for SmolStr {
367    #[inline(always)]
368    fn from(text: String) -> Self {
369        Self::new(text)
370    }
371}
372
373impl From<Box<str>> for SmolStr {
374    #[inline]
375    fn from(s: Box<str>) -> SmolStr {
376        SmolStr::new(s)
377    }
378}
379
380impl From<Arc<str>> for SmolStr {
381    #[inline]
382    fn from(s: Arc<str>) -> SmolStr {
383        let repr = Repr::new_on_stack(s.as_ref()).unwrap_or_else(|| Repr::Heap(s));
384        Self(repr)
385    }
386}
387
388impl<'a> From<Cow<'a, str>> for SmolStr {
389    #[inline]
390    fn from(s: Cow<'a, str>) -> SmolStr {
391        SmolStr::new(s)
392    }
393}
394
395impl From<SmolStr> for Arc<str> {
396    #[inline(always)]
397    fn from(text: SmolStr) -> Self {
398        match text.0 {
399            Repr::Heap(data) => data,
400            _ => text.as_str().into(),
401        }
402    }
403}
404
405impl From<SmolStr> for String {
406    #[inline(always)]
407    fn from(text: SmolStr) -> Self {
408        text.as_str().into()
409    }
410}
411
412impl Borrow<str> for SmolStr {
413    #[inline(always)]
414    fn borrow(&self) -> &str {
415        self.as_str()
416    }
417}
418
419impl FromStr for SmolStr {
420    type Err = Infallible;
421
422    #[inline]
423    fn from_str(s: &str) -> Result<SmolStr, Self::Err> {
424        Ok(SmolStr::from(s))
425    }
426}
427
428const INLINE_CAP: usize = InlineSize::_V23 as usize;
429const N_NEWLINES: usize = 32;
430const N_SPACES: usize = 128;
431const WS: &str =
432    "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n                                                                                                                                ";
433const _: () = {
434    assert!(WS.len() == N_NEWLINES + N_SPACES);
435    assert!(WS.as_bytes()[N_NEWLINES - 1] == b'\n');
436    assert!(WS.as_bytes()[N_NEWLINES] == b' ');
437};
438
439/// A [`u8`] with a bunch of niches.
440#[derive(Clone, Copy, Debug, PartialEq)]
441#[repr(u8)]
442enum InlineSize {
443    _V0 = 0,
444    _V1,
445    _V2,
446    _V3,
447    _V4,
448    _V5,
449    _V6,
450    _V7,
451    _V8,
452    _V9,
453    _V10,
454    _V11,
455    _V12,
456    _V13,
457    _V14,
458    _V15,
459    _V16,
460    _V17,
461    _V18,
462    _V19,
463    _V20,
464    _V21,
465    _V22,
466    _V23,
467}
468
469impl InlineSize {
470    /// SAFETY: `value` must be less than or equal to [`INLINE_CAP`]
471    #[inline(always)]
472    const unsafe fn transmute_from_u8(value: u8) -> Self {
473        debug_assert!(value <= InlineSize::_V23 as u8);
474        // SAFETY: The caller is responsible to uphold this invariant
475        unsafe { mem::transmute::<u8, Self>(value) }
476    }
477}
478
479#[derive(Clone, Debug)]
480enum Repr {
481    Inline {
482        len: InlineSize,
483        buf: [u8; INLINE_CAP],
484    },
485    Static(&'static str),
486    Heap(Arc<str>),
487}
488
489impl Repr {
490    /// This function tries to create a new Repr::Inline or Repr::Static
491    /// If it isn't possible, this function returns None
492    fn new_on_stack<T>(text: T) -> Option<Self>
493    where
494        T: AsRef<str>,
495    {
496        let text = text.as_ref();
497
498        let len = text.len();
499        if len <= INLINE_CAP {
500            let mut buf = [0; INLINE_CAP];
501            buf[..len].copy_from_slice(text.as_bytes());
502            return Some(Repr::Inline {
503                // SAFETY: We know that `len` is less than or equal to the maximum value of `InlineSize`
504                len: unsafe { InlineSize::transmute_from_u8(len as u8) },
505                buf,
506            });
507        }
508
509        if len <= N_NEWLINES + N_SPACES {
510            let bytes = text.as_bytes();
511            let possible_newline_count = cmp::min(len, N_NEWLINES);
512            let newlines = bytes[..possible_newline_count]
513                .iter()
514                .take_while(|&&b| b == b'\n')
515                .count();
516            let possible_space_count = len - newlines;
517            if possible_space_count <= N_SPACES && bytes[newlines..].iter().all(|&b| b == b' ') {
518                let spaces = possible_space_count;
519                let substring = &WS[N_NEWLINES - newlines..N_NEWLINES + spaces];
520                return Some(Repr::Static(substring));
521            }
522        }
523        None
524    }
525
526    fn new(text: &str) -> Self {
527        Self::new_on_stack(text).unwrap_or_else(|| Repr::Heap(Arc::from(text)))
528    }
529
530    #[inline(always)]
531    fn len(&self) -> usize {
532        match self {
533            Repr::Heap(data) => data.len(),
534            Repr::Static(data) => data.len(),
535            Repr::Inline { len, .. } => *len as usize,
536        }
537    }
538
539    #[inline(always)]
540    fn is_empty(&self) -> bool {
541        match self {
542            Repr::Heap(data) => data.is_empty(),
543            Repr::Static(data) => data.is_empty(),
544            &Repr::Inline { len, .. } => len as u8 == 0,
545        }
546    }
547
548    #[inline]
549    fn as_str(&self) -> &str {
550        match self {
551            Repr::Heap(data) => data,
552            Repr::Static(data) => data,
553            Repr::Inline { len, buf } => {
554                let len = *len as usize;
555                // SAFETY: len is guaranteed to be <= INLINE_CAP
556                let buf = unsafe { buf.get_unchecked(..len) };
557                // SAFETY: buf is guaranteed to be valid utf8 for ..len bytes
558                unsafe { ::core::str::from_utf8_unchecked(buf) }
559            }
560        }
561    }
562
563    fn ptr_eq(&self, other: &Self) -> bool {
564        match (self, other) {
565            (Self::Heap(l0), Self::Heap(r0)) => Arc::ptr_eq(l0, r0),
566            (Self::Static(l0), Self::Static(r0)) => core::ptr::eq(l0, r0),
567            (
568                Self::Inline {
569                    len: l_len,
570                    buf: l_buf,
571                },
572                Self::Inline {
573                    len: r_len,
574                    buf: r_buf,
575                },
576            ) => l_len == r_len && l_buf == r_buf,
577            _ => false,
578        }
579    }
580}
581
582/// Convert value to [`SmolStr`] using [`fmt::Display`], potentially without allocating.
583///
584/// Almost identical to [`ToString`], but converts to `SmolStr` instead.
585pub trait ToSmolStr {
586    fn to_smolstr(&self) -> SmolStr;
587}
588
589/// [`str`] methods producing [`SmolStr`]s.
590pub trait StrExt: private::Sealed {
591    /// Returns the lowercase equivalent of this string slice as a new [`SmolStr`],
592    /// potentially without allocating.
593    ///
594    /// See [`str::to_lowercase`].
595    #[must_use = "this returns a new SmolStr without modifying the original"]
596    fn to_lowercase_smolstr(&self) -> SmolStr;
597
598    /// Returns the uppercase equivalent of this string slice as a new [`SmolStr`],
599    /// potentially without allocating.
600    ///
601    /// See [`str::to_uppercase`].
602    #[must_use = "this returns a new SmolStr without modifying the original"]
603    fn to_uppercase_smolstr(&self) -> SmolStr;
604
605    /// Returns the ASCII lowercase equivalent of this string slice as a new [`SmolStr`],
606    /// potentially without allocating.
607    ///
608    /// See [`str::to_ascii_lowercase`].
609    #[must_use = "this returns a new SmolStr without modifying the original"]
610    fn to_ascii_lowercase_smolstr(&self) -> SmolStr;
611
612    /// Returns the ASCII uppercase equivalent of this string slice as a new [`SmolStr`],
613    /// potentially without allocating.
614    ///
615    /// See [`str::to_ascii_uppercase`].
616    #[must_use = "this returns a new SmolStr without modifying the original"]
617    fn to_ascii_uppercase_smolstr(&self) -> SmolStr;
618
619    /// Replaces all matches of a &str with another &str returning a new [`SmolStr`],
620    /// potentially without allocating.
621    ///
622    /// See [`str::replace`].
623    #[must_use = "this returns a new SmolStr without modifying the original"]
624    fn replace_smolstr(&self, from: &str, to: &str) -> SmolStr;
625
626    /// Replaces first N matches of a &str with another &str returning a new [`SmolStr`],
627    /// potentially without allocating.
628    ///
629    /// See [`str::replacen`].
630    #[must_use = "this returns a new SmolStr without modifying the original"]
631    fn replacen_smolstr(&self, from: &str, to: &str, count: usize) -> SmolStr;
632}
633
634impl StrExt for str {
635    #[inline]
636    fn to_lowercase_smolstr(&self) -> SmolStr {
637        from_char_iter(self.chars().flat_map(|c| c.to_lowercase()))
638    }
639
640    #[inline]
641    fn to_uppercase_smolstr(&self) -> SmolStr {
642        from_char_iter(self.chars().flat_map(|c| c.to_uppercase()))
643    }
644
645    #[inline]
646    fn to_ascii_lowercase_smolstr(&self) -> SmolStr {
647        from_char_iter(self.chars().map(|c| c.to_ascii_lowercase()))
648    }
649
650    #[inline]
651    fn to_ascii_uppercase_smolstr(&self) -> SmolStr {
652        from_char_iter(self.chars().map(|c| c.to_ascii_uppercase()))
653    }
654
655    #[inline]
656    fn replace_smolstr(&self, from: &str, to: &str) -> SmolStr {
657        self.replacen_smolstr(from, to, usize::MAX)
658    }
659
660    #[inline]
661    fn replacen_smolstr(&self, from: &str, to: &str, count: usize) -> SmolStr {
662        let mut result = SmolStrBuilder::new();
663        let mut last_end = 0;
664        for (start, part) in self.match_indices(from).take(count) {
665            // SAFETY: `start` is guaranteed to be within the bounds of `self` as per
666            // `match_indices` and last_end is always less than or equal to `start`
667            result.push_str(unsafe { self.get_unchecked(last_end..start) });
668            result.push_str(to);
669            last_end = start + part.len();
670        }
671        // SAFETY: `self.len()` is guaranteed to be within the bounds of `self` and last_end is
672        // always less than or equal to `self.len()`
673        result.push_str(unsafe { self.get_unchecked(last_end..self.len()) });
674        SmolStr::from(result)
675    }
676}
677
678impl<T> ToSmolStr for T
679where
680    T: fmt::Display + ?Sized,
681{
682    fn to_smolstr(&self) -> SmolStr {
683        format_smolstr!("{}", self)
684    }
685}
686
687mod private {
688    /// No downstream impls allowed.
689    pub trait Sealed {}
690    impl Sealed for str {}
691}
692
693/// Formats arguments to a [`SmolStr`], potentially without allocating.
694///
695/// See [`alloc::format!`] or [`format_args!`] for syntax documentation.
696#[macro_export]
697macro_rules! format_smolstr {
698    ($($tt:tt)*) => {{
699        let mut w = $crate::SmolStrBuilder::new();
700        ::core::fmt::Write::write_fmt(&mut w, format_args!($($tt)*)).expect("a formatting trait implementation returned an error");
701        w.finish()
702    }};
703}
704
705/// A builder that can be used to efficiently build a [`SmolStr`].
706///
707/// This won't allocate if the final string fits into the inline buffer.
708#[derive(Clone, Default, Debug, PartialEq, Eq)]
709pub struct SmolStrBuilder(SmolStrBuilderRepr);
710
711#[derive(Clone, Debug, PartialEq, Eq)]
712enum SmolStrBuilderRepr {
713    Inline { len: usize, buf: [u8; INLINE_CAP] },
714    Heap(String),
715}
716
717impl Default for SmolStrBuilderRepr {
718    #[inline]
719    fn default() -> Self {
720        SmolStrBuilderRepr::Inline {
721            buf: [0; INLINE_CAP],
722            len: 0,
723        }
724    }
725}
726
727impl SmolStrBuilder {
728    /// Creates a new empty [`SmolStrBuilder`].
729    #[must_use]
730    pub const fn new() -> Self {
731        Self(SmolStrBuilderRepr::Inline {
732            buf: [0; INLINE_CAP],
733            len: 0,
734        })
735    }
736
737    /// Builds a [`SmolStr`] from `self`.
738    #[must_use]
739    pub fn finish(&self) -> SmolStr {
740        SmolStr(match &self.0 {
741            &SmolStrBuilderRepr::Inline { len, buf } => {
742                debug_assert!(len <= INLINE_CAP);
743                Repr::Inline {
744                    // SAFETY: We know that `value.len` is less than or equal to the maximum value of `InlineSize`
745                    len: unsafe { InlineSize::transmute_from_u8(len as u8) },
746                    buf,
747                }
748            }
749            SmolStrBuilderRepr::Heap(heap) => Repr::new(heap),
750        })
751    }
752
753    /// Appends the given [`char`] to the end of `self`'s buffer.
754    pub fn push(&mut self, c: char) {
755        match &mut self.0 {
756            SmolStrBuilderRepr::Inline { len, buf } => {
757                let char_len = c.len_utf8();
758                let new_len = *len + char_len;
759                if new_len <= INLINE_CAP {
760                    c.encode_utf8(&mut buf[*len..]);
761                    *len += char_len;
762                } else {
763                    let mut heap = String::with_capacity(new_len);
764                    // copy existing inline bytes over to the heap
765                    // SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
766                    unsafe { heap.as_mut_vec().extend_from_slice(&buf[..*len]) };
767                    heap.push(c);
768                    self.0 = SmolStrBuilderRepr::Heap(heap);
769                }
770            }
771            SmolStrBuilderRepr::Heap(h) => h.push(c),
772        }
773    }
774
775    /// Appends a given string slice onto the end of `self`'s buffer.
776    pub fn push_str(&mut self, s: &str) {
777        match &mut self.0 {
778            SmolStrBuilderRepr::Inline { len, buf } => {
779                let old_len = *len;
780                *len += s.len();
781
782                // if the new length will fit on the stack (even if it fills it entirely)
783                if *len <= INLINE_CAP {
784                    buf[old_len..*len].copy_from_slice(s.as_bytes());
785                    return; // skip the heap push below
786                }
787
788                let mut heap = String::with_capacity(*len);
789
790                // copy existing inline bytes over to the heap
791                // SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
792                unsafe { heap.as_mut_vec().extend_from_slice(&buf[..old_len]) };
793                heap.push_str(s);
794                self.0 = SmolStrBuilderRepr::Heap(heap);
795            }
796            SmolStrBuilderRepr::Heap(heap) => heap.push_str(s),
797        }
798    }
799}
800
801impl fmt::Write for SmolStrBuilder {
802    #[inline]
803    fn write_str(&mut self, s: &str) -> fmt::Result {
804        self.push_str(s);
805        Ok(())
806    }
807}
808
809impl From<SmolStrBuilder> for SmolStr {
810    fn from(value: SmolStrBuilder) -> Self {
811        value.finish()
812    }
813}
814
815#[cfg(feature = "arbitrary")]
816impl<'a> arbitrary::Arbitrary<'a> for SmolStr {
817    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> Result<Self, arbitrary::Error> {
818        let s = <&str>::arbitrary(u)?;
819        Ok(SmolStr::new(s))
820    }
821}
822
823#[cfg(feature = "borsh")]
824mod borsh;
825#[cfg(feature = "serde")]
826mod serde;