1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use super::node_refs::NodeRefs;
use crate::{variants::variant::Variant, Node};

/// An empty single node, used to represent cases where the node does not hold any reference.
///
/// The following are some example references which can be expressed by `NodeRefNone`:
///
/// * previous node in a singly linked list;
/// * parent of a node in a tree where bottom up traversal is not necessary.
#[derive(Default, Clone, Debug)]
pub struct NodeRefNone(());

impl<'a, V, T> NodeRefs<'a, V, T> for NodeRefNone
where
    V: Variant<'a, T>,
{
    type References = ();

    #[inline(always)]
    fn new(_: Self::References) -> Self {
        Self(())
    }

    #[inline(always)]
    fn get(&self) -> &Self::References {
        &self.0
    }

    #[inline(always)]
    fn get_mut(&mut self) -> &mut Self::References {
        &mut self.0
    }

    #[inline(always)]
    fn update_reference(&mut self, _: &'a Node<'a, V, T>, _: &'a Node<'a, V, T>) {}

    #[inline(always)]
    fn referenced_nodes(&self) -> impl Iterator<Item = &'a Node<'a, V, T>>
    where
        V: 'a,
        T: 'a,
    {
        std::iter::empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{MemoryReclaimNever, NodeData, NodeDataLazyClose};

    #[derive(Debug, Clone, Copy)]
    struct Var;
    impl<'a> Variant<'a, char> for Var {
        type Storage = NodeDataLazyClose<char>;
        type Prev = NodeRefNone;
        type Next = NodeRefNone;
        type Ends = NodeRefNone;
        type MemoryReclaim = MemoryReclaimNever;
    }

    #[test]
    fn new_default() {
        let _new = <NodeRefNone as NodeRefs<'_, Var, char>>::new(());
        let _default = NodeRefNone::default();
    }

    #[test]
    fn get() {
        let nr = <NodeRefNone as NodeRefs<'_, Var, char>>::new(());
        assert_eq!(&(), <NodeRefNone as NodeRefs<'_, Var, char>>::get(&nr));
    }

    #[test]
    fn get_mut() {
        let mut nr = <NodeRefNone as NodeRefs<'_, Var, char>>::new(());
        assert_eq!(
            &mut (),
            <NodeRefNone as NodeRefs<'_, Var, char>>::get_mut(&mut nr)
        );
    }

    #[test]
    fn update_reference() {
        let data = NodeDataLazyClose::active('a');
        let a = Node::<'_, Var, _>::new(data, Default::default(), Default::default());

        let data = NodeDataLazyClose::active('b');
        let b = Node::<'_, Var, _>::new(data, Default::default(), Default::default());

        let mut with_ref = <NodeRefNone as NodeRefs<'_, Var, char>>::new(());
        assert_eq!(
            0,
            <NodeRefNone as NodeRefs<'_, Var, char>>::referenced_nodes(&with_ref).count()
        );

        with_ref.update_reference(&a, &b);

        assert_eq!(
            0,
            <NodeRefNone as NodeRefs<'_, Var, char>>::referenced_nodes(&with_ref).count()
        );
    }

    #[test]
    fn referenced_nodes() {
        let with_ref = <NodeRefNone as NodeRefs<'_, Var, char>>::new(());
        assert_eq!(
            0,
            <NodeRefNone as NodeRefs<'_, Var, char>>::referenced_nodes(&with_ref).count()
        );
    }
}