Skip to content
Open
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.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "faststr"
version = "0.2.31"
version = "0.2.32"
authors = ["Volo Team <[email protected]>"]
edition = "2021"
description = "Faststr is a string library that reduces the cost of clone."
Expand Down
33 changes: 22 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ impl FastStr {
ch.encode_utf8(&mut buf[len..]);
len += size;
}
Self(Repr::Inline { len, buf })
Self(Repr::Inline {
len: len as u8,
buf,
})
}

fn can_inline(s: &str) -> bool {
Expand Down Expand Up @@ -477,7 +480,10 @@ where
buf[len..][..size].copy_from_slice(slice.as_bytes());
len += size;
}
FastStr(Repr::Inline { len, buf })
FastStr(Repr::Inline {
len: len as u8,
buf,
})
}

impl iter::FromIterator<String> for FastStr {
Expand Down Expand Up @@ -566,7 +572,7 @@ impl From<Cow<'static, str>> for FastStr {
}
}

const INLINE_CAP: usize = 24;
const INLINE_CAP: usize = 38;

#[derive(Clone)]
enum Repr {
Expand All @@ -575,7 +581,7 @@ enum Repr {
ArcStr(Arc<str>),
ArcString(Arc<String>),
StaticStr(&'static str),
Inline { len: usize, buf: [u8; INLINE_CAP] },
Inline { len: u8, buf: [u8; INLINE_CAP] },
}

impl Repr {
Expand Down Expand Up @@ -613,7 +619,10 @@ impl Repr {
unsafe fn new_inline_impl(s: &str) -> Self {
let mut buf = [0u8; INLINE_CAP];
core::ptr::copy_nonoverlapping(s.as_ptr(), buf.as_mut_ptr(), s.len());
Self::Inline { len: s.len(), buf }
Self::Inline {
len: s.len() as u8,
buf,
}
}

#[inline]
Expand Down Expand Up @@ -655,7 +664,7 @@ impl Repr {
Self::ArcStr(arc_str) => arc_str.len(),
Self::ArcString(arc_string) => arc_string.len(),
Self::StaticStr(s) => s.len(),
Self::Inline { len, .. } => *len,
Self::Inline { len, .. } => *len as usize,
}
}

Expand All @@ -680,7 +689,9 @@ impl Repr {
Self::ArcStr(arc_str) => arc_str,
Self::ArcString(arc_string) => arc_string,
Self::StaticStr(s) => s,
Self::Inline { len, buf } => unsafe { core::str::from_utf8_unchecked(&buf[..*len]) },
Self::Inline { len, buf } => unsafe {
core::str::from_utf8_unchecked(&buf[..*len as usize])
},
}
}

Expand All @@ -696,7 +707,7 @@ impl Repr {
}
Self::StaticStr(s) => s.to_string(),
Self::Inline { len, buf } => unsafe {
String::from_utf8_unchecked(buf[..len].to_vec())
String::from_utf8_unchecked(buf[..len as usize].to_vec())
},
}
}
Expand All @@ -711,7 +722,7 @@ impl Repr {
Bytes::from(Arc::try_unwrap(arc_string).unwrap_or_else(|arc| (*arc).clone()))
}
Self::StaticStr(s) => Bytes::from_static(s.as_bytes()),
Self::Inline { len, buf } => Bytes::from(buf[..len].to_vec()),
Self::Inline { len, buf } => Bytes::from(buf[..len as usize].to_vec()),
}
}

Expand Down Expand Up @@ -771,7 +782,7 @@ impl Repr {
core::str::from_utf8_unchecked(&s.as_bytes()[sub_offset..sub_offset + sub_len])
}),
Repr::Inline { len: _, buf } => Self::Inline {
len: sub_len,
len: sub_len as u8,
buf: {
let mut new_buf = [0; INLINE_CAP];
new_buf[..sub_len].copy_from_slice(&buf[sub_offset..sub_offset + sub_len]);
Expand All @@ -791,7 +802,7 @@ impl AsRef<[u8]> for Repr {
Self::ArcStr(arc_str) => arc_str.as_bytes(),
Self::ArcString(arc_string) => arc_string.as_bytes(),
Self::StaticStr(s) => s.as_bytes(),
Self::Inline { len, buf } => &buf[..*len],
Self::Inline { len, buf } => &buf[..*len as usize],
}
}
}
Expand Down
Loading