orx_linked_list/iter/
singly_iter_ptr.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
use crate::Singly;
use core::iter::FusedIterator;
use orx_pinned_vec::PinnedVec;
use orx_selfref_col::{CoreCol, Node, NodePtr};

/// An ordered iterator over pointers to the elements of the singly linked list.
///
/// Can be created by calling the `iter_ptr` method.
pub struct SinglyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Singly<T>>>,
{
    pub(crate) col: &'a CoreCol<Singly<T>, P>,
    current: Option<NodePtr<Singly<T>>>,
}

impl<'a, T, P> SinglyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Singly<T>>>,
{
    pub(crate) fn new(col: &'a CoreCol<Singly<T>, P>, current: Option<NodePtr<Singly<T>>>) -> Self {
        Self { col, current }
    }
}

impl<'a, T, P> Iterator for SinglyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Singly<T>>>,
{
    type Item = NodePtr<Singly<T>>;

    fn next(&mut self) -> Option<Self::Item> {
        match &self.current {
            Some(p) => {
                let ptr = Some(p.clone());
                self.current = self.col.node(p).next().get();
                ptr
            }
            None => None,
        }
    }
}

impl<'a, T, P> FusedIterator for SinglyIterPtr<'a, T, P> where P: PinnedVec<Node<Singly<T>>> {}

impl<'a, T, P> Clone for SinglyIterPtr<'a, T, P>
where
    P: PinnedVec<Node<Singly<T>>>,
{
    fn clone(&self) -> Self {
        Self {
            col: self.col,
            current: self.current.clone(),
        }
    }
}