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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use core::cell::Ref;
use core::ops::Deref;

use rkyv::Archive;

use crate::{Compound, Primitive};

mod cardinality;
mod max_key;
mod unit;

// re-exports
pub use cardinality::{Cardinality, Nth};
pub use max_key::{FindMaxKey, Keyed, MaxKey, Member};

/// The trait defining an annotation type over a leaf
pub trait Annotation<Leaf>:
    Default + Clone + Combine<Self> + Primitive
{
    /// Creates an annotation from the leaf type
    fn from_leaf(leaf: &Leaf) -> Self;

    /// Create an annotation from a node
    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!()
    }
}

/// Trait for defining how to combine Annotations
pub trait Combine<A> {
    /// Combines multiple annotations
    fn combine(&mut self, with: &A);
}

/// A wrapped annotation that is either owning its A or providing an annotated
/// link
#[derive(Debug)]
pub enum ARef<'a, A> {
    /// The annotation is owned
    Owned(A),
    /// The annotation is a reference
    Borrowed(&'a A),
    /// Referenced
    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(),
        }
    }
}