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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use super::node_data::NodeData;

/// Node data storage with lazy closure.
/// In other words, the value is stored as it is and closing of a node means setting it to None.
#[derive(Clone)]
pub struct NodeDataLazyClose<T>(Option<T>);

impl<T> NodeData<T> for NodeDataLazyClose<T> {
    #[inline(always)]
    fn active(value: T) -> Self {
        Self(Some(value))
    }

    #[inline(always)]
    fn get(&self) -> Option<&T> {
        self.0.as_ref()
    }

    #[inline(always)]
    fn is_active(&self) -> bool {
        self.0.is_some()
    }

    #[inline(always)]
    fn get_mut(&mut self) -> Option<&mut T> {
        self.0.as_mut()
    }

    #[inline(always)]
    fn swap_data(&mut self, new_value: T) -> T {
        let output = self.0.take();
        self.0 = Some(new_value);
        output.expect("NodeDataLazyClose must be `is_active` to be able to `swap_data`")
    }
}

impl<T> NodeDataLazyClose<T> {
    /// Creates a new closed node data.
    #[inline(always)]
    pub fn closed() -> Self {
        Self(None)
    }

    /// Returns whether or not the node data is active.
    #[inline(always)]
    pub fn is_active(&self) -> bool {
        self.0.is_some()
    }

    /// Returns whether or not the node data is closed.
    #[inline(always)]
    pub fn is_closed(&self) -> bool {
        self.0.is_none()
    }

    /// Closes the node storage and returns the internally stored value.
    #[inline(always)]
    pub fn close(&mut self) -> Option<T> {
        self.0.take()
    }
}

impl<T> Default for NodeDataLazyClose<T> {
    #[inline(always)]
    fn default() -> Self {
        Self::closed()
    }
}

impl<T> From<T> for NodeDataLazyClose<T> {
    #[inline(always)]
    fn from(value: T) -> Self {
        Self(Some(value))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn active() {
        let data = NodeDataLazyClose::active(42);
        assert_eq!(Some(42), data.0);
    }

    #[test]
    fn from() {
        let data: NodeDataLazyClose<_> = 'x'.into();
        assert_eq!(Some('x'), data.0);
    }

    #[test]
    fn get() {
        let data = NodeDataLazyClose::active(42);
        assert_eq!(Some(&42), data.get());
    }

    #[test]
    fn get_mut() {
        let mut data = NodeDataLazyClose::active(42);
        *data.get_mut().unwrap() = 7;
        assert_eq!(Some(&7), data.get());
    }

    #[test]
    fn swap_data() {
        let mut data = NodeDataLazyClose::active(42);
        let old = data.swap_data(7);
        assert_eq!(Some(&7), data.get());
        assert_eq!(42, old);
    }

    #[test]
    #[should_panic]
    fn swap_data_of_closed() {
        let mut data = NodeDataLazyClose::closed();
        let _old = data.swap_data(7);
    }

    #[test]
    fn closed() {
        let data = NodeDataLazyClose::<char>::closed();
        assert_eq!(None, data.0);

        let data = NodeDataLazyClose::<char>::default();
        assert_eq!(None, data.0);
    }

    #[test]
    fn is_active() {
        let data = NodeDataLazyClose::active(42);
        assert!(data.is_active());

        let data = NodeDataLazyClose::<char>::closed();
        assert!(!data.is_active());
    }

    #[test]
    fn is_closed() {
        let data = NodeDataLazyClose::active(42);
        assert!(!data.is_closed());

        let data = NodeDataLazyClose::<char>::closed();
        assert!(data.is_closed());
    }

    #[test]
    fn close() {
        let mut data = NodeDataLazyClose::active(42);
        assert_eq!(Some(42), data.close());
        assert!(data.is_closed());

        let mut data = NodeDataLazyClose::<char>::closed();
        assert_eq!(None, data.close());
    }
}