Skip to content

Commit 71e7bd2

Browse files
authored
Rollup merge of rust-lang#71660 - sollyucko:master, r=dtolnay
impl PartialEq<Vec<B>> for &[A], &mut [A] rust-lang/rfcs#2917
2 parents 1a4e2b6 + 4896a06 commit 71e7bd2

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

src/liballoc/tests/vec.rs

+54
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22
use std::collections::TryReserveError::*;
3+
use std::fmt::Debug;
34
use std::mem::size_of;
45
use std::panic::{catch_unwind, AssertUnwindSafe};
56
use std::vec::{Drain, IntoIter};
@@ -1573,3 +1574,56 @@ fn test_push_growth_strategy() {
15731574
}
15741575
}
15751576
}
1577+
1578+
macro_rules! generate_assert_eq_vec_and_prim {
1579+
($name:ident<$B:ident>($type:ty)) => {
1580+
fn $name<A: PartialEq<$B> + Debug, $B: Debug>(a: Vec<A>, b: $type) {
1581+
assert!(a == b);
1582+
assert_eq!(a, b);
1583+
}
1584+
};
1585+
}
1586+
1587+
generate_assert_eq_vec_and_prim! { assert_eq_vec_and_slice <B>(&[B]) }
1588+
generate_assert_eq_vec_and_prim! { assert_eq_vec_and_array_3<B>([B; 3]) }
1589+
1590+
#[test]
1591+
fn partialeq_vec_and_prim() {
1592+
assert_eq_vec_and_slice(vec![1, 2, 3], &[1, 2, 3]);
1593+
assert_eq_vec_and_array_3(vec![1, 2, 3], [1, 2, 3]);
1594+
}
1595+
1596+
macro_rules! assert_partial_eq_valid {
1597+
($a2:ident, $a3:ident; $b2:ident, $b3: ident) => {
1598+
assert!($a2 == $b2);
1599+
assert!($a2 != $b3);
1600+
assert!($a3 != $b2);
1601+
assert!($a3 == $b3);
1602+
assert_eq!($a2, $b2);
1603+
assert_ne!($a2, $b3);
1604+
assert_ne!($a3, $b2);
1605+
assert_eq!($a3, $b3);
1606+
};
1607+
}
1608+
1609+
#[test]
1610+
fn partialeq_vec_full() {
1611+
let vec2: Vec<_> = vec![1, 2];
1612+
let vec3: Vec<_> = vec![1, 2, 3];
1613+
let slice2: &[_] = &[1, 2];
1614+
let slice3: &[_] = &[1, 2, 3];
1615+
let slicemut2: &[_] = &mut [1, 2];
1616+
let slicemut3: &[_] = &mut [1, 2, 3];
1617+
let array2: [_; 2] = [1, 2];
1618+
let array3: [_; 3] = [1, 2, 3];
1619+
let arrayref2: &[_; 2] = &[1, 2];
1620+
let arrayref3: &[_; 3] = &[1, 2, 3];
1621+
1622+
assert_partial_eq_valid!(vec2,vec3; vec2,vec3);
1623+
assert_partial_eq_valid!(vec2,vec3; slice2,slice3);
1624+
assert_partial_eq_valid!(vec2,vec3; slicemut2,slicemut3);
1625+
assert_partial_eq_valid!(slice2,slice3; vec2,vec3);
1626+
assert_partial_eq_valid!(slicemut2,slicemut3; vec2,vec3);
1627+
assert_partial_eq_valid!(vec2,vec3; array2,array3);
1628+
assert_partial_eq_valid!(vec2,vec3; arrayref2,arrayref3);
1629+
}

src/liballoc/vec.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -2342,12 +2342,12 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {
23422342
}
23432343

23442344
macro_rules! __impl_slice_eq1 {
2345-
([$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => {
2346-
#[stable(feature = "rust1", since = "1.0.0")]
2345+
([$($vars:tt)*] $lhs:ty, $rhs:ty $(where $ty:ty: $bound:ident)?, #[$stability:meta]) => {
2346+
#[$stability]
23472347
impl<A, B, $($vars)*> PartialEq<$rhs> for $lhs
23482348
where
23492349
A: PartialEq<B>,
2350-
$($constraints)*
2350+
$($ty: $bound)?
23512351
{
23522352
#[inline]
23532353
fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] }
@@ -2357,18 +2357,23 @@ macro_rules! __impl_slice_eq1 {
23572357
}
23582358
}
23592359

2360-
__impl_slice_eq1! { [] Vec<A>, Vec<B>, }
2361-
__impl_slice_eq1! { [] Vec<A>, &[B], }
2362-
__impl_slice_eq1! { [] Vec<A>, &mut [B], }
2363-
__impl_slice_eq1! { [] Cow<'_, [A]>, Vec<B>, A: Clone }
2364-
__impl_slice_eq1! { [] Cow<'_, [A]>, &[B], A: Clone }
2365-
__impl_slice_eq1! { [] Cow<'_, [A]>, &mut [B], A: Clone }
2366-
__impl_slice_eq1! { [const N: usize] Vec<A>, [B; N], [B; N]: LengthAtMost32 }
2367-
__impl_slice_eq1! { [const N: usize] Vec<A>, &[B; N], [B; N]: LengthAtMost32 }
2360+
__impl_slice_eq1! { [] Vec<A>, Vec<B>, #[stable(feature = "rust1", since = "1.0.0")] }
2361+
__impl_slice_eq1! { [] Vec<A>, &[B], #[stable(feature = "rust1", since = "1.0.0")] }
2362+
__impl_slice_eq1! { [] Vec<A>, &mut [B], #[stable(feature = "rust1", since = "1.0.0")] }
2363+
__impl_slice_eq1! { [] &[A], Vec<B>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")] }
2364+
__impl_slice_eq1! { [] &mut [A], Vec<B>, #[stable(feature = "partialeq_vec_for_ref_slice", since = "1.46.0")] }
2365+
__impl_slice_eq1! { [] Cow<'_, [A]>, Vec<B> where A: Clone, #[stable(feature = "rust1", since = "1.0.0")] }
2366+
__impl_slice_eq1! { [] Cow<'_, [A]>, &[B] where A: Clone, #[stable(feature = "rust1", since = "1.0.0")] }
2367+
__impl_slice_eq1! { [] Cow<'_, [A]>, &mut [B] where A: Clone, #[stable(feature = "rust1", since = "1.0.0")] }
2368+
__impl_slice_eq1! { [const N: usize] Vec<A>, [B; N] where [B; N]: LengthAtMost32, #[stable(feature = "rust1", since = "1.0.0")] }
2369+
__impl_slice_eq1! { [const N: usize] Vec<A>, &[B; N] where [B; N]: LengthAtMost32, #[stable(feature = "rust1", since = "1.0.0")] }
23682370

23692371
// NOTE: some less important impls are omitted to reduce code bloat
23702372
// FIXME(Centril): Reconsider this?
23712373
//__impl_slice_eq1! { [const N: usize] Vec<A>, &mut [B; N], [B; N]: LengthAtMost32 }
2374+
//__impl_slice_eq1! { [const N: usize] [A; N], Vec<B>, [A; N]: LengthAtMost32 }
2375+
//__impl_slice_eq1! { [const N: usize] &[A; N], Vec<B>, [A; N]: LengthAtMost32 }
2376+
//__impl_slice_eq1! { [const N: usize] &mut [A; N], Vec<B>, [A; N]: LengthAtMost32 }
23722377
//__impl_slice_eq1! { [const N: usize] Cow<'a, [A]>, [B; N], [B; N]: LengthAtMost32 }
23732378
//__impl_slice_eq1! { [const N: usize] Cow<'a, [A]>, &[B; N], [B; N]: LengthAtMost32 }
23742379
//__impl_slice_eq1! { [const N: usize] Cow<'a, [A]>, &mut [B; N], [B; N]: LengthAtMost32 }

0 commit comments

Comments
 (0)