@@ -705,49 +705,55 @@ macro_rules! format_smolstr {
705
705
/// A builder that can be used to efficiently build a [`SmolStr`].
706
706
///
707
707
/// This won't allocate if the final string fits into the inline buffer.
708
+ #[ derive( Clone , Default , Debug , PartialEq , Eq ) ]
709
+ pub struct SmolStrBuilder ( SmolStrBuilderRepr ) ;
710
+
708
711
#[ derive( Clone , Debug , PartialEq , Eq ) ]
709
- pub enum SmolStrBuilder {
712
+ enum SmolStrBuilderRepr {
710
713
Inline { len : usize , buf : [ u8 ; INLINE_CAP ] } ,
711
714
Heap ( String ) ,
712
715
}
713
716
714
- impl Default for SmolStrBuilder {
717
+ impl Default for SmolStrBuilderRepr {
715
718
#[ inline]
716
719
fn default ( ) -> Self {
717
- Self :: new ( )
720
+ SmolStrBuilderRepr :: Inline {
721
+ buf : [ 0 ; INLINE_CAP ] ,
722
+ len : 0 ,
723
+ }
718
724
}
719
725
}
720
726
721
727
impl SmolStrBuilder {
722
728
/// Creates a new empty [`SmolStrBuilder`].
723
729
#[ must_use]
724
730
pub const fn new ( ) -> Self {
725
- SmolStrBuilder :: Inline {
731
+ Self ( SmolStrBuilderRepr :: Inline {
726
732
buf : [ 0 ; INLINE_CAP ] ,
727
733
len : 0 ,
728
- }
734
+ } )
729
735
}
730
736
731
737
/// Builds a [`SmolStr`] from `self`.
732
738
#[ must_use]
733
739
pub fn finish ( & self ) -> SmolStr {
734
- SmolStr ( match self {
735
- & SmolStrBuilder :: Inline { len, buf } => {
740
+ SmolStr ( match & self . 0 {
741
+ & SmolStrBuilderRepr :: Inline { len, buf } => {
736
742
debug_assert ! ( len <= INLINE_CAP ) ;
737
743
Repr :: Inline {
738
744
// SAFETY: We know that `value.len` is less than or equal to the maximum value of `InlineSize`
739
745
len : unsafe { InlineSize :: transmute_from_u8 ( len as u8 ) } ,
740
746
buf,
741
747
}
742
748
}
743
- SmolStrBuilder :: Heap ( heap) => Repr :: new ( heap) ,
749
+ SmolStrBuilderRepr :: Heap ( heap) => Repr :: new ( heap) ,
744
750
} )
745
751
}
746
752
747
753
/// Appends the given [`char`] to the end of `self`'s buffer.
748
754
pub fn push ( & mut self , c : char ) {
749
- match self {
750
- SmolStrBuilder :: Inline { len, buf } => {
755
+ match & mut self . 0 {
756
+ SmolStrBuilderRepr :: Inline { len, buf } => {
751
757
let char_len = c. len_utf8 ( ) ;
752
758
let new_len = * len + char_len;
753
759
if new_len <= INLINE_CAP {
@@ -759,17 +765,17 @@ impl SmolStrBuilder {
759
765
// SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
760
766
unsafe { heap. as_mut_vec ( ) . extend_from_slice ( buf) } ;
761
767
heap. push ( c) ;
762
- * self = SmolStrBuilder :: Heap ( heap) ;
768
+ self . 0 = SmolStrBuilderRepr :: Heap ( heap) ;
763
769
}
764
770
}
765
- SmolStrBuilder :: Heap ( h) => h. push ( c) ,
771
+ SmolStrBuilderRepr :: Heap ( h) => h. push ( c) ,
766
772
}
767
773
}
768
774
769
775
/// Appends a given string slice onto the end of `self`'s buffer.
770
776
pub fn push_str ( & mut self , s : & str ) {
771
- match self {
772
- Self :: Inline { len, buf } => {
777
+ match & mut self . 0 {
778
+ SmolStrBuilderRepr :: Inline { len, buf } => {
773
779
let old_len = * len;
774
780
* len += s. len ( ) ;
775
781
@@ -785,9 +791,9 @@ impl SmolStrBuilder {
785
791
// SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
786
792
unsafe { heap. as_mut_vec ( ) . extend_from_slice ( & buf[ ..old_len] ) } ;
787
793
heap. push_str ( s) ;
788
- * self = SmolStrBuilder :: Heap ( heap) ;
794
+ self . 0 = SmolStrBuilderRepr :: Heap ( heap) ;
789
795
}
790
- SmolStrBuilder :: Heap ( heap) => heap. push_str ( s) ,
796
+ SmolStrBuilderRepr :: Heap ( heap) => heap. push_str ( s) ,
791
797
}
792
798
}
793
799
}
0 commit comments