Skip to content

Commit

Permalink
feat: use more inline (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWhiteWu authored May 27, 2024
1 parent 231882f commit 0631a66
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 58 deletions.
88 changes: 33 additions & 55 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ keywords = ["string", "str", "volo"]

[dependencies]
bytes = "1"
serde = { version = "1", optional = true, default_features = false }
serde = { version = "1", optional = true, default-features = false }
simdutf8 = { version = "0.1", features = ["aarch64_neon"] }
redis = { version = "0.25", optional = true, default_features = false }
redis = { version = "0.25", optional = true, default-features = false }
itoa = { version = "1", optional = true }

[features]
Expand All @@ -26,7 +26,6 @@ serde-unsafe = ["serde"]
redis = ["dep:redis", "itoa"]
redis-unsafe = ["redis"]


[dev-dependencies]
static_assertions = { version = "1" }
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,27 @@ impl FastStr {
/// Create a new `FastStr` from an `Arc<str>`.
#[inline]
pub fn from_arc_str(s: Arc<str>) -> Self {
if Self::can_inline(&s) {
return Self::new(s);
}
Self(Repr::from_arc_str(s))
}

/// Create a new `FastStr` from a `String`.
#[inline]
pub fn from_string(s: String) -> Self {
if Self::can_inline(&s) {
return Self::new(s);
}
Self(Repr::from_string(s))
}

/// Create a new `FastStr` from an `Arc<String>`.
#[inline]
pub fn from_arc_string(s: Arc<String>) -> Self {
if Self::can_inline(&s) {
return Self::new(s.as_str());
}
Self(Repr::from_arc_string(s))
}

Expand All @@ -110,6 +119,10 @@ impl FastStr {
/// `b` must be valid UTF-8.
#[inline]
pub unsafe fn from_bytes_unchecked(b: Bytes) -> Self {
let s = std::str::from_utf8_unchecked(&b);
if Self::can_inline(s) {
return Self::new(s);
}
Self(Repr::from_bytes_unchecked(b))
}

Expand Down Expand Up @@ -274,6 +287,10 @@ impl FastStr {
}
Self(Repr::Inline { len, buf })
}

fn can_inline(s: &str) -> bool {
s.len() <= INLINE_CAP
}
}

impl Default for FastStr {
Expand Down

0 comments on commit 0631a66

Please sign in to comment.