Skip to content

Commit 73f125e

Browse files
committed
[ISSUE #47]Enhance Cheetah String FromIterator
1 parent 26f70e4 commit 73f125e

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/cheetah_string.rs

+60-1
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,50 @@ impl Default for CheetahString {
2424
}
2525

2626
impl From<String> for CheetahString {
27+
#[inline]
2728
fn from(s: String) -> Self {
2829
CheetahString::from_string(s)
2930
}
3031
}
3132

3233
impl From<Arc<String>> for CheetahString {
34+
#[inline]
3335
fn from(s: Arc<String>) -> Self {
3436
CheetahString::from_arc_string(s)
3537
}
3638
}
3739

3840
impl<'a> From<&'a str> for CheetahString {
41+
#[inline]
3942
fn from(s: &'a str) -> Self {
4043
CheetahString::from_slice(s)
4144
}
4245
}
4346

4447
impl From<&[u8]> for CheetahString {
48+
#[inline]
4549
fn from(b: &[u8]) -> Self {
4650
CheetahString::from_slice(unsafe { std::str::from_utf8_unchecked(b) })
4751
}
4852
}
4953

5054
impl FromStr for CheetahString {
5155
type Err = std::string::ParseError;
52-
56+
#[inline]
5357
fn from_str(s: &str) -> Result<Self, Self::Err> {
5458
Ok(CheetahString::from_slice(s))
5559
}
5660
}
5761

5862
impl From<Vec<u8>> for CheetahString {
63+
#[inline]
5964
fn from(v: Vec<u8>) -> Self {
6065
CheetahString::from_slice(unsafe { std::str::from_utf8_unchecked(&v) })
6166
}
6267
}
6368

6469
impl From<Cow<'static, str>> for CheetahString {
70+
#[inline]
6571
fn from(cow: Cow<'static, str>) -> Self {
6672
match cow {
6773
Cow::Borrowed(s) => CheetahString::from_static_str(s),
@@ -71,6 +77,7 @@ impl From<Cow<'static, str>> for CheetahString {
7177
}
7278

7379
impl From<Cow<'_, String>> for CheetahString {
80+
#[inline]
7481
fn from(cow: Cow<'_, String>) -> Self {
7582
match cow {
7683
Cow::Borrowed(s) => CheetahString::from_slice(s),
@@ -79,14 +86,66 @@ impl From<Cow<'_, String>> for CheetahString {
7986
}
8087
}
8188

89+
impl From<char> for CheetahString {
90+
/// Allocates an owned [`CheetahString`] from a single character.
91+
///
92+
/// # Example
93+
/// ```rust
94+
/// use cheetah_string::CheetahString;
95+
/// let c: char = 'a';
96+
/// let s: CheetahString = CheetahString::from(c);
97+
/// assert_eq!("a", &s[..]);
98+
/// ```
99+
#[inline]
100+
fn from(c: char) -> Self {
101+
CheetahString::from_string(c.to_string())
102+
}
103+
}
104+
105+
impl<'a> FromIterator<&'a char> for CheetahString {
106+
fn from_iter<T: IntoIterator<Item = &'a char>>(iter: T) -> CheetahString {
107+
let mut buf = String::new();
108+
buf.extend(iter);
109+
CheetahString::from_string(buf)
110+
}
111+
}
112+
113+
impl<'a> FromIterator<&'a str> for CheetahString {
114+
fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> CheetahString {
115+
let mut buf = String::new();
116+
buf.extend(iter);
117+
CheetahString::from_string(buf)
118+
}
119+
}
120+
121+
impl FromIterator<String> for CheetahString {
122+
#[inline]
123+
fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self {
124+
let mut buf = String::new();
125+
buf.extend(iter);
126+
CheetahString::from_string(buf)
127+
}
128+
}
129+
130+
impl<'a> FromIterator<&'a String> for CheetahString {
131+
#[inline]
132+
fn from_iter<T: IntoIterator<Item = &'a String>>(iter: T) -> Self {
133+
let mut buf = String::new();
134+
buf.extend(iter.into_iter().map(|s| s.as_str()));
135+
CheetahString::from_string(buf)
136+
}
137+
}
138+
82139
#[cfg(feature = "bytes")]
83140
impl From<bytes::Bytes> for CheetahString {
141+
#[inline]
84142
fn from(b: bytes::Bytes) -> Self {
85143
CheetahString::from_bytes(b)
86144
}
87145
}
88146

89147
impl From<&CheetahString> for CheetahString {
148+
#[inline]
90149
fn from(s: &CheetahString) -> Self {
91150
s.clone()
92151
}

0 commit comments

Comments
 (0)