orx_selfref_col/references/
single.rs

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
use super::{refs::Refs, NodePtr};
use crate::variant::Variant;
use core::fmt::Debug;

/// A single node reference.
pub struct RefsSingle<V>(Option<NodePtr<V>>)
where
    V: Variant;

impl<V: Variant> Clone for RefsSingle<V> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<V: Variant> Debug for RefsSingle<V> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_tuple("RefsSingle").field(&self.0).finish()
    }
}

impl<V: Variant> Refs for RefsSingle<V> {
    #[inline(always)]
    fn empty() -> Self {
        Self(None)
    }

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

    #[inline(always)]
    fn clear(&mut self) {
        _ = self.0.take();
    }

    #[inline(always)]
    fn remove_at(&mut self, ref_idx: usize) {
        assert_eq!(
            ref_idx, 0,
            "Reference idx {} is out of bounds for RefsSingle.",
            ref_idx
        );
        self.clear();
    }

    #[inline(always)]
    fn remove(&mut self, ptr: usize) -> Option<usize> {
        match &mut self.0 {
            None => None,
            Some(x) => match x.ptr() as usize == ptr {
                true => {
                    self.clear();
                    Some(0)
                }
                false => None,
            },
        }
    }
}

impl<V: Variant> RefsSingle<V> {
    /// Returns the pointer to the referenced node.
    pub fn get(&self) -> Option<&NodePtr<V>> {
        self.0.as_ref()
    }

    /// Sets the pointer to the referenced node with the given `node_idx`.
    pub fn set(&mut self, node_idx: Option<NodePtr<V>>) {
        self.0 = node_idx
    }

    /// Sets the pointer to the referenced node with the given `node_idx`.
    pub fn set_some(&mut self, node_idx: NodePtr<V>) {
        self.0 = Some(node_idx)
    }

    /// Un-sets the reference.
    pub fn set_none(&mut self) {
        self.0 = None
    }
}