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, PlainValue, Repr,
10};
11
12#[derive(Debug)]
13pub(super) enum EntryElement {
14    Kept(Rc<dyn AnyModifierElement>),
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.to_shared()),
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 Rc<dyn AnyModifierElement>),
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(match element.repr() {
82            Repr::Plain(value) => Held::Plain {
83                fns: value.fns(),
84                words: value.words(),
85            },
86            Repr::Shared { element, .. } => Held::Element(element),
87        })
88    }
89}
90
91impl HeldElement<'_> {
92    /// The type of the element.
93    pub fn element_type(&self) -> TypeId {
94        match self.0 {
95            Held::Element(element) => element.element_type(),
96            Held::Node { held, .. } => held.element_type(),
97            Held::Plain { fns, .. } => fns.element_type(),
98        }
99    }
100
101    /// The type of the node the element makes: its own type for a plain
102    /// element, which makes none.
103    pub fn node_type(&self) -> TypeId {
104        match self.0 {
105            Held::Element(element) => match element.maker() {
106                ElementMaker::Node(maker) => maker.node_type(),
107                ElementMaker::Plain(value) => value.fns().element_type(),
108            },
109            Held::Node { held, .. } => held.node_type(),
110            Held::Plain { fns, .. } => fns.element_type(),
111        }
112    }
113
114    /// The capabilities of the element's node.
115    pub fn capabilities(&self) -> NodeCapabilities {
116        match self.0 {
117            Held::Element(element) => element.capabilities(),
118            Held::Node { capabilities, .. } => capabilities,
119            Held::Plain { fns, .. } => fns.capabilities(),
120        }
121    }
122
123    /// Whether the element asks for an update of its node even when equal:
124    /// one its node holds never does, nor does a plain one.
125    pub fn requires_update(&self) -> bool {
126        match self.0 {
127            Held::Element(element) => element.requires_update(),
128            Held::Node { .. } | Held::Plain { .. } => false,
129        }
130    }
131
132    /// Whether the chain keeps the element's values, in its node or as
133    /// words, in place of the element. Such an element asks for no update
134    /// when equal, and an element equal to it is of its type, so that one
135    /// asks for none either.
136    pub fn is_kept_by_value(&self) -> bool {
137        matches!(self.0, Held::Node { .. } | Held::Plain { .. })
138    }
139
140    /// Whether `element` is this element itself, not only an equal one.
141    pub fn is(&self, element: &DynModifierElement) -> bool {
142        match self.0 {
143            Held::Element(held) => element
144                .shared()
145                .is_some_and(|shared| Rc::ptr_eq(held, shared)),
146            Held::Node { .. } | Held::Plain { .. } => false,
147        }
148    }
149
150    /// Whether `element` is this element itself, or one equal to it that
151    /// asks for no update.
152    #[inline]
153    pub fn matches(&self, element: &DynModifierElement) -> bool {
154        // The two kinds that most pairs are compare directly: decoding the
155        // element once each way and calling through `dyn` cost a text's set
156        // a third more on the gauntlet.
157        match (self.0, element.repr()) {
158            (Held::Plain { fns, words }, Repr::Plain(value)) => value.equals_plain(fns, words),
159            (
160                Held::Element(held),
161                Repr::Shared {
162                    element: shared, ..
163                },
164            ) => {
165                Rc::ptr_eq(held, shared)
166                    || (!held.requires_update()
167                        && !shared.requires_update()
168                        && held.equals_element(&**shared))
169            }
170            _ => !self.requires_update() && !element.requires_update() && self.equals(&**element),
171        }
172    }
173
174    /// Whether `element` is of this element's type with equal data.
175    pub fn equals(&self, element: &dyn AnyModifierElement) -> bool {
176        match self.0 {
177            Held::Element(held) => held.equals_element(element),
178            Held::Node { node, .. } => element.equals_node(&*node.borrow()),
179            Held::Plain { fns, words } => element.equals_plain(fns, words),
180        }
181    }
182
183    /// The element's value, when the chain keeps it as words of an `E`.
184    #[inline]
185    pub fn plain<E: PlainElement>(&self) -> Option<E> {
186        match self.0 {
187            Held::Plain { fns, words } => fns.read(words),
188            Held::Element(_) | Held::Node { .. } => None,
189        }
190    }
191
192    /// The hash of the element, as the element itself hashes.
193    pub fn hash_code(&self) -> u64 {
194        match self.0 {
195            Held::Element(element) => element.hash_code(),
196            Held::Node { held, node, .. } => held.hash_code(&*node.borrow()),
197            Held::Plain { fns, words } => fns.hash_code(words),
198        }
199    }
200
201    /// The element, made again from its node when the node holds it.
202    pub fn to_element(&self) -> DynModifierElement {
203        match self.0 {
204            Held::Element(element) => DynModifierElement::of_shared(Rc::clone(element)),
205            Held::Node { held, node, .. } => held.element(&*node.borrow()),
206            Held::Plain { fns, words } => fns.element(words),
207        }
208    }
209}