dyn_vec/src/iter.rs

215 lines
5 KiB
Rust
Raw Normal View History

//! Iteration.
2022-12-26 11:39:50 -06:00
use std::{marker::PhantomData, mem::ManuallyDrop, ptr::NonNull};
2022-06-09 21:00:24 -05:00
2022-08-19 12:13:36 -05:00
use crate::{ptr_ext::PtrExt, DynVec};
struct BaseIter<T: ?Sized> {
ptr: *const *mut T,
ptr_back: *const *mut T,
}
2022-08-14 14:21:40 -05:00
unsafe impl<T: Send> Send for BaseIter<T> {}
unsafe impl<T: Sync> Sync for BaseIter<T> {}
impl<T: ?Sized> BaseIter<T> {
2022-12-26 11:39:50 -06:00
pub(crate) fn new(vec: &DynVec<T>) -> Self {
Self {
ptr: vec.get_ptr_to_extra(vec.len).cast(),
ptr_back: vec.get_ptr_to_extra(0).cast(),
}
}
}
impl<T: ?Sized> Iterator for BaseIter<T> {
type Item = *mut T;
fn next(&mut self) -> Option<Self::Item> {
if self.ptr == self.ptr_back {
2022-08-14 14:21:40 -05:00
return None;
}
self.ptr_back = self.ptr_back.wrapping_sub(1);
Some(unsafe { self.ptr_back.read() })
}
}
impl<T: ?Sized> DoubleEndedIterator for BaseIter<T> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.ptr == self.ptr_back {
2022-08-14 14:21:40 -05:00
return None;
}
let el = unsafe { self.ptr.read() };
self.ptr = self.ptr.wrapping_add(1);
Some(el)
}
}
// By-ref iteration
2022-12-26 11:39:50 -06:00
impl<T: ?Sized> DynVec<T> {
/// Iterates over the vector by-ref.
pub fn iter(&self) -> Iter<T> {
Iter {
base: BaseIter::new(self),
_phantom: PhantomData,
}
}
}
2022-08-19 12:13:36 -05:00
impl<'a, T: ?Sized> IntoIterator for &'a DynVec<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
2022-08-19 12:13:36 -05:00
/// A by-ref iterator over the elements of a [`DynVec`].
pub struct Iter<'a, T: ?Sized> {
base: BaseIter<T>,
2022-08-14 14:21:40 -05:00
_phantom: PhantomData<&'a T>,
}
2022-06-08 23:00:11 -05:00
unsafe impl<'a, T: Send> Send for Iter<'a, T> {}
unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {}
impl<'a, T: ?Sized> Iterator for Iter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
2022-05-17 09:47:54 -05:00
self.base.next().map(|v| unsafe { &*v })
}
}
impl<'a, T: ?Sized> DoubleEndedIterator for Iter<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
2022-05-17 09:47:54 -05:00
self.base.next_back().map(|v| unsafe { &*v })
}
}
// By-mut iteration
2022-12-26 11:39:50 -06:00
impl<T: ?Sized> DynVec<T> {
/// Iterates over the vector by-mut.
pub fn iter_mut(&mut self) -> IterMut<T> {
IterMut {
base: BaseIter::new(self),
_phantom: PhantomData,
}
}
}
2022-08-19 12:13:36 -05:00
impl<'a, T: ?Sized> IntoIterator for &'a mut DynVec<T> {
type Item = &'a mut T;
type IntoIter = IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
2022-08-19 12:13:36 -05:00
/// A by-mut iterator over the elements of a [`DynVec`].
2022-05-16 09:42:00 -05:00
#[allow(clippy::module_name_repetitions)]
pub struct IterMut<'a, T: ?Sized> {
base: BaseIter<T>,
2022-08-14 14:21:40 -05:00
_phantom: PhantomData<&'a mut T>,
}
2022-06-08 23:00:11 -05:00
unsafe impl<'a, T: Send> Send for IterMut<'a, T> {}
unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}
impl<'a, T: ?Sized> Iterator for IterMut<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
2022-05-17 09:47:54 -05:00
self.base.next().map(|v| unsafe { &mut *v })
}
}
impl<'a, T: ?Sized> DoubleEndedIterator for IterMut<'a, T> {
fn next_back(&mut self) -> Option<Self::Item> {
2022-08-14 14:21:40 -05:00
self.base.next_back().map(|v| unsafe { &mut *v })
}
}
// By-value iteration
2022-08-19 12:13:36 -05:00
impl<T: ?Sized> IntoIterator for DynVec<T> {
type Item = Box<T>;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
2022-12-26 11:39:50 -06:00
let this = ManuallyDrop::new(self);
IntoIter {
ptr: this.ptr,
capacity: this.capacity,
base: BaseIter::new(&this),
}
}
}
2022-08-19 12:13:36 -05:00
/// A by-value iterator over the elements of a [`DynVec`].
2022-05-16 09:42:00 -05:00
#[allow(clippy::module_name_repetitions)]
pub struct IntoIter<T: ?Sized> {
ptr: NonNull<u8>,
capacity: usize,
2022-08-14 14:21:40 -05:00
base: BaseIter<T>,
}
2022-06-08 23:00:11 -05:00
unsafe impl<T: Send> Send for IntoIter<T> {}
unsafe impl<T: Sync> Sync for IntoIter<T> {}
impl<T: ?Sized> Iterator for IntoIter<T> {
type Item = Box<T>;
fn next(&mut self) -> Option<Self::Item> {
2022-05-17 09:47:54 -05:00
Some(unsafe { self.base.next()?.read_to_box() })
}
}
impl<T: ?Sized> DoubleEndedIterator for IntoIter<T> {
fn next_back(&mut self) -> Option<Self::Item> {
2022-05-17 09:47:54 -05:00
Some(unsafe { self.base.next_back()?.read_to_box() })
}
}
impl<T: ?Sized> Drop for IntoIter<T> {
fn drop(&mut self) {
2022-08-19 12:13:36 -05:00
drop(DynVec::<T> {
ptr: self.ptr,
len: 0,
capacity: self.capacity,
end_ptr: self.ptr,
2022-08-14 14:21:40 -05:00
_phantom: PhantomData,
});
}
}
// // this implementation will collect *while unsizing*, and would conflict with the other
2022-08-19 12:13:36 -05:00
// impl<T: ?Sized, U> FromIterator<U> for DynVec<T> where for<'a> &'a U: CoerceUnsized<&'a T> {
// fn from_iter<I: IntoIterator<Item = U>>(iter: I) -> Self {
2022-08-19 12:13:36 -05:00
// let mut vec = DynVec::new();
// for item in iter.into_iter() {
// vec.push_unsize(item);
// }
// vec
// }
// }
2022-08-19 12:13:36 -05:00
impl<T> FromIterator<T> for DynVec<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
2022-08-14 14:21:40 -05:00
let mut vec = Self::new();
2022-05-16 09:42:00 -05:00
for item in iter {
vec.push(item);
}
vec
}
2022-08-14 14:21:40 -05:00
}