mirror of
https://github.com/Noratrieb/vechonk.git
synced 2026-01-15 17:15:11 +01:00
iterators and trait impls!
This commit is contained in:
parent
0550582d03
commit
dbd07bb4c8
4 changed files with 235 additions and 52 deletions
63
src/iter.rs
63
src/iter.rs
|
|
@ -1,5 +1,7 @@
|
|||
use crate::{RawVechonk, Vechonk};
|
||||
use alloc::boxed::Box;
|
||||
use core::marker::PhantomData;
|
||||
use core::mem;
|
||||
|
||||
/// An iterator over the elements of a [`Vechonk`]
|
||||
pub struct Iter<'a, T: ?Sized> {
|
||||
|
|
@ -47,3 +49,64 @@ impl<'a, T: ?Sized> ExactSizeIterator for Iter<'a, T> {
|
|||
self.raw.len - self.current_index
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over the elements of a [`Vechonk`]
|
||||
pub struct IntoIter<T: ?Sized> {
|
||||
raw: RawVechonk<T>,
|
||||
current_index: usize,
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized> IntoIter<T> {
|
||||
pub(super) fn new(chonk: Vechonk<T>) -> IntoIter<T> {
|
||||
let raw = chonk.raw.copy();
|
||||
|
||||
// We don't want to free the memory!
|
||||
mem::forget(chonk);
|
||||
|
||||
Self {
|
||||
raw,
|
||||
current_index: 0,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Iterator for IntoIter<T> {
|
||||
type Item = Box<T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_index == self.raw.len {
|
||||
return None;
|
||||
}
|
||||
|
||||
// SAFETY: We just did a bounds check above
|
||||
// We also increment the `current_index`, to make sure that we never access it again
|
||||
let ptr = unsafe { self.raw.box_elem_unchecked(self.current_index) };
|
||||
|
||||
self.current_index += 1;
|
||||
|
||||
Some(ptr)
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let count = self.raw.len - self.current_index;
|
||||
|
||||
(count, Some(count))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> ExactSizeIterator for IntoIter<T> {
|
||||
fn len(&self) -> usize {
|
||||
self.raw.len - self.current_index
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Drop for IntoIter<T> {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: We as `Vechonk` do own the data, and it has the length `self.raw.cap`
|
||||
unsafe {
|
||||
RawVechonk::<T>::dealloc(self.raw.cap, self.raw.ptr.as_ptr());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue