orx_linked_list/iter/
singly_iter_mut.rs

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