cranpose_core/slot/
table.rs1use std::rc::Rc;
2
3use super::{
4 AnchorRegistry, DeferredDrop, GroupRecord, MovableIndex, NodeRecord, PayloadAnchorRegistry,
5 SlotLifecycleCoordinator, SlotWriteSessionState, debug::SlotTableDiagnostics,
6 growth::GrowthSlack, payload_store::PayloadStore,
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: PayloadStore,
38 pub(super) nodes: Vec<NodeRecord>,
39 pub(super) anchors: AnchorRegistry,
40 pub(super) payload_anchors: PayloadAnchorRegistry,
41 pub(super) movables: MovableIndex,
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: PayloadStore::default(),
52 nodes: Vec::new(),
53 anchors: AnchorRegistry::new(),
54 payload_anchors: PayloadAnchorRegistry::new(),
55 movables: MovableIndex::default(),
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.compact();
95 self.nodes.shrink_to_fit();
96 self.anchors.shrink_to_fit();
97 self.payload_anchors.shrink_to_fit();
98 self.movables.shrink_to_fit();
99 }
100
101 pub(crate) fn storage_capacity(&self) -> usize {
102 self.groups.capacity() + self.payloads.capacity() + self.nodes.capacity()
103 }
104
105 pub(crate) fn trim_growth_slack(&mut self) {
106 self.groups.trim_growth_slack();
107 self.payloads.trim_growth_slack();
108 self.nodes.trim_growth_slack();
109 self.anchors.trim_growth_slack();
110 self.payload_anchors.trim_growth_slack();
111 }
112
113 pub(crate) fn take_effect_drops(&mut self) -> Vec<DeferredDrop> {
114 let mut drops = Vec::new();
115 self.payloads.for_each_mut(|payload| {
116 if let Some(fresh) = payload.payload_type.fresh {
117 let old = std::mem::replace(&mut payload.value, fresh());
118 drops.push(DeferredDrop::payload(old));
119 }
120 });
121 drops
122 }
123
124 pub(crate) fn take_all_drops(&mut self) -> Vec<DeferredDrop> {
125 let mut drops = Vec::with_capacity(self.payloads.len());
126 self.payloads
127 .drain_rev(|payload| drops.push(payload.into_deferred_drop()));
128 self.groups.clear();
129 self.nodes.clear();
130 self.anchors.clear();
131 self.payload_anchors.clear();
132 self.movables.clear();
133 drops
134 }
135}
136
137impl Default for SlotTable {
138 fn default() -> Self {
139 Self::new()
140 }
141}