Skip to content
Merged
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
16 changes: 16 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,15 @@ impl FromIterator<char> for EcoString {
}
}

impl<'a> FromIterator<&'a str> for EcoString {
#[inline]
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
let mut buf = Self::new();
buf.extend(iter);
buf
}
}

impl FromIterator<Self> for EcoString {
#[inline]
fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self {
Expand All @@ -534,6 +543,13 @@ impl Extend<char> for EcoString {
}
}

impl<'a> Extend<&'a str> for EcoString {
#[inline]
fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) {
iter.into_iter().for_each(move |s| self.push_str(s));
}
}

impl From<EcoString> for String {
/// This needs to allocate to change the layout.
#[inline]
Expand Down
8 changes: 7 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,20 @@ fn test_str_construction() {

assert_eq!(str_from_eco_string, str_from_eco_string_ref);
assert_eq!(str_from_eco_string_ref, "foo");

let from_string_iter: EcoString = ["foo", " ", "bar"].into_iter().collect();

assert_eq!(from_string_iter, "foo bar");
}

#[test]
fn test_str_extend() {
let mut s = EcoString::from("Hello, ");
s.extend("world!".chars());

assert_eq!(s, "Hello, world!");
s.extend([" How ", "are ", "you", "?"]);

assert_eq!(s, "Hello, world! How are you?");
}

#[test]
Expand Down