urcu/stack/
iterator.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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::*;

/// An iterator over the nodes of an [`RcuStack`].
///
/// [`RcuStack`]: crate::stack::container::RcuStack
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> {
        // SAFETY: The RCU critical section is enforced.
        unsafe { self.raw.next().as_ref() }.map(|node| node.deref())
    }
}

/// An iterator over popped nodes of an [`RcuStack`].
///
/// [`RcuStack`]: crate::stack::container::RcuStack
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> {
        // SAFETY: The grace period is enforced by [`Ref`].
        NonNull::new(unsafe { self.raw.next() }).map(Ref::new)
    }
}