Skip to content

Commit dbb778c

Browse files
author
Ulrik Sverdrup
committed
collections: Convert SliceConcatExt to use associated types
Coherence now allows this, we have SliceConcatExt<T> for [V] where T: Sized + Clone and SliceConcatExt<str> for [S], these don't conflict because str is never Sized.
1 parent 0d7d3ec commit dbb778c

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/libcollections/slice.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -996,9 +996,12 @@ impl<T> [T] {
996996
////////////////////////////////////////////////////////////////////////////////
997997
// Extension traits for slices over specific kinds of data
998998
////////////////////////////////////////////////////////////////////////////////
999-
#[unstable(feature = "collections", reason = "U should be an associated type")]
999+
#[unstable(feature = "collections", reason = "recently changed")]
10001000
/// An extension trait for concatenating slices
1001-
pub trait SliceConcatExt<T: ?Sized, U> {
1001+
pub trait SliceConcatExt<T: ?Sized> {
1002+
/// The resulting type after concatenation
1003+
type Output;
1004+
10021005
/// Flattens a slice of `T` into a single value `U`.
10031006
///
10041007
/// # Examples
@@ -1011,7 +1014,7 @@ pub trait SliceConcatExt<T: ?Sized, U> {
10111014
/// println!("{}", s); // prints "helloworld"
10121015
/// ```
10131016
#[stable(feature = "rust1", since = "1.0.0")]
1014-
fn concat(&self) -> U;
1017+
fn concat(&self) -> Self::Output;
10151018

10161019
/// Flattens a slice of `T` into a single value `U`, placing a given separator between each.
10171020
///
@@ -1025,10 +1028,12 @@ pub trait SliceConcatExt<T: ?Sized, U> {
10251028
/// println!("{}", s); // prints "hello world"
10261029
/// ```
10271030
#[stable(feature = "rust1", since = "1.0.0")]
1028-
fn connect(&self, sep: &T) -> U;
1031+
fn connect(&self, sep: &T) -> Self::Output;
10291032
}
10301033

1031-
impl<T: Clone, V: AsRef<[T]>> SliceConcatExt<T, Vec<T>> for [V] {
1034+
impl<T: Clone, V: AsRef<[T]>> SliceConcatExt<T> for [V] {
1035+
type Output = Vec<T>;
1036+
10321037
fn concat(&self) -> Vec<T> {
10331038
let size = self.iter().fold(0, |acc, v| acc + v.as_ref().len());
10341039
let mut result = Vec::with_capacity(size);

src/libcollections/str.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ pub use core::str::pattern;
8383
Section: Creating a string
8484
*/
8585

86-
impl<S: AsRef<str>> SliceConcatExt<str, String> for [S] {
86+
impl<S: AsRef<str>> SliceConcatExt<str> for [S] {
87+
type Output = String;
88+
8789
fn concat(&self) -> String {
8890
if self.is_empty() {
8991
return String::new();

0 commit comments

Comments
 (0)