Skip to content

Commit cdf82c4

Browse files
committed
Auto merge of #38661 - frewsxcv:vec-deque-partial-eq, r=aturon
Implement `PartialEq<[A]>` for `VecDeque<A>`. Fixes #38625.
2 parents 24055d0 + b081872 commit cdf82c4

File tree

2 files changed

+93
-36
lines changed

2 files changed

+93
-36
lines changed

src/libcollections/vec_deque.rs

+74-36
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,9 @@ impl<T> VecDeque<T> {
469469
/// buf.push_back(3);
470470
/// buf.push_back(4);
471471
/// buf.push_back(5);
472+
/// assert_eq!(buf, [3, 4, 5]);
472473
/// buf.swap(0, 2);
473-
/// assert_eq!(buf[0], 5);
474-
/// assert_eq!(buf[2], 3);
474+
/// assert_eq!(buf, [5, 4, 3]);
475475
/// ```
476476
#[stable(feature = "rust1", since = "1.0.0")]
477477
pub fn swap(&mut self, i: usize, j: usize) {
@@ -649,9 +649,9 @@ impl<T> VecDeque<T> {
649649
/// buf.push_back(5);
650650
/// buf.push_back(10);
651651
/// buf.push_back(15);
652+
/// assert_eq!(buf, [5, 10, 15]);
652653
/// buf.truncate(1);
653-
/// assert_eq!(buf.len(), 1);
654-
/// assert_eq!(Some(&5), buf.get(0));
654+
/// assert_eq!(buf, [5]);
655655
/// ```
656656
#[stable(feature = "deque_extras", since = "1.16.0")]
657657
pub fn truncate(&mut self, len: usize) {
@@ -826,8 +826,9 @@ impl<T> VecDeque<T> {
826826
/// use std::collections::VecDeque;
827827
///
828828
/// let mut v: VecDeque<_> = vec![1, 2, 3].into_iter().collect();
829-
/// assert_eq!(vec![3].into_iter().collect::<VecDeque<_>>(), v.drain(2..).collect());
830-
/// assert_eq!(vec![1, 2].into_iter().collect::<VecDeque<_>>(), v);
829+
/// let drained = v.drain(2..).collect::<VecDeque<_>>();
830+
/// assert_eq!(drained, [3]);
831+
/// assert_eq!(v, [1, 2]);
831832
///
832833
/// // A full range clears all contents
833834
/// v.drain(..);
@@ -1179,11 +1180,10 @@ impl<T> VecDeque<T> {
11791180
/// buf.push_back(1);
11801181
/// buf.push_back(2);
11811182
/// buf.push_back(3);
1183+
/// assert_eq!(buf, [1, 2, 3]);
11821184
///
11831185
/// assert_eq!(buf.swap_remove_back(0), Some(1));
1184-
/// assert_eq!(buf.len(), 2);
1185-
/// assert_eq!(buf[0], 3);
1186-
/// assert_eq!(buf[1], 2);
1186+
/// assert_eq!(buf, [3, 2]);
11871187
/// ```
11881188
#[stable(feature = "deque_extras_15", since = "1.5.0")]
11891189
pub fn swap_remove_back(&mut self, index: usize) -> Option<T> {
@@ -1215,11 +1215,10 @@ impl<T> VecDeque<T> {
12151215
/// buf.push_back(1);
12161216
/// buf.push_back(2);
12171217
/// buf.push_back(3);
1218+
/// assert_eq!(buf, [1, 2, 3]);
12181219
///
12191220
/// assert_eq!(buf.swap_remove_front(2), Some(3));
1220-
/// assert_eq!(buf.len(), 2);
1221-
/// assert_eq!(buf[0], 2);
1222-
/// assert_eq!(buf[1], 1);
1221+
/// assert_eq!(buf, [2, 1]);
12231222
/// ```
12241223
#[stable(feature = "deque_extras_15", since = "1.5.0")]
12251224
pub fn swap_remove_front(&mut self, index: usize) -> Option<T> {
@@ -1250,11 +1249,10 @@ impl<T> VecDeque<T> {
12501249
/// vec_deque.push_back('a');
12511250
/// vec_deque.push_back('b');
12521251
/// vec_deque.push_back('c');
1252+
/// assert_eq!(vec_deque, &['a', 'b', 'c']);
12531253
///
12541254
/// vec_deque.insert(1, 'd');
1255-
///
1256-
/// let vec = vec_deque.into_iter().collect::<Vec<_>>();
1257-
/// assert_eq!(vec, ['a', 'd', 'b', 'c']);
1255+
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c']);
12581256
/// ```
12591257
#[stable(feature = "deque_extras_15", since = "1.5.0")]
12601258
pub fn insert(&mut self, index: usize, value: T) {
@@ -1478,9 +1476,10 @@ impl<T> VecDeque<T> {
14781476
/// buf.push_back(1);
14791477
/// buf.push_back(2);
14801478
/// buf.push_back(3);
1479+
/// assert_eq!(buf, [1, 2, 3]);
14811480
///
14821481
/// assert_eq!(buf.remove(1), Some(2));
1483-
/// assert_eq!(buf.get(1), Some(&3));
1482+
/// assert_eq!(buf, [1, 3]);
14841483
/// ```
14851484
#[stable(feature = "rust1", since = "1.0.0")]
14861485
pub fn remove(&mut self, index: usize) -> Option<T> {
@@ -1659,9 +1658,8 @@ impl<T> VecDeque<T> {
16591658
///
16601659
/// let mut buf: VecDeque<_> = vec![1,2,3].into_iter().collect();
16611660
/// let buf2 = buf.split_off(1);
1662-
/// // buf = [1], buf2 = [2, 3]
1663-
/// assert_eq!(buf.len(), 1);
1664-
/// assert_eq!(buf2.len(), 2);
1661+
/// assert_eq!(buf, [1]);
1662+
/// assert_eq!(buf2, [2, 3]);
16651663
/// ```
16661664
#[inline]
16671665
#[stable(feature = "split_off", since = "1.4.0")]
@@ -1718,11 +1716,11 @@ impl<T> VecDeque<T> {
17181716
/// ```
17191717
/// use std::collections::VecDeque;
17201718
///
1721-
/// let mut buf: VecDeque<_> = vec![1, 2, 3].into_iter().collect();
1722-
/// let mut buf2: VecDeque<_> = vec![4, 5, 6].into_iter().collect();
1719+
/// let mut buf: VecDeque<_> = vec![1, 2].into_iter().collect();
1720+
/// let mut buf2: VecDeque<_> = vec![3, 4].into_iter().collect();
17231721
/// buf.append(&mut buf2);
1724-
/// assert_eq!(buf.len(), 6);
1725-
/// assert_eq!(buf2.len(), 0);
1722+
/// assert_eq!(buf, [1, 2, 3, 4]);
1723+
/// assert_eq!(buf2, []);
17261724
/// ```
17271725
#[inline]
17281726
#[stable(feature = "append", since = "1.4.0")]
@@ -1745,9 +1743,7 @@ impl<T> VecDeque<T> {
17451743
/// let mut buf = VecDeque::new();
17461744
/// buf.extend(1..5);
17471745
/// buf.retain(|&x| x%2 == 0);
1748-
///
1749-
/// let v: Vec<_> = buf.into_iter().collect();
1750-
/// assert_eq!(&v[..], &[2, 4]);
1746+
/// assert_eq!(buf, [2, 4]);
17511747
/// ```
17521748
#[stable(feature = "vec_deque_retain", since = "1.4.0")]
17531749
pub fn retain<F>(&mut self, mut f: F)
@@ -1781,11 +1777,13 @@ impl<T: Clone> VecDeque<T> {
17811777
/// buf.push_back(5);
17821778
/// buf.push_back(10);
17831779
/// buf.push_back(15);
1780+
/// assert_eq!(buf, [5, 10, 15]);
1781+
///
17841782
/// buf.resize(2, 0);
1785-
/// buf.resize(6, 20);
1786-
/// for (a, b) in [5, 10, 20, 20, 20, 20].iter().zip(&buf) {
1787-
/// assert_eq!(a, b);
1788-
/// }
1783+
/// assert_eq!(buf, [5, 10]);
1784+
///
1785+
/// buf.resize(5, 20);
1786+
/// assert_eq!(buf, [5, 10, 20, 20, 20]);
17891787
/// ```
17901788
#[stable(feature = "deque_extras", since = "1.16.0")]
17911789
pub fn resize(&mut self, new_len: usize, value: T) {
@@ -2162,6 +2160,46 @@ impl<A: PartialEq> PartialEq for VecDeque<A> {
21622160
#[stable(feature = "rust1", since = "1.0.0")]
21632161
impl<A: Eq> Eq for VecDeque<A> {}
21642162

2163+
macro_rules! __impl_slice_eq1 {
2164+
($Lhs: ty, $Rhs: ty) => {
2165+
__impl_slice_eq1! { $Lhs, $Rhs, Sized }
2166+
};
2167+
($Lhs: ty, $Rhs: ty, $Bound: ident) => {
2168+
#[stable(feature = "vec-deque-partial-eq-slice", since = "1.16.0")]
2169+
impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
2170+
fn eq(&self, other: &$Rhs) -> bool {
2171+
if self.len() != other.len() {
2172+
return false;
2173+
}
2174+
let (sa, sb) = self.as_slices();
2175+
let (oa, ob) = other[..].split_at(sa.len());
2176+
sa == oa && sb == ob
2177+
}
2178+
}
2179+
}
2180+
}
2181+
2182+
__impl_slice_eq1! { VecDeque<A>, Vec<B> }
2183+
__impl_slice_eq1! { VecDeque<A>, &'b [B] }
2184+
__impl_slice_eq1! { VecDeque<A>, &'b mut [B] }
2185+
2186+
macro_rules! array_impls {
2187+
($($N: expr)+) => {
2188+
$(
2189+
__impl_slice_eq1! { VecDeque<A>, [B; $N] }
2190+
__impl_slice_eq1! { VecDeque<A>, &'b [B; $N] }
2191+
__impl_slice_eq1! { VecDeque<A>, &'b mut [B; $N] }
2192+
)+
2193+
}
2194+
}
2195+
2196+
array_impls! {
2197+
0 1 2 3 4 5 6 7 8 9
2198+
10 11 12 13 14 15 16 17 18 19
2199+
20 21 22 23 24 25 26 27 28 29
2200+
30 31 32
2201+
}
2202+
21652203
#[stable(feature = "rust1", since = "1.0.0")]
21662204
impl<A: PartialOrd> PartialOrd for VecDeque<A> {
21672205
fn partial_cmp(&self, other: &VecDeque<A>) -> Option<Ordering> {
@@ -2434,7 +2472,7 @@ mod tests {
24342472
let final_len = usable_cap / 2;
24352473

24362474
for len in 0..final_len {
2437-
let expected = if back {
2475+
let expected: VecDeque<_> = if back {
24382476
(0..len).collect()
24392477
} else {
24402478
(0..len).rev().collect()
@@ -2483,7 +2521,7 @@ mod tests {
24832521
// len is the length *after* insertion
24842522
for len in 1..cap {
24852523
// 0, 1, 2, .., len - 1
2486-
let expected = (0..).take(len).collect();
2524+
let expected = (0..).take(len).collect::<VecDeque<_>>();
24872525
for tail_pos in 0..cap {
24882526
for to_insert in 0..len {
24892527
tester.tail = tail_pos;
@@ -2516,7 +2554,7 @@ mod tests {
25162554
// len is the length *after* removal
25172555
for len in 0..cap - 1 {
25182556
// 0, 1, 2, .., len - 1
2519-
let expected = (0..).take(len).collect();
2557+
let expected = (0..).take(len).collect::<VecDeque<_>>();
25202558
for tail_pos in 0..cap {
25212559
for to_remove in 0..len + 1 {
25222560
tester.tail = tail_pos;
@@ -2591,7 +2629,7 @@ mod tests {
25912629

25922630
for len in 0..cap + 1 {
25932631
// 0, 1, 2, .., len - 1
2594-
let expected = (0..).take(len).collect();
2632+
let expected = (0..).take(len).collect::<VecDeque<_>>();
25952633
for tail_pos in 0..max_cap + 1 {
25962634
tester.tail = tail_pos;
25972635
tester.head = tail_pos;
@@ -2624,9 +2662,9 @@ mod tests {
26242662
// index to split at
26252663
for at in 0..len + 1 {
26262664
// 0, 1, 2, .., at - 1 (may be empty)
2627-
let expected_self = (0..).take(at).collect();
2665+
let expected_self = (0..).take(at).collect::<VecDeque<_>>();
26282666
// at, at + 1, .., len - 1 (may be empty)
2629-
let expected_other = (at..).take(len - at).collect();
2667+
let expected_other = (at..).take(len - at).collect::<VecDeque<_>>();
26302668

26312669
for tail_pos in 0..cap {
26322670
tester.tail = tail_pos;

src/libcollectionstest/vec_deque.rs

+19
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,25 @@ fn test_eq() {
603603
assert!(e == VecDeque::new());
604604
}
605605

606+
#[test]
607+
fn test_partial_eq_array() {
608+
let d = VecDeque::<char>::new();
609+
assert!(d == []);
610+
611+
let mut d = VecDeque::new();
612+
d.push_front('a');
613+
assert!(d == ['a']);
614+
615+
let mut d = VecDeque::new();
616+
d.push_back('a');
617+
assert!(d == ['a']);
618+
619+
let mut d = VecDeque::new();
620+
d.push_back('a');
621+
d.push_back('b');
622+
assert!(d == ['a', 'b']);
623+
}
624+
606625
#[test]
607626
fn test_hash() {
608627
let mut x = VecDeque::new();

0 commit comments

Comments
 (0)