how much PartialEq would you like? yes

This commit is contained in:
missing 2022-07-07 21:18:47 -07:00 committed by missing
parent e34e33e5af
commit dfaf41808f
2 changed files with 64 additions and 8 deletions

View file

@ -1,4 +1,4 @@
//! Implements `Debug`, and `PartialEq` for various list-like types.
//! Implements `Debug`, `Default`, `Extend`, and `Deref`. Also implements `PartialEq` for various list-like types.
use crate::{Vec, Extra};
@ -30,9 +30,9 @@ impl<T: ?Sized + PartialEq<U>, U: ?Sized> PartialEq<Vec<U>> for Vec<T> {
impl<T: ?Sized + Eq> Eq for Vec<T> {}
// Vec<T> == &[U]
impl<T: PartialEq<U>, U> PartialEq<&[U]> for Vec<T> {
fn eq(&self, other: &&[U]) -> bool {
// Vec<T> == [U]
impl<T: PartialEq<U>, U> PartialEq<[U]> for Vec<T> {
fn eq(&self, other: &[U]) -> bool {
if self.len != other.len() { return false }
for (el, el2) in self.iter().zip(other.iter()) {
if el != el2 { return false }
@ -41,24 +41,80 @@ impl<T: PartialEq<U>, U> PartialEq<&[U]> for Vec<T> {
}
}
// [U] == Vec<T>
impl<T: PartialEq<U>, U> PartialEq<Vec<T>> for [U] {
fn eq(&self, other: &Vec<T>) -> bool {
other == self
}
}
// Vec<T> == &[U]
impl<T: PartialEq<U>, U> PartialEq<&[U]> for Vec<T> {
fn eq(&self, other: &&[U]) -> bool {
*self == **other
}
}
// &[U] == Vec<T>
impl<T: PartialEq<U>, U> PartialEq<Vec<T>> for &[U] {
fn eq(&self, other: &Vec<T>) -> bool {
other == self
*other == **self
}
}
// Vec<T> == &mut [U]
impl<T: PartialEq<U>, U> PartialEq<&mut [U]> for Vec<T> {
fn eq(&self, other: &&mut [U]) -> bool {
*self == **other
}
}
// &mut [U] == Vec<T>
impl<T: PartialEq<U>, U> PartialEq<Vec<T>> for &mut [U] {
fn eq(&self, other: &Vec<T>) -> bool {
*other == **self
}
}
// Vec<T> == [U; N]
impl<T: PartialEq<U>, U, const N: usize> PartialEq<[U; N]> for Vec<T> {
fn eq(&self, other: &[U; N]) -> bool {
*self == &other[..]
*self == other[..]
}
}
// [U; N] == Vec<T>
impl<T: PartialEq<U>, U, const N: usize> PartialEq<Vec<T>> for [U; N] {
fn eq(&self, other: &Vec<T>) -> bool {
other == self
*other == self[..]
}
}
// Vec<T> == &[U; N]
impl<T: PartialEq<U>, U, const N: usize> PartialEq<&[U; N]> for Vec<T> {
fn eq(&self, other: &&[U; N]) -> bool {
*self == other[..]
}
}
// &[U; N] == Vec<T>
impl<T: PartialEq<U>, U, const N: usize> PartialEq<Vec<T>> for &[U; N] {
fn eq(&self, other: &Vec<T>) -> bool {
*other == self[..]
}
}
// Vec<T> == &mut [U; N]
impl<T: PartialEq<U>, U, const N: usize> PartialEq<&mut [U; N]> for Vec<T> {
fn eq(&self, other: &&mut [U; N]) -> bool {
*self == other[..]
}
}
// &mut [U; N] == Vec<T>
impl<T: PartialEq<U>, U, const N: usize> PartialEq<Vec<T>> for &mut [U; N] {
fn eq(&self, other: &Vec<T>) -> bool {
*other == self[..]
}
}

View file

@ -511,7 +511,7 @@ impl<T: ?Sized> Vec<T> {
}
/// Removes the element at the specified index, shifting other elements over to fill the gap.
pub fn remove(&mut self, index: usize) -> Option<Box<T>> where T: std::fmt::Debug {
pub fn remove(&mut self, index: usize) -> Option<Box<T>> {
if index >= self.len {
return None
}