Skip to content

Commit 68bc4ed

Browse files
committed
Make SmolStrBuilder fields private
1 parent b38a973 commit 68bc4ed

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/lib.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -705,49 +705,55 @@ macro_rules! format_smolstr {
705705
/// A builder that can be used to efficiently build a [`SmolStr`].
706706
///
707707
/// 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+
708711
#[derive(Clone, Debug, PartialEq, Eq)]
709-
pub enum SmolStrBuilder {
712+
enum SmolStrBuilderRepr {
710713
Inline { len: usize, buf: [u8; INLINE_CAP] },
711714
Heap(String),
712715
}
713716

714-
impl Default for SmolStrBuilder {
717+
impl Default for SmolStrBuilderRepr {
715718
#[inline]
716719
fn default() -> Self {
717-
Self::new()
720+
SmolStrBuilderRepr::Inline {
721+
buf: [0; INLINE_CAP],
722+
len: 0,
723+
}
718724
}
719725
}
720726

721727
impl SmolStrBuilder {
722728
/// Creates a new empty [`SmolStrBuilder`].
723729
#[must_use]
724730
pub const fn new() -> Self {
725-
SmolStrBuilder::Inline {
731+
Self(SmolStrBuilderRepr::Inline {
726732
buf: [0; INLINE_CAP],
727733
len: 0,
728-
}
734+
})
729735
}
730736

731737
/// Builds a [`SmolStr`] from `self`.
732738
#[must_use]
733739
pub fn finish(&self) -> SmolStr {
734-
SmolStr(match self {
735-
&SmolStrBuilder::Inline { len, buf } => {
740+
SmolStr(match &self.0 {
741+
&SmolStrBuilderRepr::Inline { len, buf } => {
736742
debug_assert!(len <= INLINE_CAP);
737743
Repr::Inline {
738744
// SAFETY: We know that `value.len` is less than or equal to the maximum value of `InlineSize`
739745
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
740746
buf,
741747
}
742748
}
743-
SmolStrBuilder::Heap(heap) => Repr::new(heap),
749+
SmolStrBuilderRepr::Heap(heap) => Repr::new(heap),
744750
})
745751
}
746752

747753
/// Appends the given [`char`] to the end of `self`'s buffer.
748754
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 } => {
751757
let char_len = c.len_utf8();
752758
let new_len = *len + char_len;
753759
if new_len <= INLINE_CAP {
@@ -759,17 +765,17 @@ impl SmolStrBuilder {
759765
// SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
760766
unsafe { heap.as_mut_vec().extend_from_slice(buf) };
761767
heap.push(c);
762-
*self = SmolStrBuilder::Heap(heap);
768+
self.0 = SmolStrBuilderRepr::Heap(heap);
763769
}
764770
}
765-
SmolStrBuilder::Heap(h) => h.push(c),
771+
SmolStrBuilderRepr::Heap(h) => h.push(c),
766772
}
767773
}
768774

769775
/// Appends a given string slice onto the end of `self`'s buffer.
770776
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 } => {
773779
let old_len = *len;
774780
*len += s.len();
775781

@@ -785,9 +791,9 @@ impl SmolStrBuilder {
785791
// SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
786792
unsafe { heap.as_mut_vec().extend_from_slice(&buf[..old_len]) };
787793
heap.push_str(s);
788-
*self = SmolStrBuilder::Heap(heap);
794+
self.0 = SmolStrBuilderRepr::Heap(heap);
789795
}
790-
SmolStrBuilder::Heap(heap) => heap.push_str(s),
796+
SmolStrBuilderRepr::Heap(heap) => heap.push_str(s),
791797
}
792798
}
793799
}

0 commit comments

Comments
 (0)