From 6563f4ab79885666cd8602f2e954e911d2477bc6 Mon Sep 17 00:00:00 2001 From: Eh2406 Date: Fri, 21 Apr 2017 13:25:48 -0400 Subject: [PATCH 1/3] FromIterator and Extend Cow for String --- src/libcollections/string.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 8d6cf30511260..675d701df7398 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1538,6 +1538,15 @@ impl FromIterator for String { } } +#[stable(feature = "herd_cows", since = "1.9.0")] +impl<'a> FromIterator> for String { + fn from_iter>>(iter: I) -> String { + let mut buf = String::new(); + buf.extend(iter); + buf + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Extend for String { fn extend>(&mut self, iter: I) { @@ -1575,6 +1584,15 @@ impl Extend for String { } } +#[stable(feature = "herd_cows", since = "1.9.0")] +impl<'a> Extend> for String { + fn extend>>(&mut self, iter: I) { + for s in iter { + self.push_str(&s) + } + } +} + /// A convenience impl that delegates to the impl for `&str` #[unstable(feature = "pattern", reason = "API not fully fleshed out and ready to be stabilized", From 343bcdf4e914d816c9561bad02a59a6f72f69604 Mon Sep 17 00:00:00 2001 From: Eh2406 Date: Fri, 21 Apr 2017 22:18:56 -0400 Subject: [PATCH 2/3] FromIterator and Extend Borrow for String --- src/libcollections/string.rs | 73 ++++++++++-------------------------- 1 file changed, 19 insertions(+), 54 deletions(-) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 675d701df7398..85a2c138507cc 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -56,6 +56,7 @@ #![stable(feature = "rust1", since = "1.0.0")] +use core::borrow::Borrow; use core::fmt; use core::hash; use core::iter::{FromIterator, FusedIterator}; @@ -476,9 +477,9 @@ impl String { Ok(..) => Ok(String { vec: vec }), Err(e) => { Err(FromUtf8Error { - bytes: vec, - error: e, - }) + bytes: vec, + error: e, + }) } } } @@ -663,7 +664,9 @@ impl String { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf16(v: &[u16]) -> Result { - decode_utf16(v.iter().cloned()).collect::>().map_err(|_| FromUtf16Error(())) + decode_utf16(v.iter().cloned()) + .collect::>() + .map_err(|_| FromUtf16Error(())) } /// Decode a UTF-16 encoded vector `v` into a string, replacing @@ -685,7 +688,9 @@ impl String { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf16_lossy(v: &[u16]) -> String { - decode_utf16(v.iter().cloned()).map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)).collect() + decode_utf16(v.iter().cloned()) + .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)) + .collect() } /// Creates a new `String` from a length, capacity, and pointer. @@ -977,7 +982,10 @@ impl String { pub fn push(&mut self, ch: char) { match ch.len_utf8() { 1 => self.vec.push(ch as u8), - _ => self.vec.extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()), + _ => { + self.vec + .extend_from_slice(ch.encode_utf8(&mut [0; 4]).as_bytes()) + } } } @@ -1520,27 +1528,9 @@ impl<'a> FromIterator<&'a char> for String { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> FromIterator<&'a str> for String { - fn from_iter>(iter: I) -> String { - let mut buf = String::new(); - buf.extend(iter); - buf - } -} - -#[stable(feature = "extend_string", since = "1.4.0")] -impl FromIterator for String { - fn from_iter>(iter: I) -> String { - let mut buf = String::new(); - buf.extend(iter); - buf - } -} - #[stable(feature = "herd_cows", since = "1.9.0")] -impl<'a> FromIterator> for String { - fn from_iter>>(iter: I) -> String { +impl> FromIterator for String { + fn from_iter>(iter: I) -> String { let mut buf = String::new(); buf.extend(iter); buf @@ -1559,34 +1549,9 @@ impl Extend for String { } } -#[stable(feature = "extend_ref", since = "1.2.0")] -impl<'a> Extend<&'a char> for String { - fn extend>(&mut self, iter: I) { - self.extend(iter.into_iter().cloned()); - } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a> Extend<&'a str> for String { - fn extend>(&mut self, iter: I) { - for s in iter { - self.push_str(s) - } - } -} - -#[stable(feature = "extend_string", since = "1.4.0")] -impl Extend for String { - fn extend>(&mut self, iter: I) { - for s in iter { - self.push_str(&s) - } - } -} - #[stable(feature = "herd_cows", since = "1.9.0")] -impl<'a> Extend> for String { - fn extend>>(&mut self, iter: I) { +impl> Extend for String { + fn extend>(&mut self, iter: I) { for s in iter { self.push_str(&s) } @@ -1959,7 +1924,7 @@ impl ToString for T { use core::fmt::Write; let mut buf = String::new(); buf.write_fmt(format_args!("{}", self)) - .expect("a Display implementation return an error unexpectedly"); + .expect("a Display implementation return an error unexpectedly"); buf.shrink_to_fit(); buf } From c215565557c88dc0aa62ea0982ebb9eefebaff1e Mon Sep 17 00:00:00 2001 From: Eh2406 Date: Fri, 21 Apr 2017 23:06:27 -0400 Subject: [PATCH 3/3] remove the conflicting implementations --- src/libcollections/string.rs | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 85a2c138507cc..8061c53ca976d 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -1510,24 +1510,6 @@ impl Clone for String { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl FromIterator for String { - fn from_iter>(iter: I) -> String { - let mut buf = String::new(); - buf.extend(iter); - buf - } -} - -#[stable(feature = "string_from_iter_by_ref", since = "1.17.0")] -impl<'a> FromIterator<&'a char> for String { - fn from_iter>(iter: I) -> String { - let mut buf = String::new(); - buf.extend(iter); - buf - } -} - #[stable(feature = "herd_cows", since = "1.9.0")] impl> FromIterator for String { fn from_iter>(iter: I) -> String { @@ -1537,18 +1519,6 @@ impl> FromIterator for String { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl Extend for String { - fn extend>(&mut self, iter: I) { - let iterator = iter.into_iter(); - let (lower_bound, _) = iterator.size_hint(); - self.reserve(lower_bound); - for ch in iterator { - self.push(ch) - } - } -} - #[stable(feature = "herd_cows", since = "1.9.0")] impl> Extend for String { fn extend>(&mut self, iter: I) {