diff options
| author | bors <bors@rust-lang.org> | 2018-12-06 12:41:30 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-12-06 12:41:30 +0000 |
| commit | 367e783e6f66a4dba32decdc68a262953d2f3f1b (patch) | |
| tree | cda4764d303feb6c62218550fb619c73a2eebdc8 /src/liballoc | |
| parent | 128a1fa4e1f85e04f522653bb9bee83ed6523440 (diff) | |
| parent | cd1ee5edbd080659f1ff353dd828bdf9749cf11d (diff) | |
| download | rust-367e783e6f66a4dba32decdc68a262953d2f3f1b.tar.gz rust-367e783e6f66a4dba32decdc68a262953d2f3f1b.zip | |
Auto merge of #56557 - pietroalbini:rollup, r=pietroalbini
Rollup of 11 pull requests Successful merges: - #56315 (Rustdoc inline macro reexport) - #56332 ([rustdoc] Specific crate search) - #56362 (Stabilise exhaustive integer patterns) - #56426 (libsyntax_pos: A few tweaks) - #56441 (rustbuild: Fix issues with compiler docs) - #56446 (pass the parameter environment to `traits::find_associated_item`) - #56500 (cleanup: remove static lifetimes from consts) - #56525 (Avoid extra copy and syscall in std::env::current_exe) - #56528 (Remove unused dependency (rustc_lint -> rustc_mir)) - #56548 (Optimized string FromIterator + Extend impls) - #56553 (Don't print the profiling summary to stdout when -Zprofile-json is set) Failed merges: r? @ghost
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/string.rs | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index 662f8ae614f..006c602649b 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -577,7 +577,7 @@ impl String { return Cow::Borrowed(""); }; - const REPLACEMENT: &'static str = "\u{FFFD}"; + const REPLACEMENT: &str = "\u{FFFD}"; let mut res = String::with_capacity(v.len()); res.push_str(first_valid); @@ -1732,18 +1732,37 @@ impl<'a> FromIterator<&'a str> for String { #[stable(feature = "extend_string", since = "1.4.0")] impl FromIterator<String> for String { fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> String { - let mut buf = String::new(); - buf.extend(iter); - buf + let mut iterator = iter.into_iter(); + + // Because we're iterating over `String`s, we can avoid at least + // one allocation by getting the first string from the iterator + // and appending to it all the subsequent strings. + match iterator.next() { + None => String::new(), + Some(mut buf) => { + buf.extend(iterator); + buf + } + } } } #[stable(feature = "herd_cows", since = "1.19.0")] impl<'a> FromIterator<Cow<'a, str>> for String { fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> String { - let mut buf = String::new(); - buf.extend(iter); - buf + let mut iterator = iter.into_iter(); + + // Because we're iterating over CoWs, we can (potentially) avoid at least + // one allocation by getting the first item and appending to it all the + // subsequent items. + match iterator.next() { + None => String::new(), + Some(cow) => { + let mut buf = cow.into_owned(); + buf.extend(iterator); + buf + } + } } } @@ -1753,9 +1772,7 @@ impl Extend<char> for String { let iterator = iter.into_iter(); let (lower_bound, _) = iterator.size_hint(); self.reserve(lower_bound); - for ch in iterator { - self.push(ch) - } + iterator.for_each(move |c| self.push(c)); } } @@ -1769,27 +1786,21 @@ impl<'a> Extend<&'a char> for String { #[stable(feature = "rust1", since = "1.0.0")] impl<'a> Extend<&'a str> for String { fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) { - for s in iter { - self.push_str(s) - } + iter.into_iter().for_each(move |s| self.push_str(s)); } } #[stable(feature = "extend_string", since = "1.4.0")] impl Extend<String> for String { fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) { - for s in iter { - self.push_str(&s) - } + iter.into_iter().for_each(move |s| self.push_str(&s)); } } #[stable(feature = "herd_cows", since = "1.19.0")] impl<'a> Extend<Cow<'a, str>> for String { fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) { - for s in iter { - self.push_str(&s) - } + iter.into_iter().for_each(move |s| self.push_str(&s)); } } |
