syntree/
links.rs

1//! Central struct to keep track of all internal linking of a tree.
2
3use core::cell::Cell;
4
5use crate::span::Span;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub(crate) struct Links<T, I, P>
9where
10    T: Copy,
11{
12    /// The data in the node.
13    pub(crate) data: Cell<T>,
14    /// Span of the node.
15    pub(crate) span: Span<I>,
16    /// Parent node. These exists because they are needed when performing range
17    /// queries such as [Tree::node_with_range][crate::Tree::node_with_range].
18    pub(crate) parent: Option<P>,
19    /// The previous node.
20    pub(crate) prev: Option<P>,
21    /// Next sibling node.
22    pub(crate) next: Option<P>,
23    /// First child node.
24    pub(crate) first: Option<P>,
25    /// Last child node.
26    pub(crate) last: Option<P>,
27}
28
29// These tests might not always pass, due to alignment. But it's nice to ensure
30#[test]
31fn test_size() {
32    macro_rules! test {
33        ($data:ty, $index:ty, $width:ty, $max_align:expr) => {
34            assert!(
35                (
36                    std::mem::size_of::<Links<$data, $index, <$width as crate::pointer::Width>::Pointer>>() as isize
37                    -
38                    (std::mem::size_of::<$data>() as isize + ((<$index>::BITS * 2) / 8) as isize + ((<$width>::BITS * 5) / 8) as isize)
39                ).abs() <= $max_align
40            );
41        }
42    }
43
44    test!([u8; 8], u32, u16, 4);
45    test!([u8; 8], u32, u32, 4);
46    test!([u8; 8], u32, u64, 4);
47    test!([u8; 8], u32, u128, 4);
48}