mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-27 04:20:24 +00:00
Merge #303
303: Vec - PartialEq where Rhs is the Vec r=japaric a=elpiel I was using the Vec for tests and initially, I was confused because I saw the impl for PartialEq but then I realized that PartialEq with Rhs of Vec was not implemented. These impls are not mandatory but might help other people when using Vec on Rhs. PS: Amazing crate btw, thanks you for the awesome work! Co-authored-by: Lachezar Lechev <elpiel93@gmail.com> Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
This commit is contained in:
commit
c333a6c3b6
74
src/vec.rs
74
src/vec.rs
@ -1024,6 +1024,16 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// [B] == Vec<A, N>
|
||||
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for [B]
|
||||
where
|
||||
A: PartialEq<B>,
|
||||
{
|
||||
fn eq(&self, other: &Vec<A, N>) -> bool {
|
||||
<[A]>::eq(other, &self[..])
|
||||
}
|
||||
}
|
||||
|
||||
// Vec<A, N> == &[B]
|
||||
impl<A, B, const N: usize> PartialEq<&[B]> for Vec<A, N>
|
||||
where
|
||||
@ -1034,6 +1044,16 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// &[B] == Vec<A, N>
|
||||
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for &[B]
|
||||
where
|
||||
A: PartialEq<B>,
|
||||
{
|
||||
fn eq(&self, other: &Vec<A, N>) -> bool {
|
||||
<[A]>::eq(other, &self[..])
|
||||
}
|
||||
}
|
||||
|
||||
// Vec<A, N> == &mut [B]
|
||||
impl<A, B, const N: usize> PartialEq<&mut [B]> for Vec<A, N>
|
||||
where
|
||||
@ -1044,6 +1064,16 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// &mut [B] == Vec<A, N>
|
||||
impl<A, B, const N: usize> PartialEq<Vec<A, N>> for &mut [B]
|
||||
where
|
||||
A: PartialEq<B>,
|
||||
{
|
||||
fn eq(&self, other: &Vec<A, N>) -> bool {
|
||||
<[A]>::eq(other, &self[..])
|
||||
}
|
||||
}
|
||||
|
||||
// Vec<A, N> == [B; M]
|
||||
// Equality does not require equal capacity
|
||||
impl<A, B, const N: usize, const M: usize> PartialEq<[B; M]> for Vec<A, N>
|
||||
@ -1055,6 +1085,17 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// [B; M] == Vec<A, N>
|
||||
// Equality does not require equal capacity
|
||||
impl<A, B, const N: usize, const M: usize> PartialEq<Vec<A, N>> for [B; M]
|
||||
where
|
||||
A: PartialEq<B>,
|
||||
{
|
||||
fn eq(&self, other: &Vec<A, N>) -> bool {
|
||||
<[A]>::eq(other, &self[..])
|
||||
}
|
||||
}
|
||||
|
||||
// Vec<A, N> == &[B; M]
|
||||
// Equality does not require equal capacity
|
||||
impl<A, B, const N: usize, const M: usize> PartialEq<&[B; M]> for Vec<A, N>
|
||||
@ -1066,6 +1107,17 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// &[B; M] == Vec<A, N>
|
||||
// Equality does not require equal capacity
|
||||
impl<A, B, const N: usize, const M: usize> PartialEq<Vec<A, N>> for &[B; M]
|
||||
where
|
||||
A: PartialEq<B>,
|
||||
{
|
||||
fn eq(&self, other: &Vec<A, N>) -> bool {
|
||||
<[A]>::eq(other, &self[..])
|
||||
}
|
||||
}
|
||||
|
||||
// Implements Eq if underlying data is Eq
|
||||
impl<T, const N: usize> Eq for Vec<T, N> where T: Eq {}
|
||||
|
||||
@ -1239,6 +1291,28 @@ mod tests {
|
||||
assert!(xs < ys);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cmp_with_arrays_and_slices() {
|
||||
let mut xs: Vec<i32, 12> = Vec::new();
|
||||
xs.push(1).unwrap();
|
||||
|
||||
let array = [1];
|
||||
|
||||
assert_eq!(xs, array);
|
||||
assert_eq!(array, xs);
|
||||
|
||||
assert_eq!(xs, array.as_slice());
|
||||
assert_eq!(array.as_slice(), xs);
|
||||
|
||||
assert_eq!(xs, &array);
|
||||
assert_eq!(&array, xs);
|
||||
|
||||
let longer_array = [1; 20];
|
||||
|
||||
assert_ne!(xs, longer_array);
|
||||
assert_ne!(longer_array, xs);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full() {
|
||||
let mut v: Vec<i32, 4> = Vec::new();
|
||||
|
Loading…
x
Reference in New Issue
Block a user