use std::marker::PhantomData;
use std::ops::Deref;
use std::ptr::NonNull;
use crate::rcu::RcuContext;
use crate::stack::raw::{RawIter, RawIterRef};
use crate::stack::reference::Ref;
use crate::utility::*;
pub struct Iter<'ctx, 'guard, T, C>
where
C: RcuContext + 'ctx,
{
raw: RawIter<T>,
_guard: &'guard C::Guard<'ctx>,
_unsend: PhantomUnsend,
_unsync: PhantomUnsync,
}
impl<'ctx, 'guard, T, C> Iter<'ctx, 'guard, T, C>
where
C: RcuContext + 'ctx,
{
pub(crate) fn new(raw: RawIter<T>, guard: &'guard C::Guard<'ctx>) -> Self {
Self {
raw,
_guard: guard,
_unsend: PhantomData,
_unsync: PhantomData,
}
}
}
impl<'guard, T, C> Iterator for Iter<'_, 'guard, T, C>
where
Self: 'guard,
C: RcuContext,
{
type Item = &'guard T;
fn next(&mut self) -> Option<Self::Item> {
unsafe { self.raw.next().as_ref() }.map(|node| node.deref())
}
}
pub struct IterRef<T, C> {
raw: RawIterRef<T>,
_unsend: PhantomUnsend<C>,
_unsync: PhantomUnsync<C>,
}
impl<T, C> IterRef<T, C> {
pub(crate) fn new(raw: RawIterRef<T>) -> Self {
Self {
raw,
_unsend: PhantomData,
_unsync: PhantomData,
}
}
}
impl<T, C> Iterator for IterRef<T, C>
where
T: Send + 'static,
C: RcuContext + 'static,
{
type Item = Ref<T, C>;
fn next(&mut self) -> Option<Self::Item> {
NonNull::new(unsafe { self.raw.next() }).map(Ref::new)
}
}