1use core::{convert, mem, num};
2
3use o3::{
4 alloc,
5 base::marker::ThreadBound,
6 depot,
7 depot::{
8 arena::slab::{
9 self, entries,
10 external::{self, errors, shared},
11 lookup,
12 },
13 capacity,
14 index::Initialized,
15 },
16};
17
18use crate::driver::{
19 route,
20 route::{indices, table},
21};
22
23#[derive(Clone, Copy)]
25#[repr(transparent)]
26pub struct Identity<'d, Tag: route::Tag>(Option<route::Target<'d, Tag>>);
27
28unsafe impl depot::raw::Compact for indices::SlotIndex {
31 const MAX: u32 = route::SLOT_MASK as u32;
32
33 fn into_raw(self) -> u32 {
34 self.raw()
35 }
36
37 unsafe fn from_raw(raw: u32) -> Self {
38 indices::SlotIndex::from_field(u64::from(raw))
39 }
40}
41
42unsafe impl depot::raw::Count<indices::SlotIndex> for table::Capacity {
44 fn count(self) -> capacity::Count {
45 self.count()
46 }
47}
48
49unsafe impl alloc::raw::Extent for table::Capacity {
51 fn len(&self) -> usize {
52 self.count().get()
53 }
54}
55
56#[repr(transparent)]
58pub struct Cell<'a, T, Tag: route::Tag> {
59 pub(in crate::driver::route) inner: &'a shared::Storage<T, route::Epoch, Tag>,
60}
61
62type Allocation<'a, T, Tag> = depot::raw::Proof<
63 (&'a shared::Storage<T, route::Epoch, Tag>, u32),
64 Initialized<entries::Allocated>,
65>;
66
67pub struct Slot<'a, T, Tag: route::Tag>(Allocation<'a, T, Tag>);
69
70impl<'a, T, Tag: route::Tag> Slot<'a, T, Tag> {
71 pub fn vacancy(
72 self,
73 ) -> Result<external::Vacant<'a, T, route::Epoch, Tag, entries::Interior<true>>, slab::Vacancy>
74 {
75 external::Vacant::allocated(self.0)
76 }
77
78 pub fn try_entry(
79 self,
80 ) -> Result<table::Current<'a, T, Tag>, lookup::Error<(), convert::Infallible>> {
81 Ok(table::Current(external::Occupied::allocated(self.0)?))
82 }
83
84 pub fn try_key(
85 self,
86 epoch: route::Epoch,
87 ) -> Result<table::Current<'a, T, Tag>, errors::Lookup<route::Epoch, Tag, ()>> {
88 Ok(table::Current(external::Occupied::try_bound(
89 self.0, epoch,
90 )?))
91 }
92}
93
94unsafe impl<'a, T, Tag: route::Tag> depot::raw::Project<indices::SlotIndex, entries::Allocated>
96 for Cell<'a, T, Tag>
97{
98 type Output = Slot<'a, T, Tag>;
99
100 unsafe fn project(self, index: indices::SlotIndex) -> Self::Output {
101 Slot(unsafe { depot::raw::Proof::new((self.inner, index.raw())) })
103 }
104}
105
106impl<'d, Tag: route::Tag> Identity<'d, Tag> {
107 pub const VACANT: Self = Self(None);
108
109 pub const fn from_target(target: route::Target<'d, Tag>) -> Self {
110 Self(Some(target))
111 }
112
113 pub fn into_target(self) -> Option<route::Target<'d, Tag>> {
114 self.0
115 }
116
117 pub unsafe fn target(self) -> route::Target<'d, Tag> {
121 unsafe { mem::transmute(self.0) }
123 }
124}
125
126impl<Tag: route::Tag> PartialEq for Identity<'_, Tag> {
127 fn eq(&self, other: &Self) -> bool {
128 self.0 == other.0
129 }
130}
131
132impl<Tag: route::Tag> Eq for Identity<'_, Tag> {}
133
134#[derive(Clone, Copy)]
135#[repr(transparent)]
136pub(crate) struct Auxiliary(u32);
137
138impl Auxiliary {
139 pub(crate) const fn from_meta(meta: u64) -> Self {
140 Self(((meta & route::AUX_MASK) >> route::AUX_SHIFT) as u32)
141 }
142
143 pub(crate) const fn with_bits(self, mask: u8, value: u8) -> Self {
144 let mask = mask as u32;
145 Self((self.0 & !mask) | (value as u32 & mask))
146 }
147
148 pub(crate) const fn raw(self) -> u32 {
149 self.0
150 }
151}
152
153impl From<u8> for Auxiliary {
154 fn from(raw: u8) -> Self {
155 Self(u32::from(raw))
156 }
157}
158
159#[derive(Clone, Copy)]
160#[repr(C)]
161pub(crate) struct Packed {
162 identity: num::NonZero<u64>,
163 thread: ThreadBound,
164 meta: u64,
165}
166
167impl Packed {
168 pub(crate) const fn new(token: route::Token, aux: Auxiliary) -> Self {
169 let route::Token {
170 epoch,
171 thread,
172 meta,
173 } = token;
174 Self {
175 identity: epoch,
176 thread,
177 meta: (meta & !route::AUX_MASK) | ((aux.raw() as u64) << route::AUX_SHIFT),
178 }
179 }
180
181 pub(crate) const fn words(self) -> [u64; 2] {
182 [self.identity.get(), self.meta]
183 }
184
185 pub(crate) const fn from_words(
186 identity: num::NonZero<u64>,
187 meta: u64,
188 thread: ThreadBound,
189 ) -> Self {
190 Self {
191 identity,
192 thread,
193 meta,
194 }
195 }
196
197 pub(crate) const unsafe fn restore(words: [u64; 2], thread: ThreadBound) -> Self {
200 Self::from_words(
201 unsafe { num::NonZero::<u64>::new_unchecked(words[0]) },
202 words[1],
203 thread,
204 )
205 }
206
207 pub(crate) const fn target(self) -> route::Token {
208 route::Token {
209 epoch: self.identity,
210 thread: self.thread,
211 meta: self.meta & !route::AUX_MASK,
212 }
213 }
214}
215
216const _: () = {
217 assert!(
218 mem::size_of::<Identity<'static, route::KeyTag<1>>>()
219 == mem::size_of::<route::Target<'static, route::KeyTag<1>>>()
220 );
221 assert!(
222 mem::align_of::<Identity<'static, route::KeyTag<1>>>()
223 == mem::align_of::<route::Target<'static, route::KeyTag<1>>>()
224 );
225 assert!(
226 mem::size_of::<Identity<'static, route::KeyTag<1>>>() == mem::size_of::<route::Token>()
227 );
228 assert!(
229 mem::align_of::<Identity<'static, route::KeyTag<1>>>() == mem::align_of::<route::Token>()
230 );
231 assert!(mem::size_of::<Auxiliary>() == mem::size_of::<u32>());
232 assert!(mem::size_of::<Packed>() == 2 * mem::size_of::<u64>());
233 assert!(mem::align_of::<Packed>() == mem::align_of::<u64>());
234};