orx_tree/traversal/depth_first/
iter_mut.rs

1use super::dfs_enumeration::DepthFirstEnumeration;
2use super::iter_ptr::DfsIterPtr;
3use super::stack::Item;
4use crate::TreeVariant;
5use crate::aliases::Col;
6use crate::memory::MemoryPolicy;
7use crate::pinned_storage::PinnedStorage;
8use crate::traversal::node_item_mut::NodeItemMut;
9use alloc::vec::Vec;
10use core::marker::PhantomData;
11use orx_self_or::SoM;
12
13pub struct DfsIterMut<'a, V, M, P, E, S, D>
14where
15    V: TreeVariant,
16    M: MemoryPolicy,
17    P: PinnedStorage,
18    E: DepthFirstEnumeration,
19    S: SoM<Vec<Item<V, E>>>,
20    D: NodeItemMut<'a, V, M, P>,
21{
22    col: &'a Col<V, M, P>,
23    iter: DfsIterPtr<V, E, S>,
24    phantom: PhantomData<D>,
25}
26
27impl<'a, V, M, P, E, S, D> DfsIterMut<'a, V, M, P, E, S, D>
28where
29    V: TreeVariant,
30    M: MemoryPolicy,
31    P: PinnedStorage,
32    E: DepthFirstEnumeration,
33    S: SoM<Vec<Item<V, E>>>,
34    D: NodeItemMut<'a, V, M, P>,
35{
36    /// # Safety
37    ///
38    /// We are creating a mutable iterator over nodes of the collection `col`.
39    /// This is safe only when the second argument `iter` makes sure that there exists only one mutable
40    /// reference to the collection.
41    ///
42    /// This is the case how this method is used, as follows:
43    /// * Mutable iterators are created through the `Dfs` traverser's `TraverserMut::iter_mut` method.
44    /// * This method requires a mutable reference to a mutable node `NodeMut` which is guaranteed to
45    ///   be the only reference to the collection.
46    /// * Finally, this iterator's lifetime is equal to the borrow duration of the mutable node.
47    pub(crate) unsafe fn from((col, iter): (&'a Col<V, M, P>, DfsIterPtr<V, E, S>)) -> Self {
48        Self {
49            col,
50            iter,
51            phantom: PhantomData,
52        }
53    }
54}
55
56impl<'a, V, M, P, E, S, D> Iterator for DfsIterMut<'a, V, M, P, E, S, D>
57where
58    V: TreeVariant,
59    M: MemoryPolicy,
60    P: PinnedStorage,
61    E: DepthFirstEnumeration,
62    S: SoM<Vec<Item<V, E>>>,
63    D: NodeItemMut<'a, V, M, P>,
64{
65    type Item = E::Item<D>;
66
67    fn next(&mut self) -> Option<Self::Item> {
68        self.iter
69            .next()
70            .map(move |element: Item<V, E>| E::from_element_ptr_mut(self.col, element))
71    }
72}