Skip to main content

dope_core/driver/route/table/
capacity.rs

1use o3::collections::slab;
2
3use crate::driver::route;
4
5#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
6#[repr(transparent)]
7pub struct Capacity(pub(in crate::driver::route::table) slab::Capacity);
8
9impl Capacity {
10    pub const EMPTY: Self = Self(slab::Capacity::EMPTY);
11
12    pub const fn new(capacity: usize) -> Option<Self> {
13        if capacity <= route::SLOT_MASK as usize + 1 {
14            Some(Self(slab::Capacity::new(capacity as u32)))
15        } else {
16            None
17        }
18    }
19
20    pub(in crate::driver) const fn fixed(capacity: u32) -> Self {
21        assert!(capacity <= route::SLOT_MASK as u32 + 1);
22        Self(slab::Capacity::new(capacity))
23    }
24
25    pub const fn get(self) -> usize {
26        self.0.get()
27    }
28
29    pub const fn raw(self) -> u32 {
30        self.0.raw()
31    }
32
33    pub const fn slot(self, index: usize) -> Option<route::SlotIndex> {
34        if index < self.get() {
35            Some(route::SlotIndex::from_bounded(index as u32))
36        } else {
37            None
38        }
39    }
40
41    pub fn slots(self) -> impl ExactSizeIterator<Item = route::SlotIndex> + DoubleEndedIterator {
42        (0..self.raw()).map(route::SlotIndex::from_bounded)
43    }
44}