use crate::{SSlice, Sentinel};
pub struct Iter<T: Sentinel>(SSlice<T>);
impl<T: Sentinel> Iter<T> {
#[inline(always)]
pub(crate) fn new_ref(slice: &SSlice<T>) -> &Self {
unsafe { &*(slice as *const SSlice<T> as *const Self) }
}
#[inline(always)]
pub(crate) fn new_mut(slice: &mut SSlice<T>) -> &mut Self {
unsafe { &mut *(slice as *mut SSlice<T> as *mut Self) }
}
#[inline(always)]
pub fn remainder(&self) -> &SSlice<T> {
&self.0
}
#[inline(always)]
pub fn remainder_mut(&mut self) -> &mut SSlice<T> {
&mut self.0
}
#[inline(always)]
pub fn unwrap_sentinels(&self) -> UnwrapCopiedSentinels<&Self>
where
T: Copy,
{
unsafe { UnwrapCopiedSentinels::new(self.copied()) }
}
}
impl<'a, T: Sentinel> Iterator for &'a Iter<T> {
type Item = &'a T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some((first, remainder)) = self.0.split_first() {
*self = Iter::new_ref(remainder);
Some(first)
} else {
None
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
#[inline(always)]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}
}
impl<'a, T: Sentinel> ExactSizeIterator for &'a Iter<T> {
#[inline(always)]
fn len(&self) -> usize {
self.0.len()
}
}
impl<'a, T: Sentinel> Iterator for &'a mut Iter<T> {
type Item = &'a mut T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
unsafe {
if T::is_sentinel(&*self.0.as_ptr()) {
None
} else {
let first = &mut *self.0.as_mut_ptr();
*self = Iter::new_mut(SSlice::from_mut_ptr(self.0.as_mut_ptr().add(1)));
Some(first)
}
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
#[inline(always)]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}
}
impl<'a, T: Sentinel> ExactSizeIterator for &'a mut Iter<T> {
#[inline(always)]
fn len(&self) -> usize {
self.0.len()
}
}
pub type UnwrapCopiedSentinels<I> = UnwrapSentinels<core::iter::Copied<I>>;
#[derive(Clone)]
pub struct UnwrapSentinels<I> {
iter: I,
}
impl<I> UnwrapSentinels<I> {
#[inline]
pub(crate) unsafe fn new(iter: I) -> Self {
Self { iter }
}
}
impl<I> Iterator for UnwrapSentinels<I>
where
I: Iterator,
I::Item: Sentinel,
{
type Item = <I::Item as Sentinel>::Unwrapped;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter
.next()
.map(|val| unsafe { I::Item::unwrap_sentinel_unchecked(val) })
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline(always)]
fn count(self) -> usize
where
Self: Sized,
{
self.iter.count()
}
}
impl<I> ExactSizeIterator for UnwrapSentinels<I>
where
I: ExactSizeIterator,
I::Item: Sentinel,
{
#[inline(always)]
fn len(&self) -> usize {
self.iter.len()
}
}