dyn_vec/src/impls.rs
2022-12-26 11:39:50 -06:00

215 lines
5.2 KiB
Rust

//! Implements `Debug`, `Default`, `Extend`, and `Deref`. Also implements `PartialEq` for various list-like types.
use crate::{DynVec, Extra};
use std::{
fmt::Debug,
mem::size_of,
ops::{Deref, DerefMut},
ptr::NonNull,
slice,
vec::Vec,
};
// #[cfg(any(doc, feature = "unstable"))]
// use std::ops::CoerceUnsized;
impl<T: ?Sized> Default for DynVec<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: ?Sized + Debug> Debug for DynVec<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
// DynVec<T> == DynVec<U>
impl<T: ?Sized + PartialEq<U>, U: ?Sized> PartialEq<DynVec<U>> for DynVec<T> {
fn eq(&self, other: &DynVec<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 DynVec<T> {}
// DynVec<T> == [U]
impl<T: PartialEq<U> + ?Sized, U> PartialEq<[U]> for DynVec<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] == DynVec<T>
impl<T: PartialEq<U> + ?Sized, U> PartialEq<DynVec<T>> for [U] {
fn eq(&self, other: &DynVec<T>) -> bool {
other == self
}
}
// DynVec<T> == &[U]
impl<T: PartialEq<U> + ?Sized, U> PartialEq<&[U]> for DynVec<T> {
fn eq(&self, other: &&[U]) -> bool {
*self == **other
}
}
// &[U] == DynVec<T>
impl<T: PartialEq<U> + ?Sized, U> PartialEq<DynVec<T>> for &[U] {
fn eq(&self, other: &DynVec<T>) -> bool {
*other == **self
}
}
// DynVec<T> == &mut [U]
impl<T: PartialEq<U> + ?Sized, U> PartialEq<&mut [U]> for DynVec<T> {
fn eq(&self, other: &&mut [U]) -> bool {
*self == **other
}
}
// &mut [U] == DynVec<T>
impl<T: PartialEq<U> + ?Sized, U> PartialEq<DynVec<T>> for &mut [U] {
fn eq(&self, other: &DynVec<T>) -> bool {
*other == **self
}
}
// DynVec<T> == [U; N]
impl<T: PartialEq<U> + ?Sized, U, const N: usize> PartialEq<[U; N]> for DynVec<T> {
fn eq(&self, other: &[U; N]) -> bool {
*self == other[..]
}
}
// [U; N] == DynVec<T>
impl<T: PartialEq<U> + ?Sized, U, const N: usize> PartialEq<DynVec<T>> for [U; N] {
fn eq(&self, other: &DynVec<T>) -> bool {
*other == self[..]
}
}
// DynVec<T> == &[U; N]
impl<T: PartialEq<U> + ?Sized, U, const N: usize> PartialEq<&[U; N]> for DynVec<T> {
fn eq(&self, other: &&[U; N]) -> bool {
*self == other[..]
}
}
// &[U; N] == DynVec<T>
impl<T: PartialEq<U> + ?Sized, U, const N: usize> PartialEq<DynVec<T>> for &[U; N] {
fn eq(&self, other: &DynVec<T>) -> bool {
*other == self[..]
}
}
// DynVec<T> == &mut [U; N]
impl<T: PartialEq<U> + ?Sized, U, const N: usize> PartialEq<&mut [U; N]> for DynVec<T> {
fn eq(&self, other: &&mut [U; N]) -> bool {
*self == other[..]
}
}
// &mut [U; N] == DynVec<T>
impl<T: PartialEq<U> + ?Sized, U, const N: usize> PartialEq<DynVec<T>> for &mut [U; N] {
fn eq(&self, other: &DynVec<T>) -> bool {
*other == self[..]
}
}
// #[cfg(not(feature = "unstable"))]
impl<T: ?Sized> Extend<Box<T>> for DynVec<T> {
fn extend<I: IntoIterator<Item = Box<T>>>(&mut self, iter: I) {
for item in iter {
// TODO: optmize
self.push_box(item);
}
}
}
// #[cfg(feature = "unstable")]
// impl<T: ?Sized, U: ?Sized> Extend<Box<U>> for DynVec<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 DynVec<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
if self.is_empty() {
unsafe { slice::from_raw_parts(NonNull::dangling().as_ptr(), 0) }
} else {
unsafe { slice::from_raw_parts(self.get_ptr(0), self.len) }
}
}
}
impl<T> DerefMut for DynVec<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
if self.is_empty() {
unsafe { slice::from_raw_parts_mut(NonNull::dangling().as_ptr(), 0) }
} else {
unsafe { slice::from_raw_parts_mut(self.get_ptr(0) as _, self.len) }
}
}
}
unsafe impl<T: Send> Send for DynVec<T> {}
unsafe impl<T: Sync> Sync for DynVec<T> {}
impl<T> From<Vec<T>> for DynVec<T> {
fn from(std_vec: Vec<T>) -> Self {
let mut vec = Self::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<DynVec<T>> for Vec<T> {
fn from(mut vec: DynVec<T>) -> Self {
let mut std_vec = Self::with_capacity(vec.len);
for item in vec.iter() {
std_vec.push(unsafe { (item as *const T).read() });
}
vec.len = 0;
std_vec
}
}