orx_tree/traversal/depth_first/
stack.rs

1use crate::{Dyn, TreeVariant, traversal::enumeration::Enumeration};
2use alloc::vec::Vec;
3use orx_selfref_col::NodePtr;
4
5pub type Item<V, E> = <E as Enumeration>::Item<NodePtr<V>>;
6
7pub struct Stack<E: Enumeration> {
8    stack: Vec<Item<Dyn<i32>, E>>,
9}
10
11impl<E: Enumeration> Stack<E> {
12    pub(crate) fn for_variant<V>(&mut self) -> &mut Vec<Item<V, E>>
13    where
14        V: TreeVariant,
15    {
16        // # SAFETY: Size and layout of stored elements in the stack
17        // solely depend on the enumeration:
18        // * Val => NodePtr<V>
19        // * DepthVal => (usize, NodePtr<V>)
20        // * SiblingIdxVal => (usize, NodePtr<V>)
21        // * DepthSiblingIdxVal => (usize, usize, NodePtr<V>)
22        //
23        // Since NodePtr<V> under the hood contains only one raw pointer,
24        // memory size and layout of elements are independent of V.
25        unsafe { core::mem::transmute(&mut self.stack) }
26    }
27}
28
29impl<E: Enumeration> Default for Stack<E> {
30    fn default() -> Self {
31        Self {
32            stack: Default::default(),
33        }
34    }
35}