Skip to main content

dope_core/driver/route/table/
key.rs

1use core::{fmt, hash};
2
3use o3::collections::slab::external;
4
5use crate::driver::route::{self, table};
6
7type ExternalKey<Tag> = external::Key<route::Epoch, Tag>;
8
9#[derive(Clone, Copy)]
10#[repr(transparent)]
11pub struct Key<Tag: route::Tag>(table::Parts<Tag>);
12
13impl<Tag: route::Tag> PartialEq for Key<Tag> {
14    fn eq(&self, other: &Self) -> bool {
15        self.0 == other.0
16    }
17}
18
19impl<Tag: route::Tag> Eq for Key<Tag> {}
20
21impl<Tag: route::Tag> hash::Hash for Key<Tag> {
22    fn hash<H: hash::Hasher>(&self, state: &mut H) {
23        self.0.hash(state);
24    }
25}
26
27impl<Tag: route::Tag> fmt::Debug for Key<Tag> {
28    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
29        formatter
30            .debug_struct("Key")
31            .field("index", &self.index())
32            .field("epoch", &self.epoch())
33            .finish()
34    }
35}
36
37impl<Tag: route::Tag> Key<Tag> {
38    pub(in crate::driver::route::table) const fn new(
39        slot: route::SlotIndex,
40        epoch: route::Epoch,
41    ) -> Self {
42        Self(table::Parts::from_components(slot, epoch))
43    }
44
45    pub(in crate::driver::route::table) fn from_external(key: ExternalKey<Tag>) -> Self {
46        Self::new(
47            route::SlotIndex::from_bounded(key.index()),
48            key.generation(),
49        )
50    }
51
52    pub(in crate::driver::route::table) fn external(self) -> ExternalKey<Tag> {
53        ExternalKey::new(self.index(), self.epoch())
54    }
55
56    pub(in crate::driver::route::table) fn external_parts(
57        parts: table::Parts<Tag>,
58    ) -> ExternalKey<Tag> {
59        ExternalKey::new(parts.index(), parts.epoch())
60    }
61
62    pub const fn index(self) -> u32 {
63        self.0.slot().raw()
64    }
65
66    pub const fn slot(self) -> route::SlotIndex {
67        self.0.slot()
68    }
69
70    pub const fn epoch(self) -> route::Epoch {
71        self.0.epoch()
72    }
73
74    pub const fn generation(self) -> route::Epoch {
75        self.epoch()
76    }
77
78    pub const fn parts(self) -> table::Parts<Tag> {
79        self.0
80    }
81}
82
83const _: () = {
84    assert!(core::mem::size_of::<Key<route::KeyTag<1>>>() == 2 * core::mem::size_of::<u64>());
85};