orx_tree/iter/
children_mut.rs

1use crate::{
2    NodeMut, TreeVariant,
3    aliases::{Col, N},
4    memory::MemoryPolicy,
5    node_mut::NodeMutDown,
6    pinned_storage::PinnedStorage,
7    tree_variant::RefsChildren,
8};
9use orx_selfref_col::NodePtr;
10
11/// Mutable children iterator.
12pub struct ChildrenMutIter<'a, 'b, V, M, P>
13where
14    V: TreeVariant + 'a,
15    M: MemoryPolicy,
16    P: PinnedStorage,
17    'a: 'b,
18{
19    // node_ptr: *const N<V>,
20    col: &'a mut Col<V, M, P>,
21    children_ptr: <V::Children as RefsChildren<V>>::ChildrenPtrIter<'b>,
22}
23
24impl<'a, 'b, V, M, P> ChildrenMutIter<'a, 'b, V, M, P>
25where
26    V: TreeVariant + 'a,
27    M: MemoryPolicy,
28    P: PinnedStorage,
29    'a: 'b,
30{
31    pub(crate) fn new(col: &'a mut Col<V, M, P>, node_ptr: *const N<V>) -> Self {
32        let node = unsafe { &*node_ptr };
33        let children_ptr = node.next().children_ptr();
34
35        Self { children_ptr, col }
36    }
37
38    fn next_child(&mut self, child_ptr: NodePtr<V>) -> NodeMut<'b, V, M, P, NodeMutDown> {
39        let col_mut = unsafe { &mut *(self.col as *mut Col<V, M, P>) };
40        NodeMut::new(col_mut, child_ptr)
41    }
42}
43
44impl<'a, 'b, V, M, P> Iterator for ChildrenMutIter<'a, 'b, V, M, P>
45where
46    V: TreeVariant + 'a,
47    M: MemoryPolicy,
48    P: PinnedStorage,
49    'a: 'b,
50{
51    type Item = NodeMut<'b, V, M, P, NodeMutDown>;
52
53    fn next(&mut self) -> Option<Self::Item> {
54        self.children_ptr.next().map(|p| self.next_child(p.clone()))
55    }
56
57    fn size_hint(&self) -> (usize, Option<usize>) {
58        self.children_ptr.size_hint()
59    }
60}
61
62impl<'a, 'b, V, M, P> ExactSizeIterator for ChildrenMutIter<'a, 'b, V, M, P>
63where
64    V: TreeVariant + 'a,
65    M: MemoryPolicy,
66    P: PinnedStorage,
67    'a: 'b,
68{
69    fn len(&self) -> usize {
70        self.children_ptr.len()
71    }
72}
73
74impl<'a, 'b, V, M, P> DoubleEndedIterator for ChildrenMutIter<'a, 'b, V, M, P>
75where
76    V: TreeVariant + 'a,
77    M: MemoryPolicy,
78    P: PinnedStorage,
79    'a: 'b,
80{
81    fn next_back(&mut self) -> Option<Self::Item> {
82        self.children_ptr
83            .next_back()
84            .map(|p| self.next_child(p.clone()))
85    }
86}