Skip to main content

cranpose_foundation/modifier/chain/
entry_element.rs

1// The element a chain entry took last, kept as it is, held by the entry's
2// node or written as words in the entry, and the view a comparison
3// with a new element reads.
4
5use std::{any::TypeId, cell::RefCell, rc::Rc};
6
7use super::super::{
8    AnyModifierElement, DynModifierElement, ElementMaker, ModifierNode, NodeCapabilities, NodeHeld,
9    NodeMaker, PlainElement, PlainFns,
10};
11
12#[derive(Debug)]
13pub(super) enum EntryElement {
14    Kept(DynModifierElement),
15    // The node keeps every value of the element: see
16    // `ModifierNodeElement::FROM_NODE`.
17    InNode(NodeHeld),
18}
19
20impl EntryElement {
21    // How an entry keeps `element`: by its node when the node holds every
22    // value of it.
23    pub(super) fn new(element: DynModifierElement, maker: &dyn NodeMaker) -> Self {
24        match maker.held_by_node() {
25            Some(held) if element.key().is_none() => Self::InNode(held),
26            _ => Self::Kept(element),
27        }
28    }
29
30    // The key of the element: an element its node holds has none.
31    pub(super) fn key(&self) -> Option<u64> {
32        match self {
33            Self::Kept(element) => element.key(),
34            Self::InNode(_) => None,
35        }
36    }
37
38    pub(super) fn view<'a>(
39        &'a self,
40        node: &'a RefCell<dyn ModifierNode>,
41        capabilities: NodeCapabilities,
42    ) -> HeldElement<'a> {
43        HeldElement(match self {
44            Self::Kept(element) => Held::Element(element),
45            Self::InNode(held) => Held::Node {
46                held: *held,
47                node,
48                capabilities,
49            },
50        })
51    }
52}
53
54/// An element a chain holds, as a comparison with a new element reads it:
55/// the element itself, or the node that keeps every value of it.
56#[derive(Clone, Copy)]
57pub struct HeldElement<'a>(Held<'a>);
58
59#[derive(Clone, Copy)]
60enum Held<'a> {
61    Element(&'a DynModifierElement),
62    Node {
63        held: NodeHeld,
64        node: &'a RefCell<dyn ModifierNode>,
65        capabilities: NodeCapabilities,
66    },
67    Plain {
68        fns: &'static PlainFns,
69        words: &'a [u32],
70    },
71}
72
73impl<'a> HeldElement<'a> {
74    pub(super) fn of_words(fns: &'static PlainFns, words: &'a [u32]) -> Self {
75        Self(Held::Plain { fns, words })
76    }
77}
78
79impl<'a> From<&'a DynModifierElement> for HeldElement<'a> {
80    fn from(element: &'a DynModifierElement) -> Self {
81        Self(Held::Element(element))
82    }
83}
84
85impl HeldElement<'_> {
86    /// The type of the element.
87    pub fn element_type(&self) -> TypeId {
88        match self.0 {
89            Held::Element(element) => element.element_type(),
90            Held::Node { held, .. } => held.element_type(),
91            Held::Plain { fns, .. } => fns.element_type(),
92        }
93    }
94
95    /// The type of the node the element makes: its own type for a plain
96    /// element, which makes none.
97    pub fn node_type(&self) -> TypeId {
98        match self.0 {
99            Held::Element(element) => match element.maker() {
100                ElementMaker::Node(maker) => maker.node_type(),
101                ElementMaker::Plain(value) => value.fns().element_type(),
102            },
103            Held::Node { held, .. } => held.node_type(),
104            Held::Plain { fns, .. } => fns.element_type(),
105        }
106    }
107
108    /// The capabilities of the element's node.
109    pub fn capabilities(&self) -> NodeCapabilities {
110        match self.0 {
111            Held::Element(element) => element.capabilities(),
112            Held::Node { capabilities, .. } => capabilities,
113            Held::Plain { fns, .. } => fns.capabilities(),
114        }
115    }
116
117    /// Whether the element asks for an update of its node even when equal:
118    /// one its node holds never does, nor does a plain one.
119    pub fn requires_update(&self) -> bool {
120        match self.0 {
121            Held::Element(element) => element.requires_update(),
122            Held::Node { .. } | Held::Plain { .. } => false,
123        }
124    }
125
126    /// Whether the chain keeps the element's values, in its node or as
127    /// words, in place of the element. Such an element asks for no update
128    /// when equal, and an element equal to it is of its type, so that one
129    /// asks for none either.
130    pub fn is_kept_by_value(&self) -> bool {
131        matches!(self.0, Held::Node { .. } | Held::Plain { .. })
132    }
133
134    /// Whether `element` is this element itself, not only an equal one.
135    pub fn is(&self, element: &DynModifierElement) -> bool {
136        match self.0 {
137            Held::Element(held) => Rc::ptr_eq(held, element),
138            Held::Node { .. } | Held::Plain { .. } => false,
139        }
140    }
141
142    /// Whether `element` is of this element's type with equal data.
143    pub fn equals(&self, element: &dyn AnyModifierElement) -> bool {
144        match self.0 {
145            Held::Element(held) => held.equals_element(element),
146            Held::Node { node, .. } => element.equals_node(&*node.borrow()),
147            Held::Plain { fns, words } => element.equals_plain(fns, words),
148        }
149    }
150
151    /// The element's value, when the chain keeps it as words of an `E`.
152    #[inline]
153    pub fn plain<E: PlainElement>(&self) -> Option<E> {
154        match self.0 {
155            Held::Plain { fns, words } => fns.read(words),
156            Held::Element(_) | Held::Node { .. } => None,
157        }
158    }
159
160    /// The hash of the element, as the element itself hashes.
161    pub fn hash_code(&self) -> u64 {
162        match self.0 {
163            Held::Element(element) => element.hash_code(),
164            Held::Node { held, node, .. } => held.hash_code(&*node.borrow()),
165            Held::Plain { fns, words } => fns.hash_code(words),
166        }
167    }
168
169    /// The element, made again from its node when the node holds it.
170    pub fn to_element(&self) -> DynModifierElement {
171        match self.0 {
172            Held::Element(element) => Rc::clone(element),
173            Held::Node { held, node, .. } => held.element(&*node.borrow()),
174            Held::Plain { fns, words } => fns.element(words),
175        }
176    }
177}