Skip to main content

cranpose_core/
applier.rs

1use std::any::{Any, TypeId};
2
3use crate::{Node, NodeError, NodeId, RecycleOrigin, RecycledNode, RecycledNodeInsertion};
4
5/// Stores the nodes that a composition builds and applies changes to them. The UI layer supplies an
6/// applier; [`MemoryApplier`](crate::MemoryApplier) is the stock one.
7pub trait Applier: Any {
8    /// Stores `node` and returns its new id.
9    fn create(&mut self, node: Box<dyn Node>) -> NodeId;
10    /// Returns the node `id` for change, or an error if there is none.
11    fn get_mut(&mut self, id: NodeId) -> Result<&mut dyn Node, NodeError>;
12    /// Returns the node `id` to read, or an error if there is none.
13    fn get_ref(&self, id: NodeId) -> Result<&dyn Node, NodeError>;
14    /// Removes the node `id` and the nodes below it.
15    fn remove(&mut self, id: NodeId) -> Result<(), NodeError>;
16
17    /// Opens a batch of commands, in which no layout pass runs: within it a
18    /// dirty bubble may stop at an ancestor an earlier one passed
19    /// ([`Self::note_bubble_pass`]). Batches nest.
20    fn begin_bubble_batch(&mut self) {}
21
22    /// Notes that a dirty bubble marked `id` as needing measure or layout,
23    /// or carried the dirt of `id` to its ancestors, for a layout pass that
24    /// starts from the dirty nodes. An applier that keeps no notes ignores
25    /// it.
26    fn note_layout_dirt(&mut self, _id: NodeId) {}
27
28    /// Closes the batch [`Self::begin_bubble_batch`] opened.
29    fn end_bubble_batch(&mut self) {}
30
31    /// Forgets what the open batch's bubbles passed: a node changed its
32    /// parent, or other code ran, so their marks may no longer reach the root.
33    fn forget_bubble_passes(&mut self) {}
34
35    /// The flags of the dirty bubbles that passed `id` in the open batch,
36    /// for a caller that gives `id` a parent and carries them on from it.
37    /// `None` when the applier cannot tell: the caller then forgets the
38    /// batch's passes.
39    fn bubble_passes_of(&mut self, _id: NodeId) -> Option<u8> {
40        None
41    }
42
43    /// Notes that a dirty bubble carrying `flags` passed the ancestor `id`.
44    /// `true` when one carrying all of them passed it earlier in the open
45    /// batch: that one marked every ancestor above `id`, so this one may
46    /// stop. An applier that keeps no notes answers `false`.
47    fn note_bubble_pass(&mut self, _id: NodeId, _flags: u8) -> bool {
48        false
49    }
50
51    /// The node `id` for a dirty bubble to mark, or `None` when it is
52    /// missing or, with `note`, when [`Self::note_bubble_pass`] says a
53    /// bubble carrying `flags` passed it earlier in the open batch.
54    fn bubble_node(&mut self, id: NodeId, flags: u8, note: bool) -> Option<&mut dyn Node> {
55        if note && self.note_bubble_pass(id, flags) {
56            return None;
57        }
58        self.get_mut(id).ok()
59    }
60
61    /// Records that `parent_id`'s child list changed structurally this frame
62    /// (insert, remove, move, or reparent). Incremental scene consumers drain
63    /// the recorded parents and re-patch those subtrees so removed nodes are
64    /// evicted from a persistent render graph even when the frame's scene
65    /// update is otherwise scoped to unrelated dirty nodes.
66    fn record_structural_change(&mut self, _parent_id: NodeId) {}
67
68    /// Returns the current generation for a node index.
69    /// Generation is incremented when an index is reused from the freelist,
70    /// preventing stale slot entries from matching recycled nodes.
71    fn node_generation(&self, id: NodeId) -> u32;
72
73    /// Reinserts a recycled node at its retained stable ID, or creates a fresh ID if that
74    /// retained ID is no longer available.
75    fn insert_recycled_node_or_create(
76        &mut self,
77        stable_id: NodeId,
78        node: Box<dyn Node>,
79    ) -> RecycledNodeInsertion {
80        let id = self.create(node);
81        RecycledNodeInsertion::fresh(id, Some(NodeError::AlreadyExists { id: stable_id }))
82    }
83
84    /// Returns the applier as `Any`, for downcasting to its concrete type.
85    fn as_any(&self) -> &dyn Any
86    where
87        Self: Sized,
88    {
89        self
90    }
91
92    /// Returns the applier as `Any`, for downcasting to its concrete type.
93    fn as_any_mut(&mut self) -> &mut dyn Any
94    where
95        Self: Sized,
96    {
97        self
98    }
99
100    /// Trim trailing tombstones/unused capacity after structural changes.
101    fn compact(&mut self) {}
102
103    /// Returns a previously recycled node shell and its stable ID for the requested concrete type.
104    fn take_recycled_node(&mut self, _key: TypeId) -> Option<RecycledNode> {
105        None
106    }
107
108    /// Marks whether a reinserted recycled node originated from the warm recycle path.
109    fn set_recycled_node_origin(&mut self, _id: NodeId, _origin: RecycleOrigin) {}
110
111    /// Seeds a warm recyclable shell for future reuse without requiring a prior removal.
112    fn seed_recycled_node_shell(
113        &mut self,
114        _key: TypeId,
115        _recycle_pool_limit: Option<usize>,
116        _shell: Box<dyn Node>,
117    ) {
118    }
119
120    /// Records that the current apply pass had to allocate a fresh recyclable shell for this type.
121    fn record_fresh_recyclable_creation(&mut self, _key: TypeId) {}
122
123    /// Drops any recyclable shells that should not survive beyond the current apply pass.
124    fn clear_recycled_nodes(&mut self) {}
125}