urcu/collections/stack/
iterator.rs

1use std::marker::PhantomData;
2use std::ops::Deref;
3use std::ptr::NonNull;
4
5use crate::collections::stack::raw::{RawIter, RawIterRef};
6use crate::collections::stack::reference::Ref;
7use crate::rcu::flavor::RcuFlavor;
8use crate::rcu::guard::RcuGuard;
9use crate::utility::*;
10
11/// An iterator over the nodes of an [`RcuStack`].
12///
13/// [`RcuStack`]: crate::collections::stack::container::RcuStack
14pub struct Iter<'guard, T, G>
15where
16    G: RcuGuard,
17{
18    raw: RawIter<T>,
19    _guard: &'guard G,
20    _unsend: PhantomUnsend,
21    _unsync: PhantomUnsync,
22}
23
24impl<'guard, T, G> Iter<'guard, T, G>
25where
26    G: RcuGuard,
27{
28    pub(crate) fn new(raw: RawIter<T>, guard: &'guard G) -> Self {
29        Self {
30            raw,
31            _guard: guard,
32            _unsend: PhantomData,
33            _unsync: PhantomData,
34        }
35    }
36}
37
38impl<'guard, T, G> Iterator for Iter<'guard, T, G>
39where
40    Self: 'guard,
41    G: RcuGuard,
42{
43    type Item = &'guard T;
44
45    fn next(&mut self) -> Option<Self::Item> {
46        // SAFETY: The RCU critical section is enforced.
47        unsafe { self.raw.next().as_ref() }.map(|node| node.deref())
48    }
49}
50
51/// An iterator over popped nodes of an [`RcuStack`].
52///
53/// [`RcuStack`]: crate::collections::stack::container::RcuStack
54pub struct IterRef<T, F> {
55    raw: RawIterRef<T>,
56    _unsend: PhantomUnsend<F>,
57    _unsync: PhantomUnsync<F>,
58}
59
60impl<T, F> IterRef<T, F> {
61    pub(crate) fn new(raw: RawIterRef<T>) -> Self {
62        Self {
63            raw,
64            _unsend: PhantomData,
65            _unsync: PhantomData,
66        }
67    }
68}
69
70impl<T, F> Iterator for IterRef<T, F>
71where
72    T: Send + 'static,
73    F: RcuFlavor + 'static,
74{
75    type Item = Ref<T, F>;
76
77    fn next(&mut self) -> Option<Self::Item> {
78        // SAFETY: The grace period is enforced by [`Ref`].
79        NonNull::new(unsafe { self.raw.next() }).map(Ref::new)
80    }
81}