cranpose_core/slot/
table.rs1use std::rc::Rc;
2
3use super::{
4 debug::SlotTableDiagnostics, AnchorRegistry, DeferredDrop, GroupRecord, NodeRecord,
5 PayloadAnchorRegistry, PayloadRecord, ScopeIndex, SlotLifecycleCoordinator,
6 SlotWriteSessionState,
7};
8
9mod metadata;
10mod mutation;
11mod values;
12
13#[cfg(test)]
14pub(crate) use values::ValueSlotError;
15
16struct SlotStorageIdentity(Rc<()>);
17
18impl SlotStorageIdentity {
19 fn new() -> Self {
20 Self(Rc::new(()))
21 }
22
23 fn id(&self) -> usize {
24 Rc::as_ptr(&self.0).addr()
25 }
26}
27
28pub(crate) struct SlotWriteSession<'a> {
29 pub(super) table: &'a mut SlotTable,
30 pub(in crate::slot) lifecycle: &'a mut SlotLifecycleCoordinator,
31 pub(in crate::slot) state: &'a mut SlotWriteSessionState,
32}
33
34pub struct SlotTable {
35 storage_id: SlotStorageIdentity,
36 pub(super) groups: Vec<GroupRecord>,
37 pub(super) payloads: Vec<PayloadRecord>,
38 pub(super) nodes: Vec<NodeRecord>,
39 pub(super) anchors: AnchorRegistry,
40 pub(super) payload_anchors: PayloadAnchorRegistry,
41 pub(super) scope_index: ScopeIndex,
42 pub(super) diagnostics: SlotTableDiagnostics,
43 next_group_generation: u32,
44}
45
46impl SlotTable {
47 pub fn new() -> Self {
48 Self {
49 storage_id: SlotStorageIdentity::new(),
50 groups: Vec::new(),
51 payloads: Vec::new(),
52 nodes: Vec::new(),
53 anchors: AnchorRegistry::new(),
54 payload_anchors: PayloadAnchorRegistry::new(),
55 scope_index: ScopeIndex::new(),
56 diagnostics: SlotTableDiagnostics::default(),
57 next_group_generation: 1,
58 }
59 }
60
61 pub(crate) fn write_session<'a>(
62 &'a mut self,
63 lifecycle: &'a mut SlotLifecycleCoordinator,
64 state: &'a mut SlotWriteSessionState,
65 ) -> SlotWriteSession<'a> {
66 SlotWriteSession {
67 table: self,
68 lifecycle,
69 state,
70 }
71 }
72
73 pub(crate) fn storage_id(&self) -> usize {
74 self.storage_id.id()
75 }
76
77 #[cfg(test)]
78 pub(in crate::slot) fn set_next_group_generation_for_test(&mut self, generation: u32) {
79 self.next_group_generation = generation;
80 }
81
82 #[cfg(test)]
83 pub(in crate::slot) fn set_next_group_anchor_for_test(&mut self, next_anchor: usize) {
84 self.anchors.set_next_anchor_for_test(next_anchor);
85 }
86
87 #[cfg(test)]
88 pub(in crate::slot) fn set_next_payload_anchor_for_test(&mut self, next_anchor: usize) {
89 self.payload_anchors.set_next_id_for_test(next_anchor);
90 }
91
92 pub(crate) fn compact_storage(&mut self) {
93 self.groups.shrink_to_fit();
94 self.payloads.shrink_to_fit();
95 self.nodes.shrink_to_fit();
96 self.anchors.shrink_to_fit();
97 self.payload_anchors.shrink_to_fit();
98 self.scope_index.shrink_to_fit();
99 }
100
101 pub(crate) fn take_effect_drops(&mut self) -> Vec<DeferredDrop> {
102 let mut drops = Vec::new();
103 for payload in self.payloads.iter_mut() {
104 let Some(fresh) = payload.fresh else {
105 continue;
106 };
107 let old = std::mem::replace(&mut payload.value, fresh());
108 drops.push(DeferredDrop::payload(old));
109 }
110 drops
111 }
112
113 pub(crate) fn take_all_drops(&mut self) -> Vec<DeferredDrop> {
114 let payload_count = self.payloads.len();
115 let mut drops = Vec::with_capacity(payload_count);
116 for payload in self.payloads.drain(..).rev() {
117 drops.push(payload.into_deferred_drop());
118 }
119 self.groups.clear();
120 self.nodes.clear();
121 self.anchors.clear();
122 self.payload_anchors.clear();
123 self.scope_index.clear();
124 drops
125 }
126}
127
128impl Default for SlotTable {
129 fn default() -> Self {
130 Self::new()
131 }
132}