Skip to content

Commit

Permalink
Libs: Unify concat and concat_vec
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
aturon committed Dec 30, 2014
1 parent 4f863a3 commit e91d810
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 59 deletions.
36 changes: 24 additions & 12 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,19 +993,31 @@ impl<T: Ord> OrdSliceExt<T> for [T] {
}
}

#[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() {
Expand All @@ -1014,7 +1026,7 @@ impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
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;
Expand Down
45 changes: 5 additions & 40 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ use slice::SliceExt;
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};
Expand All @@ -93,36 +94,7 @@ pub use core::str::{SplitN, RSplitN};
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();
Expand Down Expand Up @@ -169,16 +141,9 @@ impl<S: Str> StrVector for [S] {
}
}

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) }
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/path/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use option::Option::{None, Some};
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;

Expand Down Expand Up @@ -306,7 +306,7 @@ impl GenericPath for Path {
}
}
}
Some(Path::new(comps.connect_vec(&SEP_BYTE)))
Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libstd/path/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use iter::{Iterator, IteratorExt, Map, repeat};
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;
Expand Down
5 changes: 2 additions & 3 deletions src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@
#[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};
Expand Down

0 comments on commit e91d810

Please sign in to comment.