Skip to content

Commit 2ca77f1

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 2ca77f1

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/libcollections/slice.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -996,9 +996,13 @@ 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+
#[unstable(feature = "collections", reason = "recently changed")]
1003+
/// The resulting type after concatenation
1004+
type Output;
1005+
10021006
/// Flattens a slice of `T` into a single value `U`.
10031007
///
10041008
/// # Examples
@@ -1011,7 +1015,7 @@ pub trait SliceConcatExt<T: ?Sized, U> {
10111015
/// println!("{}", s); // prints "helloworld"
10121016
/// ```
10131017
#[stable(feature = "rust1", since = "1.0.0")]
1014-
fn concat(&self) -> U;
1018+
fn concat(&self) -> Self::Output;
10151019

10161020
/// Flattens a slice of `T` into a single value `U`, placing a given separator between each.
10171021
///
@@ -1025,10 +1029,12 @@ pub trait SliceConcatExt<T: ?Sized, U> {
10251029
/// println!("{}", s); // prints "hello world"
10261030
/// ```
10271031
#[stable(feature = "rust1", since = "1.0.0")]
1028-
fn connect(&self, sep: &T) -> U;
1032+
fn connect(&self, sep: &T) -> Self::Output;
10291033
}
10301034

1031-
impl<T: Clone, V: AsRef<[T]>> SliceConcatExt<T, Vec<T>> for [V] {
1035+
impl<T: Clone, V: AsRef<[T]>> SliceConcatExt<T> for [V] {
1036+
type Output = Vec<T>;
1037+
10321038
fn concat(&self) -> Vec<T> {
10331039
let size = self.iter().fold(0, |acc, v| acc + v.as_ref().len());
10341040
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)