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

[ISSUE #35]🔥impl From<Cow<'static, str>> for CheetahString 🔥 #36

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
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
Loading