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