118 lines
2.9 KiB
Rust
118 lines
2.9 KiB
Rust
//! Implements `Debug`, and `PartialEq` for various list-like types.
|
|
|
|
#[allow(clippy::wildcard_imports)]
|
|
use super::*;
|
|
|
|
use std::{fmt::Debug, ops::{Deref, DerefMut}, vec::Vec as StdVec};
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> Deref for Vec<T> {
|
|
type Target = [T];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
unsafe { slice::from_raw_parts(self.get_ptr(0), self.len) }
|
|
}
|
|
}
|
|
|
|
impl<T> DerefMut for Vec<T> {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
unsafe { slice::from_raw_parts_mut(self.get_ptr(0) as _, self.len) }
|
|
}
|
|
}
|
|
|
|
unsafe impl<T: Send> Send for Vec<T> {}
|
|
unsafe impl<T: Sync> Sync for Vec<T> {}
|
|
|
|
impl<T> From<StdVec<T>> for Vec<T> {
|
|
fn from(std_vec: StdVec<T>) -> Self {
|
|
let mut vec = Vec::new();
|
|
let new_cap = (size_of::<T>() + size_of::<Extra<T>>()) * std_vec.len();
|
|
unsafe { vec.realloc(new_cap); }
|
|
|
|
for item in std_vec {
|
|
unsafe { vec.push_raw_unchecked(&item) }
|
|
}
|
|
|
|
vec
|
|
}
|
|
}
|
|
|
|
impl<T> From<Vec<T>> for StdVec<T> {
|
|
fn from(mut vec: Vec<T>) -> Self {
|
|
let mut std_vec = StdVec::with_capacity(vec.len);
|
|
|
|
for item in vec.iter() {
|
|
std_vec.push(unsafe { (item as *const T).read() });
|
|
}
|
|
|
|
vec.len = 0;
|
|
|
|
std_vec
|
|
}
|
|
} |