use crate::{UnthBuf, CellLayout};
use std::borrow::Cow;
impl<CL: CellLayout> UnthBuf<CL> {
pub fn iter(&self) -> UnthBufIter<CL> {
UnthBufIter {
idx: 0,
cap: self.capacity,
buf: Cow::Borrowed(self)
}
}
}
impl<'buf, CL: CellLayout + 'static> IntoIterator for &'buf UnthBuf<CL> {
type Item = usize;
type IntoIter = UnthBufIter<'buf, CL>;
fn into_iter(self) -> Self::IntoIter {
UnthBufIter {
idx: 0,
cap: self.capacity,
buf: Cow::Borrowed(self)
}
}
}
impl<CL: CellLayout + 'static> IntoIterator for UnthBuf<CL> {
type Item = usize;
type IntoIter = UnthBufIter<'static, CL>;
fn into_iter(self) -> Self::IntoIter {
UnthBufIter {
idx: 0,
cap: self.capacity,
buf: Cow::Owned(self)
}
}
}
pub struct UnthBufIter<'buf, CL: CellLayout + 'static> {
pub(crate) buf: Cow<'buf, UnthBuf<CL>>,
pub(crate) idx: usize,
pub(crate) cap: usize
}
impl<CL: CellLayout + 'static> core::iter::Iterator for UnthBufIter<'_, CL> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
if ! self.idx < self.cap {
return None;
}
let item = unsafe {
self.buf.get_unchecked(self.idx)
};
self.idx += 1;
Some(item)
}
}
impl<CL: CellLayout + 'static> core::iter::ExactSizeIterator for UnthBufIter<'_, CL> {
fn len(&self) -> usize {
self.buf.capacity - self.idx
}
}
impl<CL: CellLayout + 'static> core::iter::FusedIterator for UnthBufIter<'_, CL> {}