o3 0.62.0

shared-nothing primitives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use std::{mem, num::NonZero, ops, ptr};

use crate::depot::{
    self,
    arena::slab::key,
    array::Index,
    capacity,
    queue::{
        indexed::{
            Live,
            slab::{self, Slab, phase},
        },
        links, tagged,
    },
};

/// An admitted slot retaining its owner and exact membership phase.
#[must_use]
pub struct Slot<
    'a,
    T,
    Tag = (),
    const MAX: u32 = { u32::MAX },
    H: depot::raw::IndexedHook = links::Hook,
    const N: usize = 0,
    C: depot::raw::Count = capacity::Count,
    const LINKED: bool = true,
> {
    pub(in crate::depot) core: &'a mut Slab<T, Tag, MAX, H, N, C>,
    pub(in crate::depot) index: links::Index,
}

/// An unpublished slot retaining its exact cancellation duty.
#[must_use]
pub struct Vacant<
    'a,
    T,
    Tag = (),
    const MAX: u32 = { u32::MAX },
    H: depot::raw::IndexedHook = links::Hook,
    const N: usize = 0,
    C: depot::raw::Count = capacity::Count,
    P: phase::State = phase::Vacant,
> {
    pub(in crate::depot) core: &'a mut Slab<T, Tag, MAX, H, N, C>,
    pub(in crate::depot) index: links::Index,
    pub(in crate::depot) phase: P,
}

impl<
    'a,
    T,
    Tag,
    const MAX: u32,
    H: depot::raw::IndexedHook,
    const N: usize,
    C: depot::raw::Count,
    const LINKED: bool,
> Slot<'a, T, Tag, MAX, H, N, C, LINKED>
{
    pub(in crate::depot) fn value(&self) -> *mut mem::MaybeUninit<T> {
        // SAFETY: both slot phases retain their initialized payload in this block.
        unsafe { self.core.block.value(self.index) }
    }

    pub(in crate::depot) fn into_owner(self) -> (&'a mut Slab<T, Tag, MAX, H, N, C>, links::Index) {
        (self.core, self.index)
    }
}

impl<'a, T, Tag, const MAX: u32, H: depot::raw::IndexedHook, const N: usize, C: depot::raw::Count>
    Slot<'a, T, Tag, MAX, H, N, C>
{
    pub fn key(&self) -> key::Handle<Tag, MAX, true> {
        // SAFETY: the occupied row retains its generation in this owner.
        unsafe { self.core.block.key(self.index) }
    }

    pub fn into_mut(self) -> &'a mut T {
        // SAFETY: the consumed slot transfers its exclusive payload loan.
        unsafe { (&mut *self.value()).assume_init_mut() }
    }
}

impl<
    T,
    Tag,
    const MAX: u32,
    H: depot::raw::IndexedHook,
    const N: usize,
    C: depot::raw::Count,
    const LINKED: bool,
> AsRef<T> for Slot<'_, T, Tag, MAX, H, N, C, LINKED>
{
    fn as_ref(&self) -> &T {
        // SAFETY: either membership phase retains the initialized payload for this loan.
        unsafe { (&*self.value()).assume_init_ref() }
    }
}

impl<
    T,
    Tag,
    const MAX: u32,
    H: depot::raw::IndexedHook,
    const N: usize,
    C: depot::raw::Count,
    const LINKED: bool,
> AsMut<T> for Slot<'_, T, Tag, MAX, H, N, C, LINKED>
{
    fn as_mut(&mut self) -> &mut T {
        // SAFETY: this slot exclusively lends its initialized payload.
        unsafe { (&mut *self.value()).assume_init_mut() }
    }
}

impl<'a, T, Tag, const MAX: u32, H: depot::raw::IndexedHook, const N: usize, C: depot::raw::Count>
    Slot<'a, T, Tag, MAX, H, N, C, false>
{
    fn leave(self) -> (&'a mut Slab<T, Tag, MAX, H, N, C>, links::Index) {
        // SAFETY: the consumed detached row leaves the occupied count exactly once.
        unsafe { self.core.block.state_mut() }.len -= 1;
        self.into_owner()
    }

    pub(in crate::depot::queue) fn into_spare(
        self,
    ) -> (T, Vacant<'a, T, Tag, MAX, H, N, C, phase::Issued>) {
        Vacant::extract(self, phase::Issued)
    }

    pub(in crate::depot::queue) fn read(self) -> T {
        // SAFETY: retirement follows the final read of this detached payload.
        let value = unsafe { self.value().cast::<T>().read() };
        self.forget();
        value
    }

    pub(in crate::depot::queue) fn forget(self) {
        let (core, index) = self.leave();
        // SAFETY: the consumed row has transferred its payload duty.
        unsafe { core.block.release::<phase::Issued>(index) };
    }

    pub(in crate::depot::queue) fn discard(self) {
        // SAFETY: finish destruction in place while the detached row is exclusively held.
        unsafe { self.value().cast::<T>().drop_in_place() };
        self.forget();
    }
}

impl<'a, T, Tag, const MAX: u32, C: depot::raw::Count> Slot<'a, T, Tag, MAX, links::Hook, 0, C> {
    pub(in crate::depot::queue) fn detach(self) -> Slot<'a, T, Tag, MAX, links::Hook, 0, C, false> {
        // SAFETY: the occupied row identifies this reciprocal FIFO member.
        unsafe { depot::raw::Hooks::chain_unlink(&self.core.block, &self.core.chain, self.index) }
            .idle();
        Slot {
            core: self.core,
            index: self.index,
        }
    }

    pub fn push_back(self) {
        // SAFETY: the retained hook belongs to this original chain.
        let hook = unsafe { depot::raw::Hooks::hook_raw(&self.core.block, self.index) };
        unsafe {
            self.core
                .chain
                .rotate(&self.core.block, self.index, hook, false)
        };
    }

    pub fn remove(self) -> T {
        // SAFETY: retirement immediately overwrites the detached hook.
        unsafe { depot::raw::Hooks::chain_unlink(&self.core.block, &self.core.chain, self.index) };
        Slot {
            core: self.core,
            index: self.index,
        }
        .read()
    }
}

fn endpoint<const N: usize>(lane: Index<N>) -> NonZero<u32> {
    // SAFETY: every admitted fixed lane is below u8::MAX.
    unsafe { NonZero::new_unchecked(u32::MAX - lane.index() as u32) }
}

impl<'a, T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count>
    Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C>
{
    pub(in crate::depot::queue) fn detach(
        self,
    ) -> Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C, false> {
        // SAFETY: the exposed detached owner must mark this row idle in its original lane.
        let hook = unsafe { depot::raw::TagStore::hook(&*self.core, self.index) };
        unsafe { hook.unlink(&*self.core, self.index) };
        Slot {
            core: self.core,
            index: self.index,
        }
    }

    /// Appends this initialized slot to the destination without retiring its key.
    pub fn move_back(self, lane: Index<N>) {
        // SAFETY: relinking immediately replaces both words in the same hook.
        let hook = unsafe { depot::raw::TagStore::hook(&*self.core, self.index) };
        unsafe {
            hook.bypass(&*self.core);
            hook.push::<false, _>(&*self.core, endpoint(lane), self.index);
        }
    }

    pub fn take(
        self,
        lane: Index<N>,
    ) -> (
        T,
        Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C, phase::Restore<N>>,
    ) {
        Vacant::extract(self.detach(), phase::Restore(lane))
    }

    pub fn remove(self) -> T {
        // SAFETY: retirement immediately overwrites this unlinked hook.
        let hook = unsafe { depot::raw::TagStore::hook(&*self.core, self.index) };
        unsafe { hook.bypass(&*self.core) };
        Slot {
            core: self.core,
            index: self.index,
        }
        .read()
    }
}

impl<
    'a,
    T,
    Tag,
    const MAX: u32,
    H: depot::raw::IndexedHook,
    const N: usize,
    C: depot::raw::Count,
    P: phase::State,
> Vacant<'a, T, Tag, MAX, H, N, C, P>
{
    fn extract(slot: Slot<'a, T, Tag, MAX, H, N, C, false>, phase: P) -> (T, Self) {
        // SAFETY: the detached owner transfers its initialized payload to the caller.
        let value = unsafe { slot.value().cast::<T>().read() };
        let (core, index) = slot.leave();
        (value, Self { core, index, phase })
    }

    pub(in crate::depot) fn value(&self) -> *mut mem::MaybeUninit<T> {
        // SAFETY: this unpublished reservation retains its exact payload cell.
        unsafe { self.core.block.value(self.index) }
    }

    pub(in crate::depot) fn handle(&self) -> key::Handle<Tag, MAX, true> {
        // SAFETY: the reserved row retains initialized metadata and its generation.
        unsafe { self.core.block.key(self.index) }
    }

    pub(in crate::depot) fn into_owner(self) -> (&'a mut Slab<T, Tag, MAX, H, N, C>, links::Index) {
        let this = mem::ManuallyDrop::new(self);
        // SAFETY: the consuming transition transfers the exclusive loan and release duty.
        (unsafe { ptr::read(&this.core) }, this.index)
    }

    fn write(&mut self, value: T) {
        // SAFETY: this exclusive vacancy owns the uninitialized destination.
        unsafe { self.value().cast::<T>().write(value) };
    }

    /// # Safety
    /// This exact initialized row has just been linked in its original queue.
    unsafe fn published(self) -> Slot<'a, T, Tag, MAX, H, N, C> {
        let (core, index) = self.into_owner();
        unsafe { core.block.state_mut() }.len += 1;
        Slot { core, index }
    }
}

impl<'a, T, Tag, const MAX: u32, H: depot::raw::IndexedHook, const N: usize, C: depot::raw::Count>
    Vacant<'a, T, Tag, MAX, H, N, C>
{
    fn into_phase<Q: phase::State>(self, phase: Q) -> Vacant<'a, T, Tag, MAX, H, N, C, Q> {
        let (core, index) = self.into_owner();
        Vacant { core, index, phase }
    }
}

impl<'a, T, Tag, const MAX: u32, C: depot::raw::Count> Vacant<'a, T, Tag, MAX, links::Hook, 0, C> {
    pub fn key(
        self,
    ) -> (
        key::Handle<Tag, MAX, true>,
        Vacant<'a, T, Tag, MAX, links::Hook, 0, C, phase::Issued>,
    ) {
        (self.handle(), self.into_phase(phase::Issued))
    }

    pub fn fill(self, value: T) {
        let _ = self.insert(value);
    }
}

impl<'a, T, Tag, const MAX: u32, C: depot::raw::Count, P: phase::State>
    Vacant<'a, T, Tag, MAX, links::Hook, 0, C, P>
{
    pub fn insert(mut self, value: T) -> Slot<'a, T, Tag, MAX, links::Hook, 0, C> {
        self.write(value);
        // SAFETY: the initialized reservation retains its detached hook and chain.
        unsafe {
            depot::raw::Hooks::chain_push(&self.core.block, &self.core.chain, self.index);
            self.published()
        }
    }
}

impl<'a, T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count>
    Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C>
{
    pub fn select(
        self,
        lane: Index<N>,
    ) -> Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C, phase::Target<N>> {
        self.into_phase(phase::Target(lane))
    }
}

impl<'a, T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count, P: phase::State>
    Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C, P>
{
    /// # Safety
    /// This exact unpublished payload has been initialized once.
    unsafe fn publish<const FRONT: bool>(
        self,
        lane: Index<N>,
    ) -> Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C> {
        let hook = unsafe { depot::raw::TagStore::hook(&*self.core, self.index) };
        unsafe {
            hook.push::<FRONT, _>(&*self.core, endpoint(lane), self.index);
            self.published()
        }
    }
}

impl<'a, T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count>
    Vacant<'a, T, Tag, MAX, tagged::raw::Hook, N, C, phase::Target<N>>
{
    pub fn insert(mut self, value: T) -> Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C> {
        self.write(value);
        // SAFETY: this exact destination has just been initialized.
        unsafe { self.assume_init() }
    }

    /// # Safety
    /// This exact selected payload has been initialized once.
    pub(in crate::depot) unsafe fn assume_init(
        self,
    ) -> Slot<'a, T, Tag, MAX, tagged::raw::Hook, N, C> {
        let lane = self.phase.0;
        unsafe { self.publish::<false>(lane) }
    }
}

impl<T, Tag, const MAX: u32, const N: usize, C: depot::raw::Count>
    Vacant<'_, T, Tag, MAX, tagged::raw::Hook, N, C, phase::Restore<N>>
{
    pub fn restore(mut self, value: T) {
        self.write(value);
        let lane = self.phase.0;
        // SAFETY: restore republishes the retained generation at the lane's front.
        let _ = unsafe { self.publish::<true>(lane) };
    }
}

impl<
    T,
    Tag,
    const MAX: u32,
    H: depot::raw::IndexedHook,
    const N: usize,
    C: depot::raw::Count,
    P: phase::State,
> Drop for Vacant<'_, T, Tag, MAX, H, N, C, P>
{
    fn drop(&mut self) {
        // SAFETY: P retains this exact unpublished slot's cancellation duty.
        unsafe { self.core.block.release::<P>(self.index) };
    }
}

pub(super) fn resolve<
    T,
    Tag,
    const MAX: u32,
    H: depot::raw::IndexedHook,
    const N: usize,
    C: depot::raw::Count,
    O,
>(
    owner: O,
    key: key::Handle<Tag, MAX, true>,
) -> Result<Live<O, links::Index, H>, slab::Error<Tag, MAX, C>>
where
    O: ops::Deref<Target = Slab<T, Tag, MAX, H, N, C>>,
{
    let core = &*owner;
    let index = key.index();
    let ready = core.block.state().initialized;
    if index >= ready {
        let capacity = core.capacity();
        return Err(if index as usize >= capacity.count().get() {
            slab::Error::Index { key, capacity }
        } else {
            slab::Error::Uninitialized {
                key,
                initialized: ready,
            }
        });
    }
    // SAFETY: index lies in this retained block's initialized metadata prefix.
    let meta = unsafe { core.block.metadata.add(index as usize).as_ref() };
    let actual = meta.generation;
    if !H::is_linked(&meta.hook, links::Index::from(key)) {
        // SAFETY: this initialized unlinked hook retains its free/retired successor.
        return Err(if unsafe { meta.hook.next() } == index {
            slab::Error::Retired { key, actual }
        } else {
            slab::Error::Free { key, actual }
        });
    }
    if actual != key.generation() {
        return Err(slab::Error::Generation { key, actual });
    }
    // SAFETY: the retained owner fixes this initialized, live generation and hook domain.
    Ok(unsafe { depot::raw::Proof::new((owner, links::Index::from(key))) })
}
pub(super) fn pop<T, Tag, const MAX: u32, C: depot::raw::Count>(
    core: &mut Slab<T, Tag, MAX, links::Hook, 0, C>,
) -> Option<Slot<'_, T, Tag, MAX, links::Hook, 0, C, false>> {
    let index = core.chain.first()?;
    // SAFETY: this exclusive store retains its front, whose hook is immediately retired.
    unsafe { depot::raw::Hooks::chain_pop(&core.block, &core.chain, index) };
    Some(Slot { core, index })
}

const _: () = {
    assert!(size_of::<Slot<'_, (), (), 1, tagged::raw::Hook, 2>>() == 2 * size_of::<usize>());
    assert!(
        size_of::<Option<Slot<'_, (), (), 1, tagged::raw::Hook, 2>>>() == 2 * size_of::<usize>()
    );
    assert!(
        size_of::<Vacant<'_, (), (), 1, tagged::raw::Hook, 2, capacity::Count, phase::Restore<2>>>(
        ) == 2 * size_of::<usize>()
    );
};