dyn_vec/src/impls.rs

73 lines
1.8 KiB
Rust

//! Implements `Debug`, and `PartialEq` for various list-like types.
#[allow(clippy::wildcard_imports)]
use super::*;
use std::fmt::Debug;
impl<T: ?Sized> Default for Vec<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: ?Sized + Debug> Debug for Vec<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
// Vec<T> == Vec<U>
impl<T: ?Sized + PartialEq<U>, U: ?Sized> PartialEq<Vec<U>> for Vec<T> {
fn eq(&self, other: &Vec<U>) -> bool {
if self.len != other.len { return false }
for (el, el2) in self.iter().zip(other.iter()) {
if el != el2 { return false }
}
true
}
}
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 {
if self.len != other.len() { return false }
for (el, el2) in self.iter().zip(other.iter()) {
if el != el2 { return false }
}
true
}
}
// &[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; 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
}
}
impl<T: ?Sized, U: ?Sized> Extend<Box<U>> for Vec<T> where Box<U>: CoerceUnsized<Box<T>> {
fn extend<I: IntoIterator<Item = Box<U>>>(&mut self, iter: I) {
for item in iter {
// TODO: optmize
self.push_box(item);
}
}
}