Skip to content

Commit e91d810

Browse files
committed
Libs: Unify concat and concat_vec
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]
1 parent 4f863a3 commit e91d810

File tree

5 files changed

+35
-59
lines changed

5 files changed

+35
-59
lines changed

src/libcollections/slice.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -993,19 +993,31 @@ impl<T: Ord> OrdSliceExt<T> for [T] {
993993
}
994994
}
995995

996-
#[allow(missing_docs)]
997-
pub trait VectorVector<T> for Sized? {
998-
// FIXME #5898: calling these .concat and .connect conflicts with
999-
// StrVector::con{cat,nect}, since they have generic contents.
1000-
/// Flattens a vector of vectors of `T` into a single `Vec<T>`.
1001-
fn concat_vec(&self) -> Vec<T>;
1002-
1003-
/// Concatenate a vector of vectors, placing a given separator between each.
1004-
fn connect_vec(&self, sep: &T) -> Vec<T>;
996+
#[unstable = "U should be an associated type"]
997+
/// An extension trait for concatenating slices
998+
pub trait SliceConcatExt<Sized? T, U> for Sized? {
999+
/// Flattens a slice of `T` into a single value `U`.
1000+
#[stable]
1001+
fn concat(&self) -> U;
1002+
1003+
#[deprecated = "renamed to concat"]
1004+
fn concat_vec(&self) -> U {
1005+
self.concat()
1006+
}
1007+
1008+
/// Flattens a slice of `T` into a single value `U`, placing a
1009+
/// given seperator between each.
1010+
#[stable]
1011+
fn connect(&self, sep: &T) -> U;
1012+
1013+
#[deprecated = "renamed to connect"]
1014+
fn connect_vec(&self, sep: &T) -> U {
1015+
self.connect(sep)
1016+
}
10051017
}
10061018

1007-
impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
1008-
fn concat_vec(&self) -> Vec<T> {
1019+
impl<T: Clone, V: AsSlice<T>> SliceConcatExt<T, Vec<T>> for [V] {
1020+
fn concat(&self) -> Vec<T> {
10091021
let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len());
10101022
let mut result = Vec::with_capacity(size);
10111023
for v in self.iter() {
@@ -1014,7 +1026,7 @@ impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
10141026
result
10151027
}
10161028

1017-
fn connect_vec(&self, sep: &T) -> Vec<T> {
1029+
fn connect(&self, sep: &T) -> Vec<T> {
10181030
let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len());
10191031
let mut result = Vec::with_capacity(size + self.len());
10201032
let mut first = true;

src/libcollections/str.rs

+5-40
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ use slice::SliceExt;
7777
use string::String;
7878
use unicode;
7979
use vec::Vec;
80+
use slice::SliceConcatExt;
8081

8182
pub use core::str::{from_utf8, CharEq, Chars, CharIndices};
8283
pub use core::str::{Bytes, CharSplits, is_utf8};
@@ -93,36 +94,7 @@ pub use core::str::{SplitN, RSplitN};
9394
Section: Creating a string
9495
*/
9596

96-
/// Methods for vectors of strings.
97-
#[unstable = "functionality may be replaced with iterators"]
98-
pub trait StrVector for Sized? {
99-
/// Concatenates a vector of strings.
100-
///
101-
/// # Examples
102-
///
103-
/// ```rust
104-
/// let first = "Restaurant at the End of the".to_string();
105-
/// let second = " Universe".to_string();
106-
/// let string_vec = vec![first, second];
107-
/// assert_eq!(string_vec.concat(), "Restaurant at the End of the Universe".to_string());
108-
/// ```
109-
fn concat(&self) -> String;
110-
111-
/// Concatenates a vector of strings, placing a given separator between each.
112-
///
113-
/// # Examples
114-
///
115-
/// ```rust
116-
/// let first = "Roast".to_string();
117-
/// let second = "Sirloin Steak".to_string();
118-
/// let string_vec = vec![first, second];
119-
/// assert_eq!(string_vec.connect(", "), "Roast, Sirloin Steak".to_string());
120-
/// ```
121-
fn connect(&self, sep: &str) -> String;
122-
}
123-
124-
#[allow(deprecated)]
125-
impl<S: Str> StrVector for [S] {
97+
impl<S: Str> SliceConcatExt<str, String> for [S] {
12698
fn concat(&self) -> String {
12799
if self.is_empty() {
128100
return String::new();
@@ -169,16 +141,9 @@ impl<S: Str> StrVector for [S] {
169141
}
170142
}
171143

172-
impl<S: Str, T: AsSlice<S>> StrVector for T {
173-
#[inline]
174-
fn concat(&self) -> String {
175-
self.as_slice().concat()
176-
}
177-
178-
#[inline]
179-
fn connect(&self, sep: &str) -> String {
180-
self.as_slice().connect(sep)
181-
}
144+
impl<S: Str> SliceConcatExt<str, String> for Vec<S> {
145+
fn concat(&self) -> String { self[].concat() }
146+
fn connect(&self, sep: &str) -> String { self[].connect(sep) }
182147
}
183148

184149
/*

src/libstd/path/posix.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use option::Option::{None, Some};
2222
use kinds::Sized;
2323
use str::{FromStr, Str};
2424
use str;
25-
use slice::{CloneSliceExt, Splits, AsSlice, VectorVector,
25+
use slice::{CloneSliceExt, Split, AsSlice, SliceConcatExt,
2626
PartialEqSliceExt, SliceExt};
2727
use vec::Vec;
2828

@@ -306,7 +306,7 @@ impl GenericPath for Path {
306306
}
307307
}
308308
}
309-
Some(Path::new(comps.connect_vec(&SEP_BYTE)))
309+
Some(Path::new(comps.as_slice().connect(&SEP_BYTE)))
310310
}
311311
}
312312

src/libstd/path/windows.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use iter::{Iterator, IteratorExt, Map, repeat};
2525
use mem;
2626
use option::Option;
2727
use option::Option::{Some, None};
28-
use slice::SliceExt;
29-
use str::{SplitTerminator, FromStr, StrVector, StrExt};
28+
use slice::{AsSlice, SliceExt, SliceConcatExt};
29+
use str::{CharSplits, FromStr, Str, StrAllocating, StrPrelude};
3030
use string::{String, ToString};
3131
use unicode::char::UnicodeChar;
3232
use vec::Vec;

src/libstd/prelude.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@
8080
#[doc(no_inline)] pub use core::prelude::{Tuple1, Tuple2, Tuple3, Tuple4};
8181
#[doc(no_inline)] pub use core::prelude::{Tuple5, Tuple6, Tuple7, Tuple8};
8282
#[doc(no_inline)] pub use core::prelude::{Tuple9, Tuple10, Tuple11, Tuple12};
83-
#[doc(no_inline)] pub use str::{Str, StrVector};
84-
#[doc(no_inline)] pub use str::StrExt;
83+
#[doc(no_inline)] pub use str::{Str, StrExt};
8584
#[doc(no_inline)] pub use slice::AsSlice;
86-
#[doc(no_inline)] pub use slice::{VectorVector, PartialEqSliceExt};
85+
#[doc(no_inline)] pub use slice::{SliceConcatExt, PartialEqSliceExt};
8786
#[doc(no_inline)] pub use slice::{CloneSliceExt, OrdSliceExt, SliceExt};
8887
#[doc(no_inline)] pub use slice::{BoxedSliceExt};
8988
#[doc(no_inline)] pub use string::{IntoString, String, ToString};

0 commit comments

Comments
 (0)