orx_linked_list/iter/
singly_iter_owned.rs

1use crate::Singly;
2use core::iter::FusedIterator;
3use orx_pinned_vec::PinnedVec;
4use orx_selfref_col::{CoreCol, Node, NodePtr};
5
6/// An ordered consuming iterator of the singly linked list.
7///
8/// Can be created by calling the `into_iter` method.
9pub struct SinglyIterOwned<T, P>
10where
11    P: PinnedVec<Node<Singly<T>>>,
12{
13    pub(crate) col: CoreCol<Singly<T>, P>,
14    current: Option<NodePtr<Singly<T>>>,
15}
16
17impl<T, P> SinglyIterOwned<T, P>
18where
19    P: PinnedVec<Node<Singly<T>>>,
20{
21    pub(crate) fn new(col: CoreCol<Singly<T>, P>) -> Self {
22        let current = col.ends().get();
23        Self { col, current }
24    }
25}
26
27impl<T, P> Iterator for SinglyIterOwned<T, P>
28where
29    P: PinnedVec<Node<Singly<T>>>,
30{
31    type Item = T;
32
33    fn next(&mut self) -> Option<Self::Item> {
34        match self.current {
35            Some(p) => {
36                // SAFETY: collection as alive as guaranteed by the `col` field.
37                let ptr = unsafe { p.ptr_mut() };
38                self.current = self.col.node(p).next().get();
39                unsafe { &mut *ptr }.take_data()
40            }
41            None => None,
42        }
43    }
44}
45
46impl<T, P> FusedIterator for SinglyIterOwned<T, P> where P: PinnedVec<Node<Singly<T>>> {}