Skip to content

Commit

Permalink
Implement RFC 839
Browse files Browse the repository at this point in the history
Closes #25976.
  • Loading branch information
jooert committed Jun 8, 2015
1 parent a5979be commit b36ed7d
Show file tree
Hide file tree
Showing 22 changed files with 291 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/libcollections/binary_heap.rs
Expand Up @@ -760,3 +760,10 @@ impl<T: Ord> Extend<T> for BinaryHeap<T> {
}
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BinaryHeap<T> {
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}
14 changes: 14 additions & 0 deletions src/libcollections/bit.rs
Expand Up @@ -1070,6 +1070,13 @@ impl Extend<bool> for BitVec {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a> Extend<&'a bool> for BitVec {
fn extend<I: IntoIterator<Item=&'a bool>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Clone for BitVec {
#[inline]
Expand Down Expand Up @@ -1278,6 +1285,13 @@ impl Extend<usize> for BitSet {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a> Extend<&'a usize> for BitSet {
fn extend<I: IntoIterator<Item=&'a usize>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for BitSet {
#[inline]
Expand Down
7 changes: 7 additions & 0 deletions src/libcollections/btree/map.rs
Expand Up @@ -879,6 +879,13 @@ impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, K: Ord + Copy, V: Copy> Extend<(&'a K, &'a V)> for BTreeMap<K, V> {
fn extend<I: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: I) {
self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<K: Hash, V: Hash> Hash for BTreeMap<K, V> {
fn hash<H: Hasher>(&self, state: &mut H) {
Expand Down
7 changes: 7 additions & 0 deletions src/libcollections/btree/set.rs
Expand Up @@ -509,6 +509,13 @@ impl<T: Ord> Extend<T> for BTreeSet<T> {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, T: 'a + Ord + Copy> Extend<&'a T> for BTreeSet<T> {
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Ord> Default for BTreeSet<T> {
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
7 changes: 7 additions & 0 deletions src/libcollections/enum_set.rs
Expand Up @@ -288,3 +288,10 @@ impl<E:CLike> Extend<E> for EnumSet<E> {
}
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, E: 'a + CLike + Copy> Extend<&'a E> for EnumSet<E> {
fn extend<I: IntoIterator<Item=&'a E>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}
7 changes: 7 additions & 0 deletions src/libcollections/linked_list.rs
Expand Up @@ -871,6 +871,13 @@ impl<A> Extend<A> for LinkedList<A> {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T> {
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A: PartialEq> PartialEq for LinkedList<A> {
fn eq(&self, other: &LinkedList<A>) -> bool {
Expand Down
7 changes: 7 additions & 0 deletions src/libcollections/string.rs
Expand Up @@ -793,6 +793,13 @@ impl Extend<char> for String {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a> Extend<&'a char> for String {
fn extend<I: IntoIterator<Item=&'a char>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Extend<&'a str> for String {
fn extend<I: IntoIterator<Item=&'a str>>(&mut self, iterable: I) {
Expand Down
7 changes: 7 additions & 0 deletions src/libcollections/vec.rs
Expand Up @@ -1578,6 +1578,13 @@ impl<T> Extend<T> for Vec<T> {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}

__impl_slice_eq1! { Vec<A>, Vec<B> }
__impl_slice_eq1! { Vec<A>, &'b [B] }
__impl_slice_eq1! { Vec<A>, &'b mut [B] }
Expand Down
7 changes: 7 additions & 0 deletions src/libcollections/vec_deque.rs
Expand Up @@ -1785,6 +1785,13 @@ impl<A> Extend<A> for VecDeque<A> {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, T: 'a + Copy> Extend<&'a T> for VecDeque<T> {
fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: fmt::Debug> fmt::Debug for VecDeque<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
7 changes: 7 additions & 0 deletions src/libcollections/vec_map.rs
Expand Up @@ -828,6 +828,13 @@ impl<V> Extend<(usize, V)> for VecMap<V> {
}
}

#[stable(feature = "extend_ref", since = "1.2.0")]
impl<'a, V: Copy> Extend<(usize, &'a V)> for VecMap<V> {
fn extend<I: IntoIterator<Item=(usize, &'a V)>>(&mut self, iter: I) {
self.extend(iter.into_iter().map(|(key, &value)| (key, value)));
}
}

impl<V> Index<usize> for VecMap<V> {
type Output = V;

Expand Down
25 changes: 25 additions & 0 deletions src/libcollectionstest/binary_heap.rs
Expand Up @@ -217,3 +217,28 @@ fn test_drain() {

assert!(q.is_empty());
}

#[test]
fn test_extend_ref() {
let mut a = BinaryHeap::new();
a.push(1);
a.push(2);

a.extend(&[3, 4, 5]);

assert_eq!(a.len(), 5);
assert_eq!(a.into_sorted_vec(), [1, 2, 3, 4, 5]);

let mut a = BinaryHeap::new();
a.push(1);
a.push(2);
let mut b = BinaryHeap::new();
b.push(3);
b.push(4);
b.push(5);

a.extend(&b);

assert_eq!(a.len(), 5);
assert_eq!(a.into_sorted_vec(), [1, 2, 3, 4, 5]);
}
20 changes: 20 additions & 0 deletions src/libcollectionstest/bit/set.rs
Expand Up @@ -448,6 +448,26 @@ fn test_bit_set_split_off() {
0b00101011, 0b10101101])));
}

#[test]
fn test_bit_set_extend_ref() {
let mut a = BitSet::new();
a.insert(3);

a.extend(&[5, 7, 10]);

assert_eq!(a.len(), 4);
assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b00010101,0b00100000])));

let mut b = BitSet::new();
b.insert(11);
b.insert(15);

a.extend(&b);

assert_eq!(a.len(), 6);
assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b00010101,0b00110001])));
}

mod bench {
use std::collections::{BitSet, BitVec};
use std::__rand::{Rng, thread_rng, ThreadRng};
Expand Down
18 changes: 18 additions & 0 deletions src/libcollectionstest/bit/vec.rs
Expand Up @@ -630,6 +630,24 @@ fn test_bit_vec_extend() {
0b01001001, 0b10010010, 0b10111101]));
}

#[test]
fn test_bit_vecextend_ref() {
let mut bv = BitVec::from_bytes(&[0b10100011]);
bv.extend(&[true, false, true]);

assert_eq!(bv.len(), 11);
assert!(bv.eq_vec(&[true, false, true, false, false, false, true, true,
true, false, true]));

let bw = BitVec::from_bytes(&[0b00010001]);
bv.extend(&bw);

assert_eq!(bv.len(), 19);
assert!(bv.eq_vec(&[true, false, true, false, false, false, true, true,
true, false, true, false, false, false, true, false,
false, false, true]));
}

#[test]
fn test_bit_vec_append() {
// Append to BitVec that holds a multiple of u32::BITS bits
Expand Down
16 changes: 16 additions & 0 deletions src/libcollectionstest/btree/map.rs
Expand Up @@ -249,6 +249,22 @@ fn test_entry(){
assert_eq!(map.len(), 6);
}

#[test]
fn test_extend_ref() {
let mut a = BTreeMap::new();
a.insert(1, "one");
let mut b = BTreeMap::new();
b.insert(2, "two");
b.insert(3, "three");

a.extend(&b);

assert_eq!(a.len(), 3);
assert_eq!(a[&1], "one");
assert_eq!(a[&2], "two");
assert_eq!(a[&3], "three");
}

mod bench {
use std::collections::BTreeMap;
use std::__rand::{Rng, thread_rng};
Expand Down
28 changes: 28 additions & 0 deletions src/libcollectionstest/btree/set.rs
Expand Up @@ -184,3 +184,31 @@ fn test_show() {
assert_eq!(set_str, "{1, 2}");
assert_eq!(format!("{:?}", empty), "{}");
}

#[test]
fn test_extend_ref() {
let mut a = BTreeSet::new();
a.insert(1);

a.extend(&[2, 3, 4]);

assert_eq!(a.len(), 4);
assert!(a.contains(&1));
assert!(a.contains(&2));
assert!(a.contains(&3));
assert!(a.contains(&4));

let mut b = BTreeSet::new();
b.insert(5);
b.insert(6);

a.extend(&b);

assert_eq!(a.len(), 6);
assert!(a.contains(&1));
assert!(a.contains(&2));
assert!(a.contains(&3));
assert!(a.contains(&4));
assert!(a.contains(&5));
assert!(a.contains(&6));
}
22 changes: 22 additions & 0 deletions src/libcollectionstest/enum_set.rs
Expand Up @@ -242,3 +242,25 @@ fn test_overflow() {
let mut set = EnumSet::new();
set.insert(Bar::V64);
}

#[test]
fn test_extend_ref() {
let mut a = EnumSet::new();
a.insert(A);

a.extend(&[A, C]);

assert_eq!(a.len(), 2);
assert!(a.contains(&A));
assert!(a.contains(&C));

let mut b = EnumSet::new();
b.insert(B);

a.extend(&b);

assert_eq!(a.len(), 3);
assert!(a.contains(&A));
assert!(a.contains(&B));
assert!(a.contains(&C));
}
19 changes: 19 additions & 0 deletions src/libcollectionstest/linked_list.rs
Expand Up @@ -321,6 +321,25 @@ fn test_show() {
assert_eq!(format!("{:?}", list), "[\"just\", \"one\", \"test\", \"more\"]");
}

#[test]
fn test_extend_ref() {
let mut a = LinkedList::new();
a.push_back(1);

a.extend(&[2, 3, 4]);

assert_eq!(a.len(), 4);
assert_eq!(a, list_from(&[1, 2, 3, 4]));

let mut b = LinkedList::new();
b.push_back(5);
b.push_back(6);
a.extend(&b);

assert_eq!(a.len(), 6);
assert_eq!(a, list_from(&[1, 2, 3, 4, 5, 6]));
}

#[bench]
fn bench_collect_into(b: &mut test::Bencher) {
let v = &[0; 64];
Expand Down
8 changes: 8 additions & 0 deletions src/libcollectionstest/string.rs
Expand Up @@ -365,6 +365,14 @@ fn test_drain() {
assert_eq!(t, "");
}

#[test]
fn test_extend_ref() {
let mut a = "foo".to_string();
a.extend(&['b', 'a', 'r']);

assert_eq!(&a, "foobar");
}

#[bench]
fn bench_with_capacity(b: &mut Bencher) {
b.iter(|| {
Expand Down
15 changes: 15 additions & 0 deletions src/libcollectionstest/vec.rs
Expand Up @@ -112,6 +112,21 @@ fn test_extend() {
assert_eq!(v, w);
}

#[test]
fn test_extend_ref() {
let mut v = vec![1, 2];
v.extend(&[3, 4, 5]);

assert_eq!(v.len(), 5);
assert_eq!(v, [1, 2, 3, 4, 5]);

let w = vec![6, 7];
v.extend(&w);

assert_eq!(v.len(), 7);
assert_eq!(v, [1, 2, 3, 4, 5, 6, 7]);
}

#[test]
fn test_slice_from_mut() {
let mut values = vec![1, 2, 3, 4, 5];
Expand Down

0 comments on commit b36ed7d

Please sign in to comment.