o3 0.25.1

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
460
461
use std::{convert, iter, marker, pin::Pin};

pub use cell::Cell;
use cycle::Policy;
pub use slots::Slots;
use storage::Plane as _;

use crate::depot::{
    arena::slab::{self, key::cycle},
    capacity,
    capacity::Physical,
    index::Initialized,
    queue::links,
};

pub mod entries;
pub mod external;
pub mod key;
pub mod lookup;
pub mod storage;

mod cell;
mod entry;
mod slots;
pub use entry::{
    anchor::Anchor, anchored::Anchored, borrowed::Borrowed, occupied::Occupied, restore::Withdrawn,
    staged::Staged, vacant::Vacant,
};

/// One issued identity retaining its reserved slot until installation or retirement.
pub type Reserved<'a, T, Tag = (), const MAX: u32 = { u32::MAX }, const RECYCLE: bool = false> =
    Vacant<
        crate::mem::Lease<
            entries::Pending<'a, T, MAX, entries::Interior<RECYCLE>>,
            crate::mem::lease::Open,
        >,
        Tag,
    >;

/// Why no identity can currently be reserved from a slab.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Vacancy {
    /// Usable slots are occupied or reserved, or the physical capacity is zero.
    Full,
    /// The admitted non-wrapping identity domain is permanently exhausted.
    IdentityExhausted,
}

impl<T, Tag, const MAX: u32, const PARTITIONS: usize> Store<T, Tag, MAX, false, PARTITIONS> {
    /// Reserves every slot and dense index entry transactionally.
    pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            core: entries::Core::try_new(cap)?,
            tag: marker::PhantomData,
        })
    }
}

/// Slot storage whose initialized values remain pinned at their original addresses.
pub struct Pinned<
    T,
    Tag = (),
    const MAX: u32 = { u32::MAX },
    const RECYCLE: bool = false,
    const PARTITIONS: usize = 1,
    S = storage::Heap,
> where
    cycle::Mode<RECYCLE>: Policy,
    S: storage::Storage,
{
    pub(in crate::depot) core: entries::Core<T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS, S>,
    _pin: marker::PhantomPinned,
    tag: marker::PhantomData<fn() -> Tag>,
}

impl<T, Tag, const MAX: u32, const PARTITIONS: usize, S> Pinned<T, Tag, MAX, false, PARTITIONS, S>
where
    S: storage::Storage,
    S::Plane<T, MAX, entries::Exclusive>: storage::HeapPlane<T, MAX, entries::Exclusive>,
{
    /// Allocates one pinned heap extent under its slot initialization policy.
    pub fn try_new(cap: capacity::Count) -> Result<Self, crate::alloc::Error> {
        Ok(Self {
            core: entries::Core::try_new(cap)?,
            _pin: marker::PhantomPinned,
            tag: marker::PhantomData,
        })
    }
}

impl<T, Tag, const MAX: u32, const PARTITIONS: usize, const N: usize> Default
    for Pinned<T, Tag, MAX, false, PARTITIONS, storage::Inline<N>>
{
    /// Builds the complete inline slot plane without allocating.
    fn default() -> Self {
        Self {
            core: entries::Core::new(),
            _pin: marker::PhantomPinned,
            tag: marker::PhantomData,
        }
    }
}

impl<T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize, S>
    Pinned<T, Tag, MAX, RECYCLE, PARTITIONS, S>
where
    cycle::Mode<RECYCLE>: Policy,
    S: storage::Storage,
{
    pub fn capacity(&self) -> usize {
        self.core.slots.capacity()
    }

    pub fn is_full(&self) -> bool {
        self.core.is_full()
    }

    pub fn vacant(
        self: Pin<&mut Self>,
    ) -> Option<slab::Anchor<'_, T, Tag, MAX, RECYCLE, PARTITIONS, S>> {
        let store = self.into_ref().get_ref();
        Some(slab::Anchor {
            entry: store.core.free()?,
            tag: marker::PhantomData,
        })
    }

    pub fn parts(&self, parts: key::Parts<MAX>) -> Option<&T> {
        Some(entries::Occupied::key(&self.core, parts.index(), parts.generation())?.read())
    }

    pub fn parts_mut(self: Pin<&mut Self>, parts: key::Parts<MAX>) -> Option<Pin<&mut T>> {
        Some(self.anchor(parts)?.into_value())
    }

    pub fn anchor(
        self: Pin<&mut Self>,
        parts: key::Parts<MAX>,
    ) -> Option<slab::Anchored<'_, T, Tag, MAX, RECYCLE, PARTITIONS, S>> {
        let store = self.into_ref().get_ref();
        let entry = entries::Occupied::key(&store.core, parts.index(), parts.generation())?;
        Some(slab::Anchored {
            entry: entry.take(),
            tag: marker::PhantomData,
        })
    }

    /// Passes the pinned value to `f`, then drops it in place.
    pub fn remove_with<R>(
        self: Pin<&mut Self>,
        parts: key::Parts<MAX>,
        f: impl FnOnce(u32, Pin<&mut T>) -> R,
    ) -> Option<R> {
        let mut entry = self.anchor(parts)?;
        let result = f(parts.index(), entry.value());
        entry.discard();
        Some(result)
    }
}

impl<T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize>
    slab::Store<T, Tag, MAX, RECYCLE, PARTITIONS>
where
    cycle::Mode<RECYCLE>: Policy,
{
    pub fn capacity(&self) -> usize {
        self.core.slots.capacity()
    }

    /// Nonretired slots, including occupied slots and unpublished reservations.
    pub fn remaining(&self) -> capacity::Count {
        self.core.remaining()
    }

    pub fn len(&self) -> capacity::Count {
        self.core.len()
    }

    pub fn is_full(&self) -> bool {
        self.core.is_full()
    }

    /// Checks temporary or permanent admission without reserving a slot.
    pub fn try_ready(&self) -> Result<(), Vacancy> {
        if self.core.is_full() {
            Err(self.core.refusal())
        } else {
            Ok(())
        }
    }

    pub fn insert(&mut self, value: T) -> Result<key::Handle<Tag, MAX>, T> {
        let Some(pending) = self.core.free() else {
            return Err(value);
        };
        Ok(slab::Vacant::<_, Tag>::from_pending(pending).insert(value))
    }

    /// Reserves a slot or preserves the exact temporary or permanent refusal.
    pub fn try_vacant(
        &mut self,
    ) -> Result<
        slab::Vacant<entries::Pending<'_, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS>, Tag>,
        Vacancy,
    > {
        Ok(slab::Vacant::from_pending(
            self.core.free().ok_or_else(|| self.core.refusal())?,
        ))
    }

    /// Reserves free slots under one exclusive owner loan.
    /// Each yielded reservation owns only its distinct slot.
    pub fn vacancies(
        &mut self,
    ) -> impl Iterator<
        Item = slab::Vacant<
            entries::Pending<'_, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS>,
            Tag,
            false,
        >,
    > {
        let core = &self.core;
        iter::from_fn(move || core.free().map(slab::Vacant::from_pending))
    }
}

impl<T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize>
    slab::Store<T, Tag, MAX, RECYCLE, PARTITIONS>
where
    cycle::Mode<RECYCLE>: Policy,
{
    /// Borrows occupied values in physical slot order from this exact shared owner.
    pub fn values(&self) -> impl Iterator<Item = &T> {
        (0..self.capacity()).filter_map(|index| {
            entries::Occupied::at(&self.core, index as u32).map(entries::Occupied::read)
        })
    }

    pub fn try_get(
        &self,
        key: key::Handle<Tag, MAX>,
    ) -> Result<
        &T,
        key::Error<Tag, MAX, convert::Infallible, <cycle::Mode<RECYCLE> as Policy>::Retired>,
    > {
        Ok(entries::Occupied::try_key(&self.core, key)?.read())
    }

    pub fn get_mut(&mut self, key: key::Handle<Tag, MAX>) -> Option<&mut T> {
        let entry = entries::Occupied::key(&self.core, key.index(), key.generation())?;
        Some(entry.take().into_mut())
    }

    pub fn remove(&mut self, key: key::Handle<Tag, MAX>) -> Option<T> {
        let entry = entries::Occupied::key(&self.core, key.index(), key.generation())?;
        Some(entry.take().remove())
    }

    pub fn clear(&mut self) {
        self.core.clear();
    }
}

impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize>
    slab::Store<T, Tag, MAX, RECYCLE, PARTITIONS>
where
    cycle::Mode<RECYCLE>: Policy,
{
    pub fn try_entry(
        &'a mut self,
        key: key::Handle<Tag, MAX>,
    ) -> Lookup<'a, T, Tag, MAX, RECYCLE, PARTITIONS> {
        let entry = entries::Occupied::try_key(&self.core, key)?;
        Ok(slab::Occupied::new(entry))
    }
}

const _: () = {
    assert!(size_of::<slab::Occupied<entries::Busy<'static, u64>>>() == 2 * size_of::<usize>());
    assert!(
        size_of::<slab::Occupied<entries::Busy<'static, u64>, (), false>>()
            == size_of::<slab::Occupied<entries::Busy<'static, u64>>>()
    );
    assert!(
        size_of::<
            slab::Occupied<entries::Busy<'static, u64, { u32::MAX }, entries::Exclusive, 1, u8>>,
        >() == size_of::<slab::Occupied<entries::Busy<'static, u64>>>()
    );
    assert!(
        size_of::<
            Option<
                slab::Occupied<
                    entries::Busy<'static, u64, { u32::MAX }, entries::Exclusive, 1, u8>,
                >,
            >,
        >() == size_of::<slab::Occupied<entries::Busy<'static, u64>>>()
    );
};

impl<'a, 'h, T, Tag, const MAX: u32>
    TryFrom<
        crate::depot::raw::Proof<
            (
                &'a mut slab::Store<T, Tag, MAX>,
                capacity::Key<'h, key::Handle<Tag, MAX>>,
            ),
            Physical,
        >,
    > for slab::Occupied<entries::Busy<'a, T, MAX>, Tag>
{
    type Error = key::Stale<'h, Tag, MAX>;

    fn try_from(
        proof: crate::depot::raw::Proof<
            (
                &'a mut slab::Store<T, Tag, MAX>,
                capacity::Key<'h, key::Handle<Tag, MAX>>,
            ),
            Physical,
        >,
    ) -> Result<Self, Self::Error> {
        let entry = entries::Occupied::try_bound(unsafe {
            proof.map(|(source, key)| (&source.core, key))
        })?;
        Ok(Self::new(entry))
    }
}

/// A branded exclusive slab owner with an explicit generation policy.
pub struct Store<
    T,
    Tag = (),
    const MAX: u32 = { u32::MAX },
    const RECYCLE: bool = false,
    const PARTITIONS: usize = 1,
> where
    cycle::Mode<RECYCLE>: Policy,
{
    pub(in crate::depot::arena::slab) core:
        entries::Core<T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS>,
    pub(in crate::depot::arena::slab) tag: marker::PhantomData<fn() -> Tag>,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum State<B, R> {
    Free,
    Reserved,
    Occupied,
    Busy(B),
    Retired(R),
}

/// The exact non-live-borrow refusal of one admitted slot.
#[derive(Debug)]
pub(in crate::depot::arena::slab) enum Refusal<R> {
    Free,
    Reserved,
    Retired(R),
    Stale,
}

/// The classification of one admitted slot against a retained generation.
#[derive(Debug)]
pub(in crate::depot::arena::slab) enum Admitted<B, R> {
    Exact,
    Refused(Refusal<R>),
    Borrowed(B),
}

impl<B, R> State<B, R> {
    /// Classifies one admitted slot, retaining the live-borrow witness for its caller.
    pub(in crate::depot::arena::slab) fn classify<const MAX: u32>(
        self,
        actual: key::Generation<MAX>,
        expected: key::Generation<MAX>,
    ) -> Admitted<B, R> {
        match self {
            Self::Free => Admitted::Refused(Refusal::Free),
            Self::Reserved => Admitted::Refused(Refusal::Reserved),
            Self::Retired(retired) => Admitted::Refused(Refusal::Retired(retired)),
            Self::Busy(borrow) => Admitted::Borrowed(borrow),
            Self::Occupied if actual != expected => Admitted::Refused(Refusal::Stale),
            Self::Occupied => Admitted::Exact,
        }
    }
}

impl<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize>
    slab::Store<T, Tag, MAX, RECYCLE, PARTITIONS>
where
    cycle::Mode<RECYCLE>: Policy,
{
    /// Consumes an initialized-slot proof without repeating its lookup.
    pub fn value<P>(
        live: crate::depot::raw::Proof<
            (
                &'a slab::Store<T, Tag, MAX, RECYCLE, PARTITIONS>,
                links::Index,
            ),
            Initialized<P>,
        >,
    ) -> crate::depot::raw::Proof<(&'a T, links::Index), Initialized<P>> {
        // SAFETY: the native core retains the complete shared slab loan.
        entries::Core::read(unsafe { live.map(|(slab, index)| (&slab.core, index)) })
    }
}

/// One distinct physical slot retained by exclusive iteration.
pub enum Entry<'a, T, Tag = (), const MAX: u32 = { u32::MAX }> {
    Vacant(slab::Vacant<entries::Pending<'a, T, MAX>, Tag, false>),
    Occupied(slab::Occupied<entries::Busy<'a, T, MAX>, Tag, false>),
}

/// An occupied slot whose next generation has been proved available.
pub struct Reuse<'a, T, Tag = (), const MAX: u32 = { u32::MAX }, const ROOT: bool = true> {
    entry: slab::Occupied<entries::Busy<'a, T, MAX>, Tag, ROOT>,
    next: key::Generation<MAX>,
}

impl<'a, T, Tag, const MAX: u32, const ROOT: bool>
    TryFrom<slab::Occupied<entries::Busy<'a, T, MAX>, Tag, ROOT>> for Reuse<'a, T, Tag, MAX, ROOT>
{
    type Error = slab::Occupied<entries::Busy<'a, T, MAX>, Tag, ROOT>;

    fn try_from(entry: Self::Error) -> Result<Self, Self::Error> {
        let Some(next) = entry.entry.generation().checked_add(1) else {
            return Err(entry);
        };
        Ok(Self { entry, next })
    }
}

impl<'a, T, Tag, const MAX: u32, const ROOT: bool> Reuse<'a, T, Tag, MAX, ROOT> {
    pub fn key(&self) -> key::Handle<Tag, MAX> {
        self.entry.key()
    }

    /// Replaces the value and advances its identity without rechecking capacity or generation.
    pub fn replace(self, value: T) -> (slab::Occupied<entries::Busy<'a, T, MAX>, Tag, ROOT>, T) {
        let (busy, prior) = self.entry.entry.replace(self.next, value);
        (
            slab::Occupied {
                entry: busy,
                tag: self.entry.tag,
            },
            prior,
        )
    }
}

/// The retained (owner, index, projector) selects a valid field of that initialized slot.
/// The field remains disjoint from mutable payloads for the owner's full returned loan.
pub enum Field {}

impl crate::depot::raw::Sealed for Field {}
impl crate::depot::Predicate for Field {}

type Lookup<'a, T, Tag, const MAX: u32, const RECYCLE: bool, const PARTITIONS: usize> = Result<
    slab::Occupied<entries::Busy<'a, T, MAX, entries::Exclusive<RECYCLE>, PARTITIONS>, Tag>,
    key::Error<Tag, MAX, convert::Infallible, <cycle::Mode<RECYCLE> as Policy>::Retired>,
>;