cranpose_core/
recycled_node.rs1use crate::{Node, NodeError, NodeId};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum RecycleOrigin {
6 #[default]
8 Cold,
9 Warm,
11}
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum RecyclableShell {
17 Fresh,
19 Pooled,
21}
22
23pub struct RecycledNode {
26 stable_id: NodeId,
27 pub(crate) node: Box<dyn Node>,
28 origin: RecycleOrigin,
29}
30
31impl RecycledNode {
32 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 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 pub fn node_mut(&mut self) -> &mut dyn Node {
70 self.node.as_mut()
71 }
72
73 pub fn into_parts(self) -> (NodeId, Box<dyn Node>, RecycleOrigin) {
75 (self.stable_id, self.node, self.origin)
76 }
77}
78
79#[derive(Debug, Clone, PartialEq, Eq)]
81pub struct RecycledNodeInsertion {
82 pub id: NodeId,
84 pub stable_id_reused: bool,
86 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}