Skip to main content

cranpose_core/
recycled_node.rs

1use crate::{Node, NodeError, NodeId};
2
3/// Which recycle path a pooled node came from.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum RecycleOrigin {
6    /// A node created fresh, or one kept from before the last pass.
7    #[default]
8    Cold,
9    /// A node kept warm from the last pass.
10    Warm,
11}
12
13/// The node that [`Composer::emit_recyclable_node`](crate::Composer::emit_recyclable_node) hands
14/// its `reset`: where it comes from.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum RecyclableShell {
17    /// A bare node that `init` made in a new box.
18    Fresh,
19    /// A removed node of the same type that the recycle pool kept.
20    Pooled,
21}
22
23/// A removed node kept in a pool with its old id, so that a later node of the same type can reuse
24/// it.
25pub struct RecycledNode {
26    stable_id: NodeId,
27    pub(crate) node: Box<dyn Node>,
28    origin: RecycleOrigin,
29}
30
31impl RecycledNode {
32    /// A removed node as a pool shell: the node cleared where it lies, so it
33    /// keeps its box and takes no new one.
34    pub(crate) fn new(stable_id: NodeId, mut node: Box<dyn Node>, origin: RecycleOrigin) -> Self {
35        node.prepare_for_recycle();
36        Self {
37            stable_id,
38            node,
39            origin,
40        }
41    }
42
43    pub(crate) fn from_shell(
44        stable_id: NodeId,
45        node: Box<dyn Node>,
46        origin: RecycleOrigin,
47    ) -> Self {
48        Self {
49            stable_id,
50            node,
51            origin,
52        }
53    }
54
55    /// Returns the id the node had before it was recycled.
56    pub fn stable_id(&self) -> NodeId {
57        self.stable_id
58    }
59
60    pub(crate) fn origin(&self) -> RecycleOrigin {
61        self.origin
62    }
63
64    pub(crate) fn set_origin(&mut self, origin: RecycleOrigin) {
65        self.origin = origin;
66    }
67
68    /// Returns the pooled node, so that it can be reset before reuse.
69    pub fn node_mut(&mut self) -> &mut dyn Node {
70        self.node.as_mut()
71    }
72
73    /// Splits the shell into its stable id, its node and the recycle path it came from.
74    pub fn into_parts(self) -> (NodeId, Box<dyn Node>, RecycleOrigin) {
75        (self.stable_id, self.node, self.origin)
76    }
77}
78
79/// The result of putting a recycled node back into an applier.
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub struct RecycledNodeInsertion {
82    /// The id the node has now.
83    pub id: NodeId,
84    /// Whether the node got its old stable id back.
85    pub stable_id_reused: bool,
86    /// The error that forced a new id, or `None` if the old id was reused.
87    pub fallback_error: Option<NodeError>,
88}
89
90impl RecycledNodeInsertion {
91    pub(crate) fn reused(stable_id: NodeId) -> Self {
92        Self {
93            id: stable_id,
94            stable_id_reused: true,
95            fallback_error: None,
96        }
97    }
98
99    pub(crate) fn fresh(id: NodeId, fallback_error: Option<NodeError>) -> Self {
100        Self {
101            id,
102            stable_id_reused: false,
103            fallback_error,
104        }
105    }
106}