Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish 0.3.1 #86

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smol_str"
version = "0.3.0"
version = "0.3.1"
description = "small-string optimized string type with O(1) clone"
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-analyzer/smol_str"
Expand Down
38 changes: 22 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,49 +705,55 @@ macro_rules! format_smolstr {
/// A builder that can be used to efficiently build a [`SmolStr`].
///
/// This won't allocate if the final string fits into the inline buffer.
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct SmolStrBuilder(SmolStrBuilderRepr);

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SmolStrBuilder {
enum SmolStrBuilderRepr {
Inline { len: usize, buf: [u8; INLINE_CAP] },
Heap(String),
}

impl Default for SmolStrBuilder {
impl Default for SmolStrBuilderRepr {
#[inline]
fn default() -> Self {
Self::new()
SmolStrBuilderRepr::Inline {
buf: [0; INLINE_CAP],
len: 0,
}
}
}

impl SmolStrBuilder {
/// Creates a new empty [`SmolStrBuilder`].
#[must_use]
pub const fn new() -> Self {
SmolStrBuilder::Inline {
Self(SmolStrBuilderRepr::Inline {
buf: [0; INLINE_CAP],
len: 0,
}
})
}

/// Builds a [`SmolStr`] from `self`.
#[must_use]
pub fn finish(&self) -> SmolStr {
SmolStr(match self {
&SmolStrBuilder::Inline { len, buf } => {
SmolStr(match &self.0 {
&SmolStrBuilderRepr::Inline { len, buf } => {
debug_assert!(len <= INLINE_CAP);
Repr::Inline {
// SAFETY: We know that `value.len` is less than or equal to the maximum value of `InlineSize`
len: unsafe { InlineSize::transmute_from_u8(len as u8) },
buf,
}
}
SmolStrBuilder::Heap(heap) => Repr::new(heap),
SmolStrBuilderRepr::Heap(heap) => Repr::new(heap),
})
}

/// Appends the given [`char`] to the end of `self`'s buffer.
pub fn push(&mut self, c: char) {
match self {
SmolStrBuilder::Inline { len, buf } => {
match &mut self.0 {
SmolStrBuilderRepr::Inline { len, buf } => {
let char_len = c.len_utf8();
let new_len = *len + char_len;
if new_len <= INLINE_CAP {
Expand All @@ -759,17 +765,17 @@ impl SmolStrBuilder {
// SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
unsafe { heap.as_mut_vec().extend_from_slice(buf) };
heap.push(c);
*self = SmolStrBuilder::Heap(heap);
self.0 = SmolStrBuilderRepr::Heap(heap);
}
}
SmolStrBuilder::Heap(h) => h.push(c),
SmolStrBuilderRepr::Heap(h) => h.push(c),
}
}

/// Appends a given string slice onto the end of `self`'s buffer.
pub fn push_str(&mut self, s: &str) {
match self {
Self::Inline { len, buf } => {
match &mut self.0 {
SmolStrBuilderRepr::Inline { len, buf } => {
let old_len = *len;
*len += s.len();

Expand All @@ -785,9 +791,9 @@ impl SmolStrBuilder {
// SAFETY: inline data is guaranteed to be valid utf8 for `old_len` bytes
unsafe { heap.as_mut_vec().extend_from_slice(&buf[..old_len]) };
heap.push_str(s);
*self = SmolStrBuilder::Heap(heap);
self.0 = SmolStrBuilderRepr::Heap(heap);
}
SmolStrBuilder::Heap(heap) => heap.push_str(s),
SmolStrBuilderRepr::Heap(heap) => heap.push_str(s),
}
}
}
Expand Down
Loading