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
159
160
161
162
163
use core::fmt::{Debug, Formatter};
use crate::Storage;
/// Entries in a node's `children` array.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Child<Node, T> {
/// Child containing another node.
Path(Node),
/// Path-compressed single descendant.
///
/// If a prefix would be inserted that spans multiple octet strides and
/// there is no child for the starting octet, it is instead stored as a
/// single leaf. This is the primary mechanism for path compression.
Leaf {
/// The complete prefix for this route.
prefix: ipnet::IpNet,
/// Value stored for this route.
value: T,
},
/// Fringe nodes store single /8s for this depth in the trie.
///
/// This can be seen as an optimization over [`Leaf`][Self::Leaf]
/// for cases where `prefix` lies on this depth's `/8` boundary.
Fringe(T),
}
impl<Node, T> Debug for Child<Node, T>
where
Node: Debug,
T: Debug,
{
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Self::Path(inner) => inner.fmt(f),
Self::Leaf { prefix, value } => {
write!(f, "Leaf({prefix}: {value:?})")
}
Self::Fringe(fringe) => f.debug_tuple("Fringe").field(fringe).finish(),
}
}
}
impl<Node, T> Child<&Node, &T> {
/// When this holds refs, clone the values in the refs to return a
/// child-of-owned.
#[inline]
pub fn cloned(self) -> Child<Node, T>
where
Node: Clone,
T: Clone,
{
match self {
Self::Path(n) => Child::Path(n.clone()),
Self::Leaf { prefix, value } => Child::Leaf {
prefix,
value: value.clone(),
},
Self::Fringe(value) => Child::Fringe(value.clone()),
}
}
}
impl<Node, T> Child<Node, T> {
/// Convert this ref-to-child into a child-of-ref.
#[inline]
pub const fn as_ref(&self) -> Child<&Node, &T> {
match self {
Self::Path(node) => Child::Path(node),
Self::Leaf { prefix, value } => Child::Leaf {
prefix: *prefix,
value,
},
Self::Fringe(t) => Child::Fringe(t),
}
}
/// Convert this ref-mut-to-child into a child-of-ref-mut.
#[inline]
pub const fn as_mut(&mut self) -> Child<&mut Node, &mut T> {
match self {
Self::Path(node) => Child::Path(node),
Self::Leaf { prefix, value } => Child::Leaf {
prefix: *prefix,
value,
},
Self::Fringe(t) => Child::Fringe(t),
}
}
/// If this is a path node, apply `f` to the contained value.
#[inline]
pub fn map_node<Nu>(self, f: impl FnOnce(Node) -> Nu) -> Child<Nu, T> {
match self {
Self::Path(node) => Child::Path(f(node)),
Self::Leaf { prefix, value } => Child::Leaf { prefix, value },
Self::Fringe(t) => Child::Fringe(t),
}
}
/// Get the value directly contained in this node, if it's a leaf or fringe.
/// Return `None` iff this is a [`Path`][Child::Path].
#[inline]
pub fn into_value(self) -> Option<T> {
match self {
Child::Leaf { value, .. } | Child::Fringe(value) => Some(value),
Child::Path(_) => None,
}
}
/// When `Node` is a [`Storage`], this is [`as_ref`][Self::as_ref] except
/// that it also unwraps the node container type.
#[inline]
pub fn as_node_ref<C, Inner>(&self) -> Child<&Inner, &T>
where
C: Storage<Container<Inner> = Node> + ?Sized,
{
self.as_ref().map_node(C::as_ref)
}
/// When `Node` is a [`Storage`], this is [`as_mut`][Self::as_mut] except
/// that it also unwraps the node container type.
#[inline]
pub fn as_node_mut<C, Inner>(&mut self) -> Child<&mut Inner, &mut T>
where
C: Storage<Container<Inner> = Node> + ?Sized,
{
self.as_mut().map_node(C::as_mut)
}
/// Return the child node if this is a [`Path`][Child::Path].
#[inline]
pub fn into_node(self) -> Option<Node> {
match self {
Child::Path(node) => Some(node),
_ => None,
}
}
/// Construct a default leaf value for testing.
#[cfg(test)]
pub fn dummy_leaf() -> Self
where
T: Default,
{
Self::Leaf {
prefix: Default::default(),
value: Default::default(),
}
}
/// Construct a default fringe value for testing.
#[cfg(test)]
pub fn dummy_fringe() -> Self
where
T: Default,
{
Self::Fringe(Default::default())
}
}