Skip to content

Commit

Permalink
feat: support deep_clone_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWhiteWu committed Feb 23, 2024
1 parent 1ea5019 commit 9da723b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/lib.rs
Expand Up @@ -242,6 +242,16 @@ impl FastStr {
self.0.into_string()
}

/// If the inner repr of FastStr is a Bytes, then it will be deep cloned and returned as a new FastStr.
/// Otherwise, it will return a new FastStr with the same repr which has no cost.
///
/// This is not stable and may be removed or renamed in the future.
#[inline]
#[doc(hidden)]
pub fn deep_clone_bytes(&self) -> Self {
Self(self.0.deep_clone_bytes())
}

fn from_char_iter<I: iter::Iterator<Item = char>>(mut iter: I) -> Self {
let (min_size, _) = iter.size_hint();
if min_size > INLINE_CAP {
Expand Down Expand Up @@ -692,6 +702,22 @@ impl Repr {
}
}

#[inline]
fn deep_clone_bytes(&self) -> Self {
match self {
Self::Empty => Self::Empty,
// Safety: this is guaranteed by the user when creating the `FastStr`.
Self::Bytes(bytes) => unsafe { Self::new(std::str::from_utf8_unchecked(bytes)) },
Self::ArcStr(arc_str) => Self::ArcStr(Arc::clone(arc_str)),
Self::ArcString(arc_string) => Self::ArcString(Arc::clone(arc_string)),
Self::StaticStr(s) => Self::StaticStr(s),
Self::Inline { len, buf } => Self::Inline {
len: *len,
buf: *buf,
},
}
}

#[inline]
fn slice_ref(&self, subset: &[u8]) -> Self {
if subset.is_empty() {
Expand Down

0 comments on commit 9da723b

Please sign in to comment.