Skip to main content

dope_core/driver/route/table/
conn.rs

1use std::num;
2
3use o3::{base::num::bounded, depot, depot::capacity};
4
5use crate::driver::{route, route::table};
6
7/// A non-empty target-table capacity with one encodable slot left as a sentinel.
8#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
9#[repr(transparent)]
10pub struct Capacity(bounded::raw::NonZeroU32<1, { route::SLOT_MASK as u32 }>);
11
12impl Capacity {
13    pub const fn fixed<const COUNT: usize>() -> Self {
14        const { assert!(COUNT > 0 && COUNT <= route::SLOT_MASK as usize, "capacity") };
15        // SAFETY: the const bound keeps the count in 1..=SLOT_MASK.
16        Self(unsafe {
17            bounded::raw::NonZeroU32::from_proven(
18                num::NonZero::<u32>::MIN.saturating_add(COUNT as u32 - 1),
19            )
20        })
21    }
22
23    pub const fn nonzero(self) -> num::NonZero<usize> {
24        num::NonZero::<usize>::MIN.saturating_add(self.raw() as usize - 1)
25    }
26
27    pub(crate) const fn raw(self) -> u32 {
28        self.0.get()
29    }
30
31    pub const fn count(self) -> num::NonZero<u32> {
32        self.0.nonzero()
33    }
34
35    pub const fn entries(self) -> capacity::Count {
36        capacity::Count::new(self.0.get())
37    }
38
39    /// Scales this validated connection domain into a collection count.
40    ///
41    /// The factor is part of the type-level call site, so the complete
42    /// connection domain must fit before any value can be scaled.
43    pub const fn scale<const FACTOR: u32>(self) -> capacity::Count {
44        const {
45            assert!(FACTOR != 0);
46            assert!(route::SLOT_MASK <= (u32::MAX / FACTOR) as u64);
47        }
48        capacity::Count::new(self.0.get() * FACTOR)
49    }
50}
51
52impl From<num::NonZero<u16>> for Capacity {
53    fn from(count: num::NonZero<u16>) -> Self {
54        const { assert!(u16::MAX as u64 <= route::SLOT_MASK, "capacity bound") };
55        // SAFETY: the const bound keeps every nonzero u16 count within 1..=SLOT_MASK.
56        Self(unsafe { bounded::raw::NonZeroU32::from_proven(count.into()) })
57    }
58}
59
60impl TryFrom<usize> for Capacity {
61    type Error = table::Invalid;
62
63    fn try_from(actual: usize) -> Result<Self, Self::Error> {
64        bounded::raw::NonZeroU32::from_usize(actual)
65            .map(Self)
66            .ok_or_else(|| table::Invalid::from_bounds(actual, 1, route::SLOT_MASK as usize))
67    }
68}
69
70impl TryFrom<capacity::Count> for Capacity {
71    type Error = table::Invalid;
72
73    fn try_from(actual: capacity::Count) -> Result<Self, Self::Error> {
74        bounded::raw::NonZeroU32::new(actual.raw())
75            .map(Self)
76            .ok_or_else(|| table::Invalid::from_bounds(actual.get(), 1, route::SLOT_MASK as usize))
77    }
78}
79
80impl From<Capacity> for table::Capacity {
81    fn from(capacity: Capacity) -> Self {
82        Self(capacity::Count::new(capacity.0.get()))
83    }
84}
85
86// SAFETY: the retained nonzero connection domain is bounded by SLOT_MASK, which is one immutable u32 extent.
87unsafe impl depot::raw::Count for Capacity {
88    const MAX: u32 = route::SLOT_MASK as u32;
89
90    fn count(self) -> capacity::Count {
91        capacity::Count::new(self.0.get())
92    }
93}