orx_linked_list/iter/
singly_iter.rs

1use super::singly_iter_ptr::SinglyIterPtr;
2use crate::Singly;
3use core::iter::FusedIterator;
4use orx_pinned_vec::PinnedVec;
5use orx_selfref_col::{CoreCol, Node, NodePtr};
6
7/// An ordered iterator over elements of the singly linked list.
8///
9/// Can be created by calling the `iter` method.
10pub struct SinglyIter<'a, T, P>(SinglyIterPtr<'a, T, P>)
11where
12    P: PinnedVec<Node<Singly<T>>>;
13
14impl<'a, T, P> SinglyIter<'a, T, P>
15where
16    P: PinnedVec<Node<Singly<T>>>,
17{
18    pub(crate) fn new(col: &'a CoreCol<Singly<T>, P>, current: Option<NodePtr<Singly<T>>>) -> Self {
19        Self(SinglyIterPtr::new(col, current))
20    }
21}
22
23impl<'a, T, P> Iterator for SinglyIter<'a, T, P>
24where
25    P: PinnedVec<Node<Singly<T>>>,
26{
27    type Item = &'a T;
28
29    #[inline(always)]
30    fn next(&mut self) -> Option<Self::Item> {
31        self.0
32            .next()
33            .map(|p| unsafe { self.0.col.data_unchecked(&p) })
34    }
35}
36
37impl<T, P> FusedIterator for SinglyIter<'_, T, P> where P: PinnedVec<Node<Singly<T>>> {}
38
39impl<T, P> Clone for SinglyIter<'_, T, P>
40where
41    P: PinnedVec<Node<Singly<T>>>,
42{
43    fn clone(&self) -> Self {
44        Self(self.0.clone())
45    }
46}