From b081872d5bf19a063fbf7f2f315560a59592308d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Wed, 28 Dec 2016 12:16:19 -0800 Subject: [PATCH] Implement `PartialEq<&[A]>` for `VecDeque`. Fixes https://github.com/rust-lang/rust/issues/38625. --- src/libcollections/vec_deque.rs | 110 +++++++++++++++++++--------- src/libcollectionstest/vec_deque.rs | 19 +++++ 2 files changed, 93 insertions(+), 36 deletions(-) diff --git a/src/libcollections/vec_deque.rs b/src/libcollections/vec_deque.rs index 5b1bc3a3ae4..5e1adb3d808 100644 --- a/src/libcollections/vec_deque.rs +++ b/src/libcollections/vec_deque.rs @@ -469,9 +469,9 @@ impl VecDeque { /// buf.push_back(3); /// buf.push_back(4); /// buf.push_back(5); + /// assert_eq!(buf, [3, 4, 5]); /// buf.swap(0, 2); - /// assert_eq!(buf[0], 5); - /// assert_eq!(buf[2], 3); + /// assert_eq!(buf, [5, 4, 3]); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn swap(&mut self, i: usize, j: usize) { @@ -649,9 +649,9 @@ impl VecDeque { /// buf.push_back(5); /// buf.push_back(10); /// buf.push_back(15); + /// assert_eq!(buf, [5, 10, 15]); /// buf.truncate(1); - /// assert_eq!(buf.len(), 1); - /// assert_eq!(Some(&5), buf.get(0)); + /// assert_eq!(buf, [5]); /// ``` #[stable(feature = "deque_extras", since = "1.16.0")] pub fn truncate(&mut self, len: usize) { @@ -826,8 +826,9 @@ impl VecDeque { /// use std::collections::VecDeque; /// /// let mut v: VecDeque<_> = vec![1, 2, 3].into_iter().collect(); - /// assert_eq!(vec![3].into_iter().collect::>(), v.drain(2..).collect()); - /// assert_eq!(vec![1, 2].into_iter().collect::>(), v); + /// let drained = v.drain(2..).collect::>(); + /// assert_eq!(drained, [3]); + /// assert_eq!(v, [1, 2]); /// /// // A full range clears all contents /// v.drain(..); @@ -1179,11 +1180,10 @@ impl VecDeque { /// buf.push_back(1); /// buf.push_back(2); /// buf.push_back(3); + /// assert_eq!(buf, [1, 2, 3]); /// /// assert_eq!(buf.swap_remove_back(0), Some(1)); - /// assert_eq!(buf.len(), 2); - /// assert_eq!(buf[0], 3); - /// assert_eq!(buf[1], 2); + /// assert_eq!(buf, [3, 2]); /// ``` #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn swap_remove_back(&mut self, index: usize) -> Option { @@ -1215,11 +1215,10 @@ impl VecDeque { /// buf.push_back(1); /// buf.push_back(2); /// buf.push_back(3); + /// assert_eq!(buf, [1, 2, 3]); /// /// assert_eq!(buf.swap_remove_front(2), Some(3)); - /// assert_eq!(buf.len(), 2); - /// assert_eq!(buf[0], 2); - /// assert_eq!(buf[1], 1); + /// assert_eq!(buf, [2, 1]); /// ``` #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn swap_remove_front(&mut self, index: usize) -> Option { @@ -1250,11 +1249,10 @@ impl VecDeque { /// vec_deque.push_back('a'); /// vec_deque.push_back('b'); /// vec_deque.push_back('c'); + /// assert_eq!(vec_deque, &['a', 'b', 'c']); /// /// vec_deque.insert(1, 'd'); - /// - /// let vec = vec_deque.into_iter().collect::>(); - /// assert_eq!(vec, ['a', 'd', 'b', 'c']); + /// assert_eq!(vec_deque, &['a', 'd', 'b', 'c']); /// ``` #[stable(feature = "deque_extras_15", since = "1.5.0")] pub fn insert(&mut self, index: usize, value: T) { @@ -1478,9 +1476,10 @@ impl VecDeque { /// buf.push_back(1); /// buf.push_back(2); /// buf.push_back(3); + /// assert_eq!(buf, [1, 2, 3]); /// /// assert_eq!(buf.remove(1), Some(2)); - /// assert_eq!(buf.get(1), Some(&3)); + /// assert_eq!(buf, [1, 3]); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn remove(&mut self, index: usize) -> Option { @@ -1659,9 +1658,8 @@ impl VecDeque { /// /// let mut buf: VecDeque<_> = vec![1,2,3].into_iter().collect(); /// let buf2 = buf.split_off(1); - /// // buf = [1], buf2 = [2, 3] - /// assert_eq!(buf.len(), 1); - /// assert_eq!(buf2.len(), 2); + /// assert_eq!(buf, [1]); + /// assert_eq!(buf2, [2, 3]); /// ``` #[inline] #[stable(feature = "split_off", since = "1.4.0")] @@ -1718,11 +1716,11 @@ impl VecDeque { /// ``` /// use std::collections::VecDeque; /// - /// let mut buf: VecDeque<_> = vec![1, 2, 3].into_iter().collect(); - /// let mut buf2: VecDeque<_> = vec![4, 5, 6].into_iter().collect(); + /// let mut buf: VecDeque<_> = vec![1, 2].into_iter().collect(); + /// let mut buf2: VecDeque<_> = vec![3, 4].into_iter().collect(); /// buf.append(&mut buf2); - /// assert_eq!(buf.len(), 6); - /// assert_eq!(buf2.len(), 0); + /// assert_eq!(buf, [1, 2, 3, 4]); + /// assert_eq!(buf2, []); /// ``` #[inline] #[stable(feature = "append", since = "1.4.0")] @@ -1745,9 +1743,7 @@ impl VecDeque { /// let mut buf = VecDeque::new(); /// buf.extend(1..5); /// buf.retain(|&x| x%2 == 0); - /// - /// let v: Vec<_> = buf.into_iter().collect(); - /// assert_eq!(&v[..], &[2, 4]); + /// assert_eq!(buf, [2, 4]); /// ``` #[stable(feature = "vec_deque_retain", since = "1.4.0")] pub fn retain(&mut self, mut f: F) @@ -1781,11 +1777,13 @@ impl VecDeque { /// buf.push_back(5); /// buf.push_back(10); /// buf.push_back(15); + /// assert_eq!(buf, [5, 10, 15]); + /// /// buf.resize(2, 0); - /// buf.resize(6, 20); - /// for (a, b) in [5, 10, 20, 20, 20, 20].iter().zip(&buf) { - /// assert_eq!(a, b); - /// } + /// assert_eq!(buf, [5, 10]); + /// + /// buf.resize(5, 20); + /// assert_eq!(buf, [5, 10, 20, 20, 20]); /// ``` #[stable(feature = "deque_extras", since = "1.16.0")] pub fn resize(&mut self, new_len: usize, value: T) { @@ -2162,6 +2160,46 @@ impl PartialEq for VecDeque { #[stable(feature = "rust1", since = "1.0.0")] impl Eq for VecDeque {} +macro_rules! __impl_slice_eq1 { + ($Lhs: ty, $Rhs: ty) => { + __impl_slice_eq1! { $Lhs, $Rhs, Sized } + }; + ($Lhs: ty, $Rhs: ty, $Bound: ident) => { + #[stable(feature = "vec-deque-partial-eq-slice", since = "1.16.0")] + impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq { + fn eq(&self, other: &$Rhs) -> bool { + if self.len() != other.len() { + return false; + } + let (sa, sb) = self.as_slices(); + let (oa, ob) = other[..].split_at(sa.len()); + sa == oa && sb == ob + } + } + } +} + +__impl_slice_eq1! { VecDeque, Vec } +__impl_slice_eq1! { VecDeque, &'b [B] } +__impl_slice_eq1! { VecDeque, &'b mut [B] } + +macro_rules! array_impls { + ($($N: expr)+) => { + $( + __impl_slice_eq1! { VecDeque, [B; $N] } + __impl_slice_eq1! { VecDeque, &'b [B; $N] } + __impl_slice_eq1! { VecDeque, &'b mut [B; $N] } + )+ + } +} + +array_impls! { + 0 1 2 3 4 5 6 7 8 9 + 10 11 12 13 14 15 16 17 18 19 + 20 21 22 23 24 25 26 27 28 29 + 30 31 32 +} + #[stable(feature = "rust1", since = "1.0.0")] impl PartialOrd for VecDeque { fn partial_cmp(&self, other: &VecDeque) -> Option { @@ -2434,7 +2472,7 @@ mod tests { let final_len = usable_cap / 2; for len in 0..final_len { - let expected = if back { + let expected: VecDeque<_> = if back { (0..len).collect() } else { (0..len).rev().collect() @@ -2483,7 +2521,7 @@ mod tests { // len is the length *after* insertion for len in 1..cap { // 0, 1, 2, .., len - 1 - let expected = (0..).take(len).collect(); + let expected = (0..).take(len).collect::>(); for tail_pos in 0..cap { for to_insert in 0..len { tester.tail = tail_pos; @@ -2516,7 +2554,7 @@ mod tests { // len is the length *after* removal for len in 0..cap - 1 { // 0, 1, 2, .., len - 1 - let expected = (0..).take(len).collect(); + let expected = (0..).take(len).collect::>(); for tail_pos in 0..cap { for to_remove in 0..len + 1 { tester.tail = tail_pos; @@ -2591,7 +2629,7 @@ mod tests { for len in 0..cap + 1 { // 0, 1, 2, .., len - 1 - let expected = (0..).take(len).collect(); + let expected = (0..).take(len).collect::>(); for tail_pos in 0..max_cap + 1 { tester.tail = tail_pos; tester.head = tail_pos; @@ -2624,9 +2662,9 @@ mod tests { // index to split at for at in 0..len + 1 { // 0, 1, 2, .., at - 1 (may be empty) - let expected_self = (0..).take(at).collect(); + let expected_self = (0..).take(at).collect::>(); // at, at + 1, .., len - 1 (may be empty) - let expected_other = (at..).take(len - at).collect(); + let expected_other = (at..).take(len - at).collect::>(); for tail_pos in 0..cap { tester.tail = tail_pos; diff --git a/src/libcollectionstest/vec_deque.rs b/src/libcollectionstest/vec_deque.rs index cdf022e4f02..bb60f888f8b 100644 --- a/src/libcollectionstest/vec_deque.rs +++ b/src/libcollectionstest/vec_deque.rs @@ -603,6 +603,25 @@ fn test_eq() { assert!(e == VecDeque::new()); } +#[test] +fn test_partial_eq_array() { + let d = VecDeque::::new(); + assert!(d == []); + + let mut d = VecDeque::new(); + d.push_front('a'); + assert!(d == ['a']); + + let mut d = VecDeque::new(); + d.push_back('a'); + assert!(d == ['a']); + + let mut d = VecDeque::new(); + d.push_back('a'); + d.push_back('b'); + assert!(d == ['a', 'b']); +} + #[test] fn test_hash() { let mut x = VecDeque::new();