Skip to content

Commit 8e9f8f9

Browse files
committed
collections: impl Deref for Vec/String
This commit adds the following impls: impl<T> Deref<[T]> for Vec<T> impl<T> DerefMut<[T]> for Vec<T> impl Deref<str> for String This commit also removes all duplicated inherent methods from vectors and strings as implementations will now silently call through to the slice implementation. Some breakage occurred at std and beneath due to inherent methods removed in favor of those in the slice traits and std doesn't use its own prelude, cc rust-lang#18424
1 parent dd71136 commit 8e9f8f9

File tree

8 files changed

+102
-437
lines changed

8 files changed

+102
-437
lines changed

src/libcollections/str.rs

-69
Original file line numberDiff line numberDiff line change
@@ -1677,40 +1677,6 @@ mod tests {
16771677
assert_eq!(pos, p.len());
16781678
}
16791679

1680-
#[test]
1681-
fn test_split_char_iterator() {
1682-
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
1683-
1684-
let split: Vec<&str> = data.split(' ').collect();
1685-
assert_eq!( split, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1686-
1687-
let mut rsplit: Vec<&str> = data.split(' ').rev().collect();
1688-
rsplit.reverse();
1689-
assert_eq!(rsplit, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1690-
1691-
let split: Vec<&str> = data.split(|c: char| c == ' ').collect();
1692-
assert_eq!( split, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1693-
1694-
let mut rsplit: Vec<&str> = data.split(|c: char| c == ' ').rev().collect();
1695-
rsplit.reverse();
1696-
assert_eq!(rsplit, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
1697-
1698-
// Unicode
1699-
let split: Vec<&str> = data.split('ä').collect();
1700-
assert_eq!( split, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1701-
1702-
let mut rsplit: Vec<&str> = data.split('ä').rev().collect();
1703-
rsplit.reverse();
1704-
assert_eq!(rsplit, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1705-
1706-
let split: Vec<&str> = data.split(|c: char| c == 'ä').collect();
1707-
assert_eq!( split, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1708-
1709-
let mut rsplit: Vec<&str> = data.split(|c: char| c == 'ä').rev().collect();
1710-
rsplit.reverse();
1711-
assert_eq!(rsplit, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
1712-
}
1713-
17141680
#[test]
17151681
fn test_splitn_char_iterator() {
17161682
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
@@ -1729,28 +1695,6 @@ mod tests {
17291695
assert_eq!(split, vec!["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
17301696
}
17311697

1732-
#[test]
1733-
fn test_rsplitn_char_iterator() {
1734-
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
1735-
1736-
let mut split: Vec<&str> = data.rsplitn(3, ' ').collect();
1737-
split.reverse();
1738-
assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
1739-
1740-
let mut split: Vec<&str> = data.rsplitn(3, |c: char| c == ' ').collect();
1741-
split.reverse();
1742-
assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
1743-
1744-
// Unicode
1745-
let mut split: Vec<&str> = data.rsplitn(3, 'ä').collect();
1746-
split.reverse();
1747-
assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
1748-
1749-
let mut split: Vec<&str> = data.rsplitn(3, |c: char| c == 'ä').collect();
1750-
split.reverse();
1751-
assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
1752-
}
1753-
17541698
#[test]
17551699
fn test_split_char_iterator_no_trailing() {
17561700
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
@@ -1762,19 +1706,6 @@ mod tests {
17621706
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb"]);
17631707
}
17641708

1765-
#[test]
1766-
fn test_rev_split_char_iterator_no_trailing() {
1767-
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
1768-
1769-
let mut split: Vec<&str> = data.split('\n').rev().collect();
1770-
split.reverse();
1771-
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb", ""]);
1772-
1773-
let mut split: Vec<&str> = data.split_terminator('\n').rev().collect();
1774-
split.reverse();
1775-
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb"]);
1776-
}
1777-
17781709
#[test]
17791710
fn test_words() {
17801711
let data = "\n \tMäry häd\tä little lämb\nLittle lämb\n";

src/libcollections/string.rs

+5
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,11 @@ impl ops::Slice<uint, str> for String {
744744
}
745745
}
746746

747+
#[experimental = "waiting on Deref stabilization"]
748+
impl ops::Deref<str> for String {
749+
fn deref<'a>(&'a self) -> &'a str { self.as_slice() }
750+
}
751+
747752
/// Wrapper type providing a `&String` reference via `Deref`.
748753
#[experimental]
749754
pub struct DerefString<'a> {

0 commit comments

Comments
 (0)