arena_voxel_tree/
naive_node.rs

1use std::{
2    fmt::Display,
3    sync::{Arc, RwLock, Weak},
4};
5
6use smallvec::SmallVec;
7
8/// This struct holds underlying data. It shouldn't be created directly, instead use:
9/// [`NodeData`](struct@NodeData).
10/// * This library is inspired by [r3bl-org work](https://github.com/r3bl-org/)
11///
12/// ```text
13/// NodeData
14///  | | |
15///  | | +- value: T ---------------------------------------+
16///  | |                                                    |
17///  | |                                        Simple onwership of value
18///  | |
19///  | +-- parent: RwLock<WeakNodeNodeRef<T>> --------+
20///  |                                            |
21///  |                 This describes a non-ownership relationship.
22///  |                 When a node is dropped, its parent will not be dropped.
23///  |
24///  +---- children: RwLock<Vec<Child<T>>> ---+
25///                                           |
26///                 This describes an ownership relationship.
27///                 When a node is dropped its children will be dropped as well.
28/// ```
29pub struct NodeData<T>
30where
31    T: Display,
32{
33    pub value: T,
34    pub parent: Parent<T>,
35    pub children: Children<T>,
36}
37
38pub type NodeDataRef<T> = Arc<NodeData<T>>;
39pub type WeakNodeNodeRef<T> = Weak<NodeData<T>>;
40
41/// Parent relationship is one of non-ownership.
42/// This is not a `RwLock<NodeDataRef<T>>` which would cause memory leak.
43pub type Parent<T> = RwLock<WeakNodeNodeRef<T>>;
44
45/// Children relationship is one of ownership.
46pub type Children<T> = RwLock<SmallVec<[Child<T>; 8]>>;
47pub type Child<T> = NodeDataRef<T>;