Skip to content

Commit 9639e9f

Browse files
authored
Implement FromIterator<&str> and Extend<&str> for EcoString (#56)
1 parent 4ff5f41 commit 9639e9f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/string.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,15 @@ impl FromIterator<char> for EcoString {
514514
}
515515
}
516516

517+
impl<'a> FromIterator<&'a str> for EcoString {
518+
#[inline]
519+
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
520+
let mut buf = Self::new();
521+
buf.extend(iter);
522+
buf
523+
}
524+
}
525+
517526
impl FromIterator<Self> for EcoString {
518527
#[inline]
519528
fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self {
@@ -534,6 +543,13 @@ impl Extend<char> for EcoString {
534543
}
535544
}
536545

546+
impl<'a> Extend<&'a str> for EcoString {
547+
#[inline]
548+
fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) {
549+
iter.into_iter().for_each(move |s| self.push_str(s));
550+
}
551+
}
552+
537553
impl From<EcoString> for String {
538554
/// This needs to allocate to change the layout.
539555
#[inline]

tests/tests.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,20 @@ fn test_str_construction() {
473473

474474
assert_eq!(str_from_eco_string, str_from_eco_string_ref);
475475
assert_eq!(str_from_eco_string_ref, "foo");
476+
477+
let from_string_iter: EcoString = ["foo", " ", "bar"].into_iter().collect();
478+
479+
assert_eq!(from_string_iter, "foo bar");
476480
}
477481

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

483-
assert_eq!(s, "Hello, world!");
487+
s.extend([" How ", "are ", "you", "?"]);
488+
489+
assert_eq!(s, "Hello, world! How are you?");
484490
}
485491

486492
#[test]

0 commit comments

Comments
 (0)