Skip to content

Commit

Permalink
[ISSUE #35]🔥impl From<Cow<'static, str>> for CheetahString 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm committed Nov 13, 2024
1 parent f1e7e92 commit 716df52
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/cheetah_string.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use core::fmt;
use std::borrow::Borrow;
use std::borrow::{Borrow, Cow};
use std::cmp::Ordering;
use std::fmt::Display;
use std::hash::Hash;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;

const EMPTY_STRING: &str = "";
Expand Down Expand Up @@ -46,6 +47,38 @@ impl From<&[u8]> for CheetahString {
}
}

impl FromStr for CheetahString {
type Err = std::string::ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(CheetahString::from_slice(s))
}
}

impl From<Vec<u8>> for CheetahString {
fn from(v: Vec<u8>) -> Self {
CheetahString::from_slice(unsafe { std::str::from_utf8_unchecked(&v) })
}
}

impl From<Cow<'static, str>> for CheetahString {
fn from(cow: Cow<'static, str>) -> Self {
match cow {
Cow::Borrowed(s) => CheetahString::from_static_str(s),
Cow::Owned(s) => CheetahString::from_string(s),
}
}
}

impl From<Cow<'_, String>> for CheetahString {
fn from(cow: Cow<'_, String>) -> Self {
match cow {
Cow::Borrowed(s) => CheetahString::from_slice(s),
Cow::Owned(s) => CheetahString::from_string(s),
}
}
}

#[cfg(feature = "bytes")]
impl From<bytes::Bytes> for CheetahString {
fn from(b: bytes::Bytes) -> Self {
Expand All @@ -62,6 +95,9 @@ impl From<CheetahString> for String {
CheetahString {
inner: InnerString::StaticStr(s),
} => s.to_string(),
CheetahString {
inner: InnerString::ArcVecString(s),
} => unsafe { String::from_utf8_unchecked(s.to_vec()) },
#[cfg(feature = "bytes")]
CheetahString {
inner: InnerString::Bytes(b),
Expand Down Expand Up @@ -114,6 +150,20 @@ impl CheetahString {
}
}

#[inline]
pub fn from_vec(s: Vec<u8>) -> Self {
CheetahString {
inner: InnerString::ArcVecString(Arc::new(s)),
}
}

#[inline]
pub fn from_arc_vec(s: Arc<Vec<u8>>) -> Self {
CheetahString {
inner: InnerString::ArcVecString(s),
}
}

#[inline]
pub fn from_slice(s: &str) -> Self {
CheetahString {
Expand Down Expand Up @@ -147,6 +197,7 @@ impl CheetahString {
match &self.inner {
InnerString::ArcString(s) => s.as_str(),
InnerString::StaticStr(s) => s,
InnerString::ArcVecString(s) => std::str::from_utf8(s.as_ref()).unwrap(),
#[cfg(feature = "bytes")]
InnerString::Bytes(b) => std::str::from_utf8(b.as_ref()).unwrap(),
InnerString::Empty => EMPTY_STRING,
Expand All @@ -158,6 +209,7 @@ impl CheetahString {
match &self.inner {
InnerString::ArcString(s) => s.as_bytes(),
InnerString::StaticStr(s) => s.as_bytes(),
InnerString::ArcVecString(s) => s.as_ref(),
#[cfg(feature = "bytes")]
InnerString::Bytes(b) => b.as_ref(),
InnerString::Empty => &[],
Expand All @@ -169,6 +221,7 @@ impl CheetahString {
match &self.inner {
InnerString::ArcString(s) => s.len(),
InnerString::StaticStr(s) => s.len(),
InnerString::ArcVecString(s) => s.len(),
#[cfg(feature = "bytes")]
InnerString::Bytes(b) => b.len(),
InnerString::Empty => 0,
Expand All @@ -180,6 +233,7 @@ impl CheetahString {
match &self.inner {
InnerString::ArcString(s) => s.is_empty(),
InnerString::StaticStr(s) => s.is_empty(),
InnerString::ArcVecString(s) => s.is_empty(),
#[cfg(feature = "bytes")]
InnerString::Bytes(b) => b.is_empty(),
InnerString::Empty => true,
Expand All @@ -205,6 +259,12 @@ impl PartialEq<String> for CheetahString {
}
}

impl PartialEq<Vec<u8>> for CheetahString {
fn eq(&self, other: &Vec<u8>) -> bool {
self.as_bytes() == other.as_slice()
}
}

impl<'a> PartialEq<&'a str> for CheetahString {
fn eq(&self, other: &&'a str) -> bool {
self.as_str() == *other
Expand Down Expand Up @@ -279,6 +339,7 @@ impl Borrow<str> for CheetahString {
pub(super) enum InnerString {
ArcString(Arc<String>),
StaticStr(&'static str),
ArcVecString(Arc<Vec<u8>>),
#[cfg(feature = "bytes")]
Bytes(bytes::Bytes),
Empty,
Expand Down

0 comments on commit 716df52

Please sign in to comment.