]> git.lizzy.rs Git - rust.git/commitdiff
Libs: Unify concat and concat_vec
authorAaron Turon <aturon@mozilla.com>
Mon, 3 Nov 2014 04:27:46 +0000 (20:27 -0800)
committerAaron Turon <aturon@mozilla.com>
Tue, 30 Dec 2014 20:02:22 +0000 (12:02 -0800)
We've long had traits `StrVector` and `VectorVector` providing
`concat`/`connect` and `concat_vec`/`connect_vec` respectively. The
reason for the distinction is that coherence rules did not used to be
robust enough to allow impls on e.g. `Vec<String>` versus `Vec<&[T]>`.

This commit consolidates the traits into a single `SliceConcatExt` trait
provided by `slice` and the preldue (where it replaces `StrVector`,
which is removed.)

[breaking-change]

src/libcollections/slice.rs
src/libcollections/str.rs
src/libstd/path/posix.rs
src/libstd/path/windows.rs
src/libstd/prelude.rs

index ec522ae9e5aed9a48a993cf15a55cf7f7401218c..5bc99889f2ffe4a88fb49c0c431555d227d11daa 100644 (file)
@@ -993,19 +993,31 @@ fn prev_permutation(&mut self) -> bool {
     }
 }
 
-#[allow(missing_docs)]
-pub trait VectorVector<T> for Sized? {
-    // FIXME #5898: calling these .concat and .connect conflicts with
-    // StrVector::con{cat,nect}, since they have generic contents.
-    /// Flattens a vector of vectors of `T` into a single `Vec<T>`.
-    fn concat_vec(&self) -> Vec<T>;
-
-    /// Concatenate a vector of vectors, placing a given separator between each.
-    fn connect_vec(&self, sep: &T) -> Vec<T>;
+#[unstable = "U should be an associated type"]
+/// An extension trait for concatenating slices
+pub trait SliceConcatExt<Sized? T, U> for Sized? {
+    /// Flattens a slice of `T` into a single value `U`.
+    #[stable]
+    fn concat(&self) -> U;
+
+    #[deprecated = "renamed to concat"]
+    fn concat_vec(&self) -> U {
+        self.concat()
+    }
+
+    /// Flattens a slice of `T` into a single value `U`, placing a
+    /// given seperator between each.
+    #[stable]
+    fn connect(&self, sep: &T) -> U;
+
+    #[deprecated = "renamed to connect"]
+    fn connect_vec(&self, sep: &T) -> U {
+        self.connect(sep)
+    }
 }
 
-impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
-    fn concat_vec(&self) -> Vec<T> {
+impl<T: Clone, V: AsSlice<T>> SliceConcatExt<T, Vec<T>> for [V] {
+    fn concat(&self) -> Vec<T> {
         let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len());
         let mut result = Vec::with_capacity(size);
         for v in self.iter() {
@@ -1014,7 +1026,7 @@ fn concat_vec(&self) -> Vec<T> {
         result
     }
 
-    fn connect_vec(&self, sep: &T) -> Vec<T> {
+    fn connect(&self, sep: &T) -> Vec<T> {
         let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len());
         let mut result = Vec::with_capacity(size + self.len());
         let mut first = true;
index 7c7a7e19a2f18c92ece50074a8dceaf1e5992643..60449c817cbd388ad901841201c2c99a593aad6f 100644 (file)
@@ -77,6 +77,7 @@
 use string::String;
 use unicode;
 use vec::Vec;
+use slice::SliceConcatExt;
 
 pub use core::str::{from_utf8, CharEq, Chars, CharIndices};
 pub use core::str::{Bytes, CharSplits, is_utf8};
 Section: Creating a string
 */
 
-/// Methods for vectors of strings.
-#[unstable = "functionality may be replaced with iterators"]
-pub trait StrVector for Sized? {
-    /// Concatenates a vector of strings.
-    ///
-    /// # Examples
-    ///
-    /// ```rust
-    /// let first = "Restaurant at the End of the".to_string();
-    /// let second = " Universe".to_string();
-    /// let string_vec = vec![first, second];
-    /// assert_eq!(string_vec.concat(), "Restaurant at the End of the Universe".to_string());
-    /// ```
-    fn concat(&self) -> String;
-
-    /// Concatenates a vector of strings, placing a given separator between each.
-    ///
-    /// # Examples
-    ///
-    /// ```rust
-    /// let first = "Roast".to_string();
-    /// let second = "Sirloin Steak".to_string();
-    /// let string_vec = vec![first, second];
-    /// assert_eq!(string_vec.connect(", "), "Roast, Sirloin Steak".to_string());
-    /// ```
-    fn connect(&self, sep: &str) -> String;
-}
-
-#[allow(deprecated)]
-impl<S: Str> StrVector for [S] {
+impl<S: Str> SliceConcatExt<str, String> for [S] {
     fn concat(&self) -> String {
         if self.is_empty() {
             return String::new();
@@ -169,16 +141,9 @@ fn connect(&self, sep: &str) -> String {
     }
 }
 
-impl<S: Str, T: AsSlice<S>> StrVector for T {
-    #[inline]
-    fn concat(&self) -> String {
-        self.as_slice().concat()
-    }
-
-    #[inline]
-    fn connect(&self, sep: &str) -> String {
-        self.as_slice().connect(sep)
-    }
+impl<S: Str> SliceConcatExt<str, String> for Vec<S> {
+    fn concat(&self) -> String { self[].concat() }
+    fn connect(&self, sep: &str) -> String { self[].connect(sep) }
 }
 
 /*
index 60f147eac9b2b490be0e4dc9086838f9065a8c65..41cbaa2b8076b893ed1e2d82022af44d66e361f7 100644 (file)
@@ -22,7 +22,7 @@
 use kinds::Sized;
 use str::{FromStr, Str};
 use str;
-use slice::{CloneSliceExt, Splits, AsSlice, VectorVector,
+use slice::{CloneSliceExt, Split, AsSlice, SliceConcatExt,
             PartialEqSliceExt, SliceExt};
 use vec::Vec;
 
@@ -306,7 +306,7 @@ fn path_relative_from(&self, base: &Path) -> Option<Path> {
                     }
                 }
             }
-            Some(Path::new(comps.connect_vec(&SEP_BYTE)))
+            Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
         }
     }
 
index 879a96e802643429170357e93a876ab822d54dc1..165d2c32416b0ae317f750227ada2b281f539cb3 100644 (file)
@@ -25,8 +25,8 @@
 use mem;
 use option::Option;
 use option::Option::{Some, None};
-use slice::SliceExt;
-use str::{SplitTerminator, FromStr, StrVector, StrExt};
+use slice::{AsSlice, SliceExt, SliceConcatExt};
+use str::{CharSplits, FromStr, Str, StrAllocating, StrPrelude};
 use string::{String, ToString};
 use unicode::char::UnicodeChar;
 use vec::Vec;
index 11eb569e5b53a3bd2e6a9a92f27402c4a3f8a12b..f016683e3d0a99991cf5f07db5befa71e6edb27e 100644 (file)
 #[doc(no_inline)] pub use core::prelude::{Tuple1, Tuple2, Tuple3, Tuple4};
 #[doc(no_inline)] pub use core::prelude::{Tuple5, Tuple6, Tuple7, Tuple8};
 #[doc(no_inline)] pub use core::prelude::{Tuple9, Tuple10, Tuple11, Tuple12};
-#[doc(no_inline)] pub use str::{Str, StrVector};
-#[doc(no_inline)] pub use str::StrExt;
+#[doc(no_inline)] pub use str::{Str, StrExt};
 #[doc(no_inline)] pub use slice::AsSlice;
-#[doc(no_inline)] pub use slice::{VectorVector, PartialEqSliceExt};
+#[doc(no_inline)] pub use slice::{SliceConcatExt, PartialEqSliceExt};
 #[doc(no_inline)] pub use slice::{CloneSliceExt, OrdSliceExt, SliceExt};
 #[doc(no_inline)] pub use slice::{BoxedSliceExt};
 #[doc(no_inline)] pub use string::{IntoString, String, ToString};