mirror of
https://github.com/Noratrieb/vechonk.git
synced 2026-01-16 09:35:09 +01:00
start iter
This commit is contained in:
parent
1214d7245e
commit
180228337d
2 changed files with 59 additions and 0 deletions
51
src/iter.rs
Normal file
51
src/iter.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
use crate::Vechonk;
|
||||||
|
use core::marker::PhantomData;
|
||||||
|
use core::ptr::NonNull;
|
||||||
|
|
||||||
|
/// An iterator over the elements of a [`Vechonk`]
|
||||||
|
pub struct Iter<'a, T: ?Sized> {
|
||||||
|
/// A pointer to the first element
|
||||||
|
ptr: NonNull<u8>,
|
||||||
|
/// How many elements the Vechonk has
|
||||||
|
len: usize,
|
||||||
|
/// How much memory the Vechonk owns
|
||||||
|
cap: usize,
|
||||||
|
/// How much memory has been used by the elements, where the next element starts
|
||||||
|
elem_size: usize,
|
||||||
|
/// The next element the iterator will return
|
||||||
|
current_index: usize,
|
||||||
|
_marker: PhantomData<&'a T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized> Iter<'a, T> {
|
||||||
|
pub(super) fn new(chonk: &'a Vechonk<T>) -> Iter<'a, T> {
|
||||||
|
Self {
|
||||||
|
ptr: chonk.ptr,
|
||||||
|
len: chonk.len,
|
||||||
|
cap: chonk.cap,
|
||||||
|
elem_size: chonk.elem_size,
|
||||||
|
current_index: 0,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized> Iterator for Iter<'a, T> {
|
||||||
|
type Item = &'a T;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
|
let count = self.len - self.current_index;
|
||||||
|
|
||||||
|
(count, Some(count))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized> ExactSizeIterator for Iter<'a, T> {
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
self.len - self.current_index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![feature(ptr_metadata)]
|
#![feature(ptr_metadata)]
|
||||||
|
#![feature(unsize)]
|
||||||
#![deny(unsafe_op_in_unsafe_fn)]
|
#![deny(unsafe_op_in_unsafe_fn)]
|
||||||
|
|
||||||
//!
|
//!
|
||||||
|
|
@ -34,6 +35,7 @@
|
||||||
//! ╰─────────────────────────────────────────╯
|
//! ╰─────────────────────────────────────────╯
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
mod iter;
|
||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
@ -46,6 +48,8 @@ use core::ops::{Index, IndexMut};
|
||||||
use core::ptr::{NonNull, Pointee};
|
use core::ptr::{NonNull, Pointee};
|
||||||
use core::{mem, ptr};
|
use core::{mem, ptr};
|
||||||
|
|
||||||
|
pub use iter::Iter;
|
||||||
|
|
||||||
/// chonky af
|
/// chonky af
|
||||||
///
|
///
|
||||||
/// note: it does not run destructors for now, thankfully that is 100% safe :))))
|
/// note: it does not run destructors for now, thankfully that is 100% safe :))))
|
||||||
|
|
@ -235,6 +239,10 @@ impl<T: ?Sized> Vechonk<T> {
|
||||||
Some(return_box)
|
Some(return_box)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> Iter<T> {
|
||||||
|
Iter::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a reference to an element at the index. Returns `None` if the index is out of bounds
|
/// Get a reference to an element at the index. Returns `None` if the index is out of bounds
|
||||||
pub fn get(&self, index: usize) -> Option<&T> {
|
pub fn get(&self, index: usize) -> Option<&T> {
|
||||||
if index < self.len {
|
if index < self.len {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue