diff --git a/src/string.rs b/src/string.rs index e0ae7aa..91b6a58 100644 --- a/src/string.rs +++ b/src/string.rs @@ -514,6 +514,15 @@ impl FromIterator for EcoString { } } +impl<'a> FromIterator<&'a str> for EcoString { + #[inline] + fn from_iter>(iter: T) -> Self { + let mut buf = Self::new(); + buf.extend(iter); + buf + } +} + impl FromIterator for EcoString { #[inline] fn from_iter>(iter: T) -> Self { @@ -534,6 +543,13 @@ impl Extend for EcoString { } } +impl<'a> Extend<&'a str> for EcoString { + #[inline] + fn extend>(&mut self, iter: T) { + iter.into_iter().for_each(move |s| self.push_str(s)); + } +} + impl From for String { /// This needs to allocate to change the layout. #[inline] diff --git a/tests/tests.rs b/tests/tests.rs index 343b0c5..df02e28 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -473,6 +473,10 @@ 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] @@ -480,7 +484,9 @@ 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]