dope_core/driver/route/table/
borrows.rs1use external::errors;
2use o3::depot::{
3 arena::slab::{self, external},
4 capacity,
5};
6use table::entries::{occupied, vacant};
7
8use crate::driver::{
9 route,
10 route::{indices, table, table::conn},
11};
12
13#[repr(transparent)]
14pub struct View<'a, T, Tag: route::Tag> {
15 inner: &'a table::Storage<T, Tag>,
16}
17
18impl<'a, T, Tag: route::Tag> View<'a, T, Tag> {
19 pub(in crate::driver::route::table) fn new(inner: &'a table::Storage<T, Tag>) -> Self {
20 Self { inner }
21 }
22
23 pub fn capacity(&self) -> table::Capacity {
25 table::Capacity(self.inner.capacity())
26 }
27
28 pub fn len(&self) -> capacity::Count {
29 self.inner.len()
30 }
31
32 pub fn available(&self) -> capacity::Count {
37 let total = self.inner.capacity();
38 let occupied = self.inner.len();
39 capacity::Count::new(total.raw() - occupied.raw())
40 }
41
42 pub fn is_empty(&self) -> bool {
43 self.inner.is_empty()
44 }
45
46 pub fn next_generation(&self) -> Option<route::Epoch> {
47 self.inner.next_generation()
48 }
49
50 pub fn get(self, parts: table::Parts<Tag>) -> Option<&'a T> {
51 self.inner.get(parts.external())
52 }
53
54 pub fn contains(self, parts: table::Parts<Tag>) -> bool {
55 self.inner.get(parts.external()).is_some()
56 }
57
58 pub fn key_at(self, slot: indices::SlotIndex) -> Option<table::Key<Tag>> {
59 self.inner.key_at(slot.raw()).map(table::Key::new)
60 }
61}
62
63#[repr(transparent)]
64pub struct Access<'a, T, Tag: route::Tag>(&'a mut table::Storage<T, Tag>);
65
66impl<'a, T, Tag: route::Tag> Access<'a, T, Tag> {
67 pub(in crate::driver::route::table) fn new(inner: &'a mut table::Storage<T, Tag>) -> Self {
68 Self(inner)
69 }
70
71 pub fn advance_to(self, floor: Option<route::Epoch>) {
72 self.0.advance_to(floor);
73 }
74
75 pub fn get(self, parts: table::Parts<Tag>) -> Option<&'a mut T> {
76 self.0.get_mut(parts.external())
77 }
78
79 pub fn occupied(self, parts: table::Parts<Tag>) -> Option<occupied::Entry<'a, T, Tag>> {
80 self.0.occupied(parts.external()).map(occupied::Entry::new)
81 }
82
83 pub fn try_occupied(
84 self,
85 parts: table::Parts<Tag>,
86 ) -> Result<occupied::Entry<'a, T, Tag>, errors::Lookup<route::Epoch, Tag>> {
87 let entry = self.0.try_occupied(parts.external())?;
88 Ok(occupied::Entry::new(entry))
89 }
90
91 pub fn at(self, index: indices::SlotIndex) -> Option<occupied::Entry<'a, T, Tag>> {
93 self.0.occupied_at(index.raw()).map(occupied::Entry::new)
94 }
95}
96
97#[repr(transparent)]
98pub struct Grant<'a, T, Tag: route::Tag>(
99 external::Grant<'a, T, route::Epoch, Tag, { u32::MAX }, { table::PARTITIONS }>,
100);
101
102impl<'a, T, Tag: route::Tag> Grant<'a, T, Tag> {
103 fn new(
104 inner: external::Grant<'a, T, route::Epoch, Tag, { u32::MAX }, { table::PARTITIONS }>,
105 ) -> Self {
106 Self(inner)
107 }
108
109 pub fn reserve(self) -> vacant::Entry<'a, T, Tag> {
110 vacant::Entry::new(self.0.reserve())
111 }
112}
113
114#[repr(transparent)]
115pub struct Vacancy<'a, T, Tag: route::Tag>(&'a mut table::Storage<T, Tag>);
116
117impl<'a, T, Tag: route::Tag> Vacancy<'a, T, Tag> {
118 pub(in crate::driver::route::table) fn new(inner: &'a mut table::Storage<T, Tag>) -> Self {
119 Self(inner)
120 }
121
122 pub fn try_reserve(self) -> Result<vacant::Entry<'a, T, Tag>, slab::Vacancy> {
123 let grant = self.0.try_grant()?;
124 Ok(vacant::Entry::new(grant.reserve()))
125 }
126
127 pub fn try_at(self, slot: indices::SlotIndex) -> Result<Grant<'a, T, Tag>, slab::Vacancy> {
128 self.0.try_grant_at(slot.raw()).map(Grant::new)
129 }
130
131 pub fn try_head(self, capacity: conn::Capacity) -> Result<Grant<'a, T, Tag>, slab::Vacancy> {
132 self.0
133 .try_grant_in(0..capacity.count().get())
134 .map(Grant::new)
135 }
136
137 pub fn try_except(
139 self,
140 capacity: conn::Capacity,
141 excluded: indices::SlotIndex,
142 ) -> Result<Grant<'a, T, Tag>, slab::Vacancy> {
143 self.0
144 .try_except(0..capacity.count().get(), excluded.raw())
145 .map(Grant::new)
146 }
147}