Navigation Menu

Skip to content

Commit

Permalink
auto merge of #19192 : nodakai/rust/generalize-strvector, r=alexcrichton
Browse files Browse the repository at this point in the history
A single impl supports all of `[T]`, `Vec<T>` and `CVec<T>`.

Once `Iterable` is implemented, we will prefer it to `SlicePrelude`.
But the `with_capacity()` part might become tricky.
  • Loading branch information
bors committed Nov 24, 2014
2 parents 2eed1dd + ef3b88c commit 3959013
Showing 1 changed file with 75 additions and 36 deletions.
111 changes: 75 additions & 36 deletions src/libcollections/str.rs
Expand Up @@ -163,7 +163,7 @@ impl<S: Str> StrVector for [S] {
}
}

impl<S: Str> StrVector for Vec<S> {
impl<S: Str, T: AsSlice<S>> StrVector for T {
#[inline]
fn concat(&self) -> String {
self.as_slice().concat()
Expand Down Expand Up @@ -929,54 +929,93 @@ mod tests {
assert_eq!("ะเทศไท", "ประเทศไทย中华Việt Nam".slice_chars(2, 8));
}

#[test]
fn test_concat() {
fn t(v: &[String], s: &str) {
assert_eq!(v.concat().as_slice(), s);
struct S {
x: [String, .. 2]
}

impl AsSlice<String> for S {
fn as_slice<'a> (&'a self) -> &'a [String] {
&self.x
}
}

fn s(x: &str) -> String { x.into_string() }

macro_rules! test_concat {
($expected: expr, $string: expr) => {
{
let s = $string.concat();
assert_eq!($expected, s.as_slice());
}
}
t(&[String::from_str("you"), String::from_str("know"),
String::from_str("I'm"),
String::from_str("no"), String::from_str("good")],
"youknowI'mnogood");
let v: &[String] = &[];
t(v, "");
t(&[String::from_str("hi")], "hi");
}

#[test]
fn test_connect() {
fn t(v: &[String], sep: &str, s: &str) {
assert_eq!(v.connect(sep).as_slice(), s);
fn test_concat_for_different_types() {
test_concat!("ab", ["a", "b"]);
test_concat!("ab", [s("a"), s("b")]);
test_concat!("ab", vec!["a", "b"]);
test_concat!("ab", vec!["a", "b"].as_slice());
test_concat!("ab", vec![s("a"), s("b")]);

let mut v0 = ["a", "b"];
let mut v1 = [s("a"), s("b")];
unsafe {
use std::c_vec::CVec;

test_concat!("ab", CVec::new(v0.as_mut_ptr(), v0.len()));
test_concat!("ab", CVec::new(v1.as_mut_ptr(), v1.len()));
}
t(&[String::from_str("you"), String::from_str("know"),
String::from_str("I'm"),
String::from_str("no"), String::from_str("good")],
" ", "you know I'm no good");
let v: &[String] = &[];
t(v, " ", "");
t(&[String::from_str("hi")], " ", "hi");

test_concat!("ab", S { x: [s("a"), s("b")] });
}

#[test]
fn test_concat_slices() {
fn t(v: &[&str], s: &str) {
assert_eq!(v.concat().as_slice(), s);
fn test_concat_for_different_lengths() {
let empty: &[&str] = &[];
test_concat!("", empty);
test_concat!("a", ["a"]);
test_concat!("ab", ["a", "b"]);
test_concat!("abc", ["", "a", "bc"]);
}

macro_rules! test_connect {
($expected: expr, $string: expr, $delim: expr) => {
{
let s = $string.connect($delim);
assert_eq!($expected, s.as_slice());
}
}
t(&["you", "know", "I'm", "no", "good"], "youknowI'mnogood");
let v: &[&str] = &[];
t(v, "");
t(&["hi"], "hi");
}

#[test]
fn test_connect_slices() {
fn t(v: &[&str], sep: &str, s: &str) {
assert_eq!(v.connect(sep).as_slice(), s);
fn test_connect_for_different_types() {
test_connect!("a-b", ["a", "b"], "-");
let hyphen = "-".into_string();
test_connect!("a-b", [s("a"), s("b")], hyphen.as_slice());
test_connect!("a-b", vec!["a", "b"], hyphen.as_slice());
test_connect!("a-b", vec!["a", "b"].as_slice(), "-");
test_connect!("a-b", vec![s("a"), s("b")], "-");

let mut v0 = ["a", "b"];
let mut v1 = [s("a"), s("b")];
unsafe {
use std::c_vec::CVec;

test_connect!("a-b", CVec::new(v0.as_mut_ptr(), v0.len()), "-");
test_connect!("a-b", CVec::new(v1.as_mut_ptr(), v1.len()), hyphen.as_slice());
}
t(&["you", "know", "I'm", "no", "good"],
" ", "you know I'm no good");
t(&[], " ", "");
t(&["hi"], " ", "hi");

test_connect!("a-b", S { x: [s("a"), s("b")] }, "-");
}

#[test]
fn test_connect_for_different_lengths() {
let empty: &[&str] = &[];
test_connect!("", empty, "-");
test_connect!("a", ["a"], "-");
test_connect!("a-b", ["a", "b"], "-");
test_connect!("-a-bc", ["", "a", "bc"], "-");
}

#[test]
Expand Down

0 comments on commit 3959013

Please sign in to comment.