o3 0.16.2

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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use std::{cell, fmt, hash, marker, ops};

use crate::collections::{self, slab};

mod sealed;

pub(crate) use sealed::Backing;

/// A non-wrapping logical generation; `None` permanently exhausts identity.
pub trait Generation: Copy + Eq {
    const INITIAL: Self;

    #[must_use]
    fn next(self) -> Option<Self>;
}

/// A typed external identity independent of the slab's private generation.
#[repr(C)]
pub struct Key<G: Generation, Tag = ()> {
    generation: G,
    index: u32,
    tag: marker::PhantomData<*mut Tag>,
}

struct Stamped<T, G: Generation> {
    generation: G,
    value: T,
}

type BuildResult<G, Tag, I, T, R, E> = Result<(Key<G, Tag>, R), slab::BuildError<I, T, E>>;

/// Exclusive storage whose private physical generations may recycle while
/// externally visible logical generations never do.
pub struct Exclusive<
    T,
    G: Generation,
    Tag = (),
    const PHYSICAL_MAX: u32 = { u32::MAX },
    const PARTITIONS: usize = 1,
> {
    inner: slab::Exclusive<Stamped<T, G>, Tag, PHYSICAL_MAX, true, PARTITIONS>,
    next: Option<G>,
}

/// Interior-mutable storage with independent non-wrapping logical identities.
pub struct Cell<T, G: Generation, Tag = (), const PHYSICAL_MAX: u32 = { u32::MAX }> {
    inner: slab::Cell<Stamped<T, G>, Tag, PHYSICAL_MAX, true>,
    next: cell::Cell<Option<G>>,
}

pub struct Entries<
    'a,
    T,
    G: Generation,
    Tag = (),
    const PHYSICAL_MAX: u32 = { u32::MAX },
    const PARTITIONS: usize = 1,
> {
    slab: &'a Exclusive<T, G, Tag, PHYSICAL_MAX, PARTITIONS>,
}

pub struct EntriesMut<
    'a,
    T,
    G: Generation,
    Tag = (),
    const PHYSICAL_MAX: u32 = { u32::MAX },
    const PARTITIONS: usize = 1,
> {
    slab: &'a mut Exclusive<T, G, Tag, PHYSICAL_MAX, PARTITIONS>,
}

pub struct OccupiedEntry<
    'a,
    T,
    G: Generation,
    Tag = (),
    const PHYSICAL_MAX: u32 = { u32::MAX },
    const PARTITIONS: usize = 1,
> {
    inner: slab::OccupiedEntry<'a, Stamped<T, G>, Tag, PHYSICAL_MAX, true, PARTITIONS>,
    generation: marker::PhantomData<G>,
}

pub struct VacantEntry<
    'a,
    T,
    G: Generation,
    Tag = (),
    const PHYSICAL_MAX: u32 = { u32::MAX },
    const PARTITIONS: usize = 1,
> {
    inner: slab::VacantEntry<'a, Stamped<T, G>, Tag, PHYSICAL_MAX, true, PARTITIONS>,
    generation: G,
}

impl<G: Generation, Tag> Key<G, Tag> {
    #[must_use]
    pub const fn new(index: u32, generation: G) -> Self {
        Self {
            generation,
            index,
            tag: marker::PhantomData,
        }
    }

    #[must_use]
    pub const fn index(self) -> u32 {
        self.index
    }

    #[must_use]
    pub const fn generation(self) -> G {
        self.generation
    }

    #[must_use]
    pub const fn retag<Other>(self) -> Key<G, Other> {
        Key::new(self.index, self.generation)
    }
}

impl<G: Generation, Tag> Clone for Key<G, Tag> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<G: Generation, Tag> Copy for Key<G, Tag> {}

impl<G: Generation, Tag> PartialEq for Key<G, Tag> {
    fn eq(&self, other: &Self) -> bool {
        self.index == other.index && self.generation == other.generation
    }
}

impl<G: Generation, Tag> Eq for Key<G, Tag> {}

impl<G: Generation + hash::Hash, Tag> hash::Hash for Key<G, Tag> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.generation.hash(state);
        self.index.hash(state);
    }
}

impl<G: Generation + fmt::Debug, Tag> fmt::Debug for Key<G, Tag> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Key")
            .field("index", &self.index)
            .field("generation", &self.generation)
            .finish()
    }
}

fn take_generation<G: Generation>(next: &mut Option<G>) -> Option<G> {
    let generation = next.take()?;
    *next = generation.next();
    Some(generation)
}

impl<T, G: Generation, Tag, const PHYSICAL_MAX: u32, const PARTITIONS: usize>
    Exclusive<T, G, Tag, PHYSICAL_MAX, PARTITIONS>
{
    pub fn with_capacity(capacity: slab::Capacity) -> Self {
        match Self::try_with_capacity(capacity) {
            Ok(slab) => slab,
            Err(error) => error.abort(),
        }
    }

    pub fn try_with_capacity(
        capacity: slab::Capacity,
    ) -> Result<Self, collections::AllocationError> {
        let inner = Backing::external(capacity)?;
        Ok(Self {
            inner,
            next: Some(G::INITIAL),
        })
    }

    pub fn capacity(&self) -> usize {
        self.inner.capacity()
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn available(&self) -> usize {
        self.inner.available()
    }

    pub fn insert(&mut self, value: T) -> Result<Key<G, Tag>, T> {
        let Some(vacant) = self.vacant_entry() else {
            return Err(value);
        };
        Ok(vacant.insert(value))
    }

    pub fn vacant_entry(&mut self) -> Option<VacantEntry<'_, T, G, Tag, PHYSICAL_MAX, PARTITIONS>> {
        let inner = self.inner.vacant_entry()?;
        let generation = take_generation(&mut self.next)?;
        Some(VacantEntry { inner, generation })
    }

    pub fn vacant_entry_at(
        &mut self,
        index: u32,
    ) -> Option<VacantEntry<'_, T, G, Tag, PHYSICAL_MAX, PARTITIONS>> {
        let inner = self.inner.slots().vacant_entry_at(index)?;
        let generation = take_generation(&mut self.next)?;
        Some(VacantEntry { inner, generation })
    }

    pub fn vacant_entry_in(
        &mut self,
        range: ops::Range<u32>,
    ) -> Option<VacantEntry<'_, T, G, Tag, PHYSICAL_MAX, PARTITIONS>> {
        let inner = self.inner.slots().vacant_entry_in(range)?;
        let generation = take_generation(&mut self.next)?;
        Some(VacantEntry { inner, generation })
    }

    pub fn get(&self, key: Key<G, Tag>) -> Option<&T> {
        self.entries().get(key)
    }

    pub fn occupied_entry(
        &mut self,
        key: Key<G, Tag>,
    ) -> Option<OccupiedEntry<'_, T, G, Tag, PHYSICAL_MAX, PARTITIONS>> {
        let inner = self.inner.slots().occupied_entry_at(key.index())?;
        (inner.get().generation == key.generation()).then_some(OccupiedEntry {
            inner,
            generation: marker::PhantomData,
        })
    }

    pub fn entries(&self) -> Entries<'_, T, G, Tag, PHYSICAL_MAX, PARTITIONS> {
        Entries { slab: self }
    }

    pub fn entries_mut(&mut self) -> EntriesMut<'_, T, G, Tag, PHYSICAL_MAX, PARTITIONS> {
        EntriesMut { slab: self }
    }

    pub fn remove(&mut self, key: Key<G, Tag>) -> Option<T> {
        self.occupied_entry(key).map(OccupiedEntry::remove)
    }

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

impl<'a, T, G: Generation, Tag, const PHYSICAL_MAX: u32, const PARTITIONS: usize>
    Entries<'a, T, G, Tag, PHYSICAL_MAX, PARTITIONS>
{
    pub fn get(self, key: Key<G, Tag>) -> Option<&'a T> {
        let (stamped, _) = slab::raw::ExternalAccess::entry_at(&self.slab.inner, key.index())?;
        (stamped.generation == key.generation()).then_some(&stamped.value)
    }

    pub fn current(self, index: u32) -> Option<(&'a T, Key<G, Tag>)> {
        let (stamped, _) = slab::raw::ExternalAccess::entry_at(&self.slab.inner, index)?;
        Some((&stamped.value, Key::new(index, stamped.generation)))
    }

    pub fn values(self) -> impl Iterator<Item = &'a T>
    where
        T: 'a,
        G: 'a,
    {
        (0..self.slab.inner.capacity() as u32).filter_map(|index| {
            slab::raw::ExternalAccess::entry_at(&self.slab.inner, index)
                .map(|(stamped, _)| &stamped.value)
        })
    }
}

impl<'a, T, G: Generation, Tag, const PHYSICAL_MAX: u32, const PARTITIONS: usize>
    EntriesMut<'a, T, G, Tag, PHYSICAL_MAX, PARTITIONS>
{
    pub fn get(self, key: Key<G, Tag>) -> Option<&'a mut T> {
        let (stamped, _) =
            slab::raw::ExternalAccess::entry_at_mut(&mut self.slab.inner, key.index())?;
        (stamped.generation == key.generation()).then_some(&mut stamped.value)
    }

    pub fn values(self) -> impl Iterator<Item = &'a mut T>
    where
        T: 'a,
        G: 'a,
    {
        self.slab
            .inner
            .slots()
            .values_mut()
            .map(|stamped| &mut stamped.value)
    }
}

impl<T, G: Generation, Tag, const PHYSICAL_MAX: u32, const PARTITIONS: usize>
    OccupiedEntry<'_, T, G, Tag, PHYSICAL_MAX, PARTITIONS>
{
    pub fn key(&self) -> Key<G, Tag> {
        Key::new(self.inner.key().index(), self.inner.get().generation)
    }

    pub fn get(&self) -> &T {
        &self.inner.get().value
    }

    pub fn get_mut(&mut self) -> &mut T {
        &mut self.inner.get_mut().value
    }

    pub fn remove(self) -> T {
        self.inner.remove().value
    }
}

impl<'a, T, G: Generation, Tag, const PHYSICAL_MAX: u32, const PARTITIONS: usize>
    VacantEntry<'a, T, G, Tag, PHYSICAL_MAX, PARTITIONS>
{
    pub fn key(&self) -> Key<G, Tag> {
        Key::new(self.inner.key().index(), self.generation)
    }

    pub fn insert(self, value: T) -> Key<G, Tag> {
        let key = self.key();
        let _ = self.inner.insert(Stamped {
            generation: self.generation,
            value,
        });
        key
    }

    pub fn insert_occupied(
        self,
        value: T,
    ) -> OccupiedEntry<'a, T, G, Tag, PHYSICAL_MAX, PARTITIONS> {
        OccupiedEntry {
            inner: self.inner.insert_occupied(Stamped {
                generation: self.generation,
                value,
            }),
            generation: marker::PhantomData,
        }
    }
}

impl<T, G: Generation, Tag, const PHYSICAL_MAX: u32> Cell<T, G, Tag, PHYSICAL_MAX> {
    pub fn with_capacity(capacity: slab::Capacity) -> Self {
        match Self::try_with_capacity(capacity) {
            Ok(slab) => slab,
            Err(error) => error.abort(),
        }
    }

    pub fn try_with_capacity(
        capacity: slab::Capacity,
    ) -> Result<Self, collections::AllocationError> {
        let inner = Backing::external(capacity)?;
        Ok(Self {
            inner,
            next: cell::Cell::new(Some(G::INITIAL)),
        })
    }

    fn take_generation(&self) -> Option<G> {
        let generation = self.next.take()?;
        self.next.set(generation.next());
        Some(generation)
    }

    pub fn capacity(&self) -> usize {
        self.inner.capacity()
    }

    pub fn len(&self) -> usize {
        self.inner.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    pub fn available(&self) -> usize {
        self.inner.available()
    }

    pub fn key_at(&self, position: usize) -> Option<Key<G, Tag>> {
        let raw = self.inner.slots().key_at(position)?;
        self.inner
            .update(raw, |stamped| Key::new(raw.index(), stamped.generation))
    }

    pub fn keys(&self) -> impl Iterator<Item = Key<G, Tag>> + '_ {
        self.inner.keys().filter_map(|raw| {
            self.inner
                .update(raw, |stamped| Key::new(raw.index(), stamped.generation))
        })
    }

    pub fn any_or_busy(&self, mut predicate: impl FnMut(&T) -> bool) -> bool {
        self.inner.any_or_busy(|stamped| predicate(&stamped.value))
    }

    pub fn insert(&self, value: T) -> Result<Key<G, Tag>, T> {
        let Some(generation) = self.take_generation() else {
            return Err(value);
        };
        self.inner
            .insert(Stamped { generation, value })
            .map(|raw| Key::new(raw.index(), generation))
            .map_err(|stamped| stamped.value)
    }

    pub fn try_insert_with<R, E>(
        &self,
        value: T,
        f: impl FnOnce(Key<G, Tag>, &mut T) -> Result<R, E>,
    ) -> Result<(Key<G, Tag>, R), slab::InsertError<T, E>> {
        let Some(generation) = self.take_generation() else {
            return Err(slab::InsertError::Full(value));
        };
        self.inner
            .try_insert_with(Stamped { generation, value }, |raw, stamped| {
                f(Key::new(raw.index(), generation), &mut stamped.value)
            })
            .map(|(raw, result)| (Key::new(raw.index(), generation), result))
            .map_err(|error| match error {
                slab::InsertError::Full(stamped) => slab::InsertError::Full(stamped.value),
                slab::InsertError::Rejected(stamped, error) => {
                    slab::InsertError::Rejected(stamped.value, error)
                }
            })
    }

    pub fn try_insert_build<I, R, E>(
        &self,
        input: I,
        build: impl FnOnce(I) -> T,
        f: impl FnOnce(Key<G, Tag>, &mut T) -> Result<R, E>,
    ) -> BuildResult<G, Tag, I, T, R, E> {
        let Some(generation) = self.take_generation() else {
            return Err(slab::BuildError::Full(input));
        };
        self.inner
            .try_insert_build(
                input,
                |input| Stamped {
                    generation,
                    value: build(input),
                },
                |raw, stamped| f(Key::new(raw.index(), generation), &mut stamped.value),
            )
            .map(|(raw, result)| (Key::new(raw.index(), generation), result))
            .map_err(|error| match error {
                slab::BuildError::Full(input) => slab::BuildError::Full(input),
                slab::BuildError::Rejected(stamped, error) => {
                    slab::BuildError::Rejected(stamped.value, error)
                }
            })
    }

    pub fn update<R>(&self, key: Key<G, Tag>, f: impl FnOnce(&mut T) -> R) -> Option<R> {
        self.inner
            .slots()
            .update_index(key.index(), |stamped| {
                (stamped.generation == key.generation()).then(|| f(&mut stamped.value))
            })
            .flatten()
    }

    pub fn remove(&self, key: Key<G, Tag>) -> Option<T> {
        self.remove_with(key, |_| Some(())).map(|(value, ())| value)
    }

    pub fn remove_with<R>(
        &self,
        key: Key<G, Tag>,
        f: impl FnOnce(&mut T) -> Option<R>,
    ) -> Option<(T, R)> {
        self.inner
            .slots()
            .remove_index_with(key.index(), |stamped| {
                (stamped.generation == key.generation())
                    .then(|| f(&mut stamped.value))
                    .flatten()
            })
            .map(|(stamped, result)| (stamped.value, result))
    }
}