Skip to content

Commit 5dc13b3

Browse files
committed
Vec - PartialEq where Rhs is the Vec
1 parent 1463915 commit 5dc13b3

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/vec.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,16 @@ where
10241024
}
10251025
}
10261026

1027+
// [B] == Vec<A, N>
1028+
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for [B]
1029+
where
1030+
A: PartialEq<B>,
1031+
{
1032+
fn eq(&self, other: &Vec<A, N>) -> bool {
1033+
<[A]>::eq(other, &self[..])
1034+
}
1035+
}
1036+
10271037
// Vec<A, N> == &[B]
10281038
impl<A, B, const N: usize> PartialEq<&[B]> for Vec<A, N>
10291039
where
@@ -1034,6 +1044,16 @@ where
10341044
}
10351045
}
10361046

1047+
// &[B] == Vec<A, N>
1048+
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for &[B]
1049+
where
1050+
A: PartialEq<B>,
1051+
{
1052+
fn eq(&self, other: &Vec<A, N>) -> bool {
1053+
<[A]>::eq(other, &self[..])
1054+
}
1055+
}
1056+
10371057
// Vec<A, N> == &mut [B]
10381058
impl<A, B, const N: usize> PartialEq<&mut [B]> for Vec<A, N>
10391059
where
@@ -1044,6 +1064,16 @@ where
10441064
}
10451065
}
10461066

1067+
// &mut [B] == Vec<A, N>
1068+
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for &mut [B]
1069+
where
1070+
A: PartialEq<B>,
1071+
{
1072+
fn eq(&self, other: &Vec<A, N>) -> bool {
1073+
<[A]>::eq(other, &self[..])
1074+
}
1075+
}
1076+
10471077
// Vec<A, N> == [B; M]
10481078
// Equality does not require equal capacity
10491079
impl<A, B, const N: usize, const M: usize> PartialEq<[B; M]> for Vec<A, N>
@@ -1055,6 +1085,17 @@ where
10551085
}
10561086
}
10571087

1088+
// [B; M] == Vec<A, N>
1089+
// Equality does not require equal capacity
1090+
impl<A, B, const N: usize, const M: usize> PartialEq<Vec<A, N>> for [B; M]
1091+
where
1092+
A: PartialEq<B>,
1093+
{
1094+
fn eq(&self, other: &Vec<A, N>) -> bool {
1095+
<[A]>::eq(other, &self[..])
1096+
}
1097+
}
1098+
10581099
// Vec<A, N> == &[B; M]
10591100
// Equality does not require equal capacity
10601101
impl<A, B, const N: usize, const M: usize> PartialEq<&[B; M]> for Vec<A, N>
@@ -1066,6 +1107,17 @@ where
10661107
}
10671108
}
10681109

1110+
// &[B; M] == Vec<A, N>
1111+
// Equality does not require equal capacity
1112+
impl<A, B, const N: usize, const M: usize> PartialEq<Vec<A, N>> for &[B; M]
1113+
where
1114+
A: PartialEq<B>,
1115+
{
1116+
fn eq(&self, other: &Vec<A, N>) -> bool {
1117+
<[A]>::eq(other, &self[..])
1118+
}
1119+
}
1120+
10691121
// Implements Eq if underlying data is Eq
10701122
impl<T, const N: usize> Eq for Vec<T, N> where T: Eq {}
10711123

@@ -1239,6 +1291,28 @@ mod tests {
12391291
assert!(xs < ys);
12401292
}
12411293

1294+
#[test]
1295+
fn cmp_with_arrays_and_slices() {
1296+
let mut xs: Vec<i32, 12> = Vec::new();
1297+
xs.push(1).unwrap();
1298+
1299+
let array = [1];
1300+
1301+
assert_eq!(xs, array);
1302+
assert_eq!(array, xs);
1303+
1304+
assert_eq!(xs, array.as_slice());
1305+
assert_eq!(array.as_slice(), xs);
1306+
1307+
assert_eq!(xs, &array);
1308+
assert_eq!(&array, xs);
1309+
1310+
let longer_array = [1; 20];
1311+
1312+
assert_ne!(xs, longer_array);
1313+
assert_ne!(longer_array, xs);
1314+
}
1315+
12421316
#[test]
12431317
fn full() {
12441318
let mut v: Vec<i32, 4> = Vec::new();

0 commit comments

Comments
 (0)