Skip to content

Commit

Permalink
Auto merge of rust-lang#25120 - bluss:sliceconcatext, r=alexcrichton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed May 6, 2015
2 parents fc45fd9 + 2ca77f1 commit 5a83fa2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,13 @@ impl<T> [T] {
////////////////////////////////////////////////////////////////////////////////
// Extension traits for slices over specific kinds of data
////////////////////////////////////////////////////////////////////////////////
#[unstable(feature = "collections", reason = "U should be an associated type")]
#[unstable(feature = "collections", reason = "recently changed")]
/// An extension trait for concatenating slices
pub trait SliceConcatExt<T: ?Sized, U> {
pub trait SliceConcatExt<T: ?Sized> {
#[unstable(feature = "collections", reason = "recently changed")]
/// The resulting type after concatenation
type Output;

/// Flattens a slice of `T` into a single value `U`.
///
/// # Examples
Expand All @@ -1007,7 +1011,7 @@ pub trait SliceConcatExt<T: ?Sized, U> {
/// assert_eq!(["hello", "world"].concat(), "helloworld");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn concat(&self) -> U;
fn concat(&self) -> Self::Output;

/// Flattens a slice of `T` into a single value `U`, placing a given separator between each.
///
Expand All @@ -1017,10 +1021,12 @@ pub trait SliceConcatExt<T: ?Sized, U> {
/// assert_eq!(["hello", "world"].connect(" "), "hello world");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn connect(&self, sep: &T) -> U;
fn connect(&self, sep: &T) -> Self::Output;
}

impl<T: Clone, V: AsRef<[T]>> SliceConcatExt<T, Vec<T>> for [V] {
impl<T: Clone, V: AsRef<[T]>> SliceConcatExt<T> for [V] {
type Output = Vec<T>;

fn concat(&self) -> Vec<T> {
let size = self.iter().fold(0, |acc, v| acc + v.as_ref().len());
let mut result = Vec::with_capacity(size);
Expand Down
4 changes: 3 additions & 1 deletion src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ pub use core::str::pattern;
Section: Creating a string
*/

impl<S: AsRef<str>> SliceConcatExt<str, String> for [S] {
impl<S: AsRef<str>> SliceConcatExt<str> for [S] {
type Output = String;

fn concat(&self) -> String {
if self.is_empty() {
return String::new();
Expand Down

0 comments on commit 5a83fa2

Please sign in to comment.