Skip to content

Commit

Permalink
std: rename .connect/.concat in VectorVector to avoid conflicting wit…
Browse files Browse the repository at this point in the history
…h StrVector.

This is caused by StrVector having a generic implementation for &[S]
and so #5898 means that method resolution of ~[~[1]].concat() sees that
both StrVector and VectorVector have methods that (superficially) match.

They are now connect_vec and concat_vec, which means that they can actually be
called.
  • Loading branch information
huonw committed Jun 15, 2013
1 parent 38e0574 commit 1854256
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,31 +1002,33 @@ pub fn retain<T>(v: &mut ~[T], f: &fn(t: &T) -> bool) {
}

/// Flattens a vector of vectors of T into a single vector of T.
pub fn concat<T:Copy>(v: &[~[T]]) -> ~[T] { v.concat() }
pub fn concat<T:Copy>(v: &[~[T]]) -> ~[T] { v.concat_vec() }

/// Concatenate a vector of vectors, placing a given separator between each
pub fn connect<T:Copy>(v: &[~[T]], sep: &T) -> ~[T] { v.connect(sep) }
pub fn connect<T:Copy>(v: &[~[T]], sep: &T) -> ~[T] { v.connect_vec(sep) }

/// Flattens a vector of vectors of T into a single vector of T.
pub fn concat_slices<T:Copy>(v: &[&[T]]) -> ~[T] { v.concat() }
pub fn concat_slices<T:Copy>(v: &[&[T]]) -> ~[T] { v.concat_vec() }

/// Concatenate a vector of vectors, placing a given separator between each
pub fn connect_slices<T:Copy>(v: &[&[T]], sep: &T) -> ~[T] { v.connect(sep) }
pub fn connect_slices<T:Copy>(v: &[&[T]], sep: &T) -> ~[T] { v.connect_vec(sep) }

#[allow(missing_doc)]
pub trait VectorVector<T> {
pub fn concat(&self) -> ~[T];
pub fn connect(&self, sep: &T) -> ~[T];
// FIXME #5898: calling these .concat and .connect conflicts with
// StrVector::con{cat,nect}, since they have generic contents.
pub fn concat_vec(&self) -> ~[T];
pub fn connect_vec(&self, sep: &T) -> ~[T];
}

impl<'self, T:Copy> VectorVector<T> for &'self [~[T]] {
/// Flattens a vector of slices of T into a single vector of T.
pub fn concat(&self) -> ~[T] {
pub fn concat_vec(&self) -> ~[T] {
self.flat_map(|&inner| inner)
}

/// Concatenate a vector of vectors, placing a given separator between each.
pub fn connect(&self, sep: &T) -> ~[T] {
pub fn connect_vec(&self, sep: &T) -> ~[T] {
let mut r = ~[];
let mut first = true;
for self.each |&inner| {
Expand All @@ -1039,12 +1041,12 @@ impl<'self, T:Copy> VectorVector<T> for &'self [~[T]] {

impl<'self, T:Copy> VectorVector<T> for &'self [&'self [T]] {
/// Flattens a vector of slices of T into a single vector of T.
pub fn concat(&self) -> ~[T] {
pub fn concat_vec(&self) -> ~[T] {
self.flat_map(|&inner| inner.to_owned())
}

/// Concatenate a vector of slices, placing a given separator between each.
pub fn connect(&self, sep: &T) -> ~[T] {
pub fn connect_vec(&self, sep: &T) -> ~[T] {
let mut r = ~[];
let mut first = true;
for self.each |&inner| {
Expand Down Expand Up @@ -3704,25 +3706,25 @@ mod tests {
#[test]
fn test_concat() {
assert_eq!(concat([~[1], ~[2,3]]), ~[1, 2, 3]);
assert_eq!([~[1], ~[2,3]].concat(), ~[1, 2, 3]);
assert_eq!([~[1], ~[2,3]].concat_vec(), ~[1, 2, 3]);

assert_eq!(concat_slices([&[1], &[2,3]]), ~[1, 2, 3]);
assert_eq!([&[1], &[2,3]].concat(), ~[1, 2, 3]);
assert_eq!([&[1], &[2,3]].concat_vec(), ~[1, 2, 3]);
}

#[test]
fn test_connect() {
assert_eq!(connect([], &0), ~[]);
assert_eq!(connect([~[1], ~[2, 3]], &0), ~[1, 0, 2, 3]);
assert_eq!(connect([~[1], ~[2], ~[3]], &0), ~[1, 0, 2, 0, 3]);
assert_eq!([~[1], ~[2, 3]].connect(&0), ~[1, 0, 2, 3]);
assert_eq!([~[1], ~[2], ~[3]].connect(&0), ~[1, 0, 2, 0, 3]);
assert_eq!([~[1], ~[2, 3]].connect_vec(&0), ~[1, 0, 2, 3]);
assert_eq!([~[1], ~[2], ~[3]].connect_vec(&0), ~[1, 0, 2, 0, 3]);

assert_eq!(connect_slices([], &0), ~[]);
assert_eq!(connect_slices([&[1], &[2, 3]], &0), ~[1, 0, 2, 3]);
assert_eq!(connect_slices([&[1], &[2], &[3]], &0), ~[1, 0, 2, 0, 3]);
assert_eq!([&[1], &[2, 3]].connect(&0), ~[1, 0, 2, 3]);
assert_eq!([&[1], &[2], &[3]].connect(&0), ~[1, 0, 2, 0, 3]);
assert_eq!([&[1], &[2, 3]].connect_vec(&0), ~[1, 0, 2, 3]);
assert_eq!([&[1], &[2], &[3]].connect_vec(&0), ~[1, 0, 2, 0, 3]);
}

#[test]
Expand Down

5 comments on commit 1854256

@bors
Copy link
Contributor

@bors bors commented on 1854256 Jun 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Aatch
at huonw@1854256

@bors
Copy link
Contributor

@bors bors commented on 1854256 Jun 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging huonw/rust/vec-connect = 1854256 into auto

@bors
Copy link
Contributor

@bors bors commented on 1854256 Jun 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huonw/rust/vec-connect = 1854256 merged ok, testing candidate = 82f2e4d

@bors
Copy link
Contributor

@bors bors commented on 1854256 Jun 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 1854256 Jun 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 82f2e4d

Please sign in to comment.