Skip to content

Libs: Unify concat and concat_vec #18561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ pub use core::slice::{Found, NotFound};

// Functional utilities

#[experimental = "U should be an associated type"]
#[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>;
pub trait Concat<Sized? T, U> for Sized? {
/// Flattens a slice of `T` into a single value `U`.
fn concat(&self) -> U;

/// Concatenate a vector of vectors, placing a given separator between each.
fn connect_vec(&self, sep: &T) -> Vec<T>;
/// Flattens a slice of `T` into a single value `U`, placing a
/// given seperator between each.
fn connect(&self, sep: &T) -> U;
}

impl<T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
fn concat_vec(&self) -> Vec<T> {
impl<T: Clone, V: AsSlice<T>> Concat<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 @@ -129,7 +129,7 @@ impl<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 All @@ -141,6 +141,28 @@ impl<T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
}
}

#[allow(missing_docs)]
#[deprecated]
pub trait VectorVector<T> for Sized? {
/// Deprecated: use `concat` instead.
#[deprecated = "use concat instead"]
fn concat_vec(&self) -> Vec<T>;

/// Deprecated: use `connect` instead.
#[deprecated = "use connect instead"]
fn connect_vec(&self, sep: &T) -> Vec<T>;
}

impl<T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
fn concat_vec(&self) -> Vec<T> {
self.concat()
}

fn connect_vec(&self, sep: &T) -> Vec<T> {
self.connect(sep)
}
}

/// An iterator that yields the element swaps needed to produce
/// a sequence of all possible permutations for an indexed sequence of
/// elements. Each permutation is only a single swap apart.
Expand Down
44 changes: 5 additions & 39 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ use core::default::Default;
use core::fmt;
use core::cmp;
use core::iter::AdditiveIterator;
use core::kinds::Sized;
use core::prelude::{Char, Clone, Eq, Equiv, ImmutableSlice};
use core::prelude::{Iterator, MutableSlice, None, Option, Ord, Ordering};
use core::prelude::{PartialEq, PartialOrd, Result, AsSlice, Some, Tuple2};
Expand All @@ -66,6 +65,7 @@ use ringbuf::RingBuf;
use string::String;
use unicode;
use vec::Vec;
use slice::Concat;

pub use core::str::{from_utf8, CharEq, Chars, CharOffsets};
pub use core::str::{Bytes, CharSplits};
Expand All @@ -80,34 +80,7 @@ pub use unicode::str::{UnicodeStrSlice, Words, Graphemes, GraphemeIndices};
Section: Creating a string
*/

/// Methods for vectors of strings.
pub trait StrVector for Sized? {
/// Concatenates a vector of strings.
///
/// # Example
///
/// ```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.
///
/// # Example
///
/// ```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;
}

impl<S: Str> StrVector for [S] {
impl<S: Str> Concat<str, String> for [S] {
fn concat(&self) -> String {
if self.is_empty() {
return String::new();
Expand Down Expand Up @@ -154,16 +127,9 @@ impl<S: Str> StrVector for [S] {
}
}

impl<S: Str> StrVector for Vec<S> {
#[inline]
fn concat(&self) -> String {
self.as_slice().concat()
}

#[inline]
fn connect(&self, sep: &str) -> String {
self.as_slice().connect(sep)
}
impl<S: Str> Concat<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 @@ -20,7 +20,7 @@ use iter::{DoubleEndedIterator, AdditiveIterator, Extendable, Iterator, Map};
use option::{Option, None, Some};
use str::Str;
use str;
use slice::{CloneableVector, Splits, AsSlice, VectorVector,
use slice::{CloneableVector, Splits, AsSlice, Concat,
ImmutablePartialEqSlice, ImmutableSlice};
use vec::Vec;

Expand Down Expand Up @@ -315,7 +315,7 @@ impl GenericPath for Path {
}
}
}
Some(Path::new(comps.as_slice().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 @@ -22,8 +22,8 @@ use io::Writer;
use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Iterator, Map};
use mem;
use option::{Option, Some, None};
use slice::{AsSlice, ImmutableSlice};
use str::{CharSplits, Str, StrAllocating, StrVector, StrSlice};
use slice::{AsSlice, ImmutableSlice, Concat};
use str::{CharSplits, Str, StrAllocating, StrSlice};
use string::String;
use unicode::char::UnicodeChar;
use vec::Vec;
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#[doc(no_inline)] pub use path::{GenericPath, Path, PosixPath, WindowsPath};
#[doc(no_inline)] pub use ptr::{RawPtr, RawMutPtr};
#[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek};
#[doc(no_inline)] pub use str::{Str, StrVector, StrSlice};
#[doc(no_inline)] pub use str::{Str, StrSlice};
#[doc(no_inline)] pub use str::{IntoMaybeOwned, StrAllocating, UnicodeStrSlice};
#[doc(no_inline)] pub use to_string::{ToString, IntoStr};
#[doc(no_inline)] pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4};
Expand All @@ -86,7 +86,7 @@
#[doc(no_inline)] pub use slice::{MutableCloneableSlice, MutableOrdSlice};
#[doc(no_inline)] pub use slice::{ImmutableSlice, MutableSlice};
#[doc(no_inline)] pub use slice::{ImmutablePartialEqSlice, ImmutableOrdSlice};
#[doc(no_inline)] pub use slice::{AsSlice, VectorVector, BoxedSlice};
#[doc(no_inline)] pub use slice::{AsSlice, Concat, VectorVector, BoxedSlice};
#[doc(no_inline)] pub use slice::MutableSliceAllocating;
#[doc(no_inline)] pub use string::String;
#[doc(no_inline)] pub use vec::Vec;
Expand Down