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