//! Implements `Debug`, and `PartialEq` for various list-like types. use crate::{Vec, Extra}; use std::{fmt::Debug, ops::{Deref, DerefMut}, vec::Vec as StdVec, slice, mem::size_of}; impl Default for Vec { fn default() -> Self { Self::new() } } impl Debug for Vec { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_list().entries(self.iter()).finish() } } // Vec == Vec impl, U: ?Sized> PartialEq> for Vec { fn eq(&self, other: &Vec) -> bool { if self.len != other.len { return false } for (el, el2) in self.iter().zip(other.iter()) { if el != el2 { return false } } true } } impl Eq for Vec {} // Vec == &[U] impl, U> PartialEq<&[U]> for Vec { 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 impl, U> PartialEq> for &[U] { fn eq(&self, other: &Vec) -> bool { other == self } } // Vec == [U; N] impl, U, const N: usize> PartialEq<[U; N]> for Vec { fn eq(&self, other: &[U; N]) -> bool { *self == &other[..] } } // [U; N] == Vec impl, U, const N: usize> PartialEq> for [U; N] { fn eq(&self, other: &Vec) -> bool { other == self } } #[cfg(not(feature = "unstable"))] impl Extend> for Vec { fn extend>>(&mut self, iter: I) { for item in iter { // TODO: optmize self.push_box(item); } } } #[cfg(feature = "unstable")] impl Extend> for Vec where Box: CoerceUnsized> { fn extend>>(&mut self, iter: I) { for item in iter { // TODO: optmize self.push_box(item); } } } impl Deref for Vec { type Target = [T]; fn deref(&self) -> &Self::Target { unsafe { slice::from_raw_parts(self.get_ptr(0), self.len) } } } impl DerefMut for Vec { fn deref_mut(&mut self) -> &mut Self::Target { unsafe { slice::from_raw_parts_mut(self.get_ptr(0) as _, self.len) } } } unsafe impl Send for Vec {} unsafe impl Sync for Vec {} impl From> for Vec { fn from(std_vec: StdVec) -> Self { let mut vec = Vec::new(); let new_cap = (size_of::() + size_of::>()) * std_vec.len(); unsafe { vec.realloc(new_cap); } for item in std_vec { unsafe { vec.push_raw_unchecked(&item) } } vec } } impl From> for StdVec { fn from(mut vec: Vec) -> 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 } }