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
use core::cell::Ref;
use core::ops::Deref;
use rkyv::Archive;
use crate::{Compound, Primitive};
mod cardinality;
mod max_key;
mod unit;
pub use cardinality::{Cardinality, Nth};
pub use max_key::{FindMaxKey, Keyed, MaxKey, Member};
pub trait Annotation<Leaf>:
Default + Clone + Combine<Self> + Primitive
{
fn from_leaf(leaf: &Leaf) -> Self;
fn from_node<C, S>(node: &C) -> Self
where
C: Compound<Self, S, Leaf = Leaf>,
C::Leaf: Archive,
{
let mut a = Self::default();
for i in 0.. {
match node.child(i) {
crate::Child::Leaf(leaf) => a.combine(&Self::from_leaf(leaf)),
crate::Child::Link(link) => a.combine(&*link.annotation()),
crate::Child::Empty => (),
crate::Child::End => return a,
}
}
unreachable!()
}
}
pub trait Combine<A> {
fn combine(&mut self, with: &A);
}
#[derive(Debug)]
pub enum ARef<'a, A> {
Owned(A),
Borrowed(&'a A),
Referenced(Ref<'a, Option<A>>),
}
impl<'a, A> Deref for ARef<'a, A> {
type Target = A;
fn deref(&self) -> &Self::Target {
match self {
ARef::Owned(ref a) => a,
ARef::Borrowed(a) => *a,
ARef::Referenced(a) => a.as_ref().unwrap(),
}
}
}