From ff93af631719490653c571a27352a9eb072993d4 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Fri, 26 Sep 2025 23:25:20 +0100 Subject: [PATCH 1/2] Implement FromIterator<&str> for EcoString --- src/string.rs | 11 +++++++++++ tests/tests.rs | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/src/string.rs b/src/string.rs index e0ae7aa..d016976 100644 --- a/src/string.rs +++ b/src/string.rs @@ -514,6 +514,17 @@ impl FromIterator for EcoString { } } +impl<'a> FromIterator<&'a str> for EcoString { + #[inline] + fn from_iter>(iter: T) -> Self { + let mut s = Self::new(); + for sub in iter { + s.push_str(sub); + } + s + } +} + impl FromIterator for EcoString { #[inline] fn from_iter>(iter: T) -> Self { diff --git a/tests/tests.rs b/tests/tests.rs index 343b0c5..353d2f8 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] From 22190ef1c73429ba884242761add501230dcc839 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Tue, 28 Oct 2025 04:12:23 +0000 Subject: [PATCH 2/2] Implement Extend<&str> for EcoString and refactor implement FromIterator<&str> --- src/string.rs | 15 ++++++++++----- tests/tests.rs | 4 +++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/string.rs b/src/string.rs index d016976..91b6a58 100644 --- a/src/string.rs +++ b/src/string.rs @@ -517,11 +517,9 @@ impl FromIterator for EcoString { impl<'a> FromIterator<&'a str> for EcoString { #[inline] fn from_iter>(iter: T) -> Self { - let mut s = Self::new(); - for sub in iter { - s.push_str(sub); - } - s + let mut buf = Self::new(); + buf.extend(iter); + buf } } @@ -545,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 353d2f8..df02e28 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -484,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]