Skip to main content

dope_core/driver/route/token/
control.rs

1use core::{mem, num};
2
3use crate::driver::route::{self, Route, indices, kind, token};
4
5/// One framework control decoded without manufacturing a keyed identity.
6#[derive(Clone, Copy, Eq, PartialEq, Debug)]
7#[repr(transparent)]
8pub struct Control(num::NonZero<u64>);
9
10impl From<Control> for num::NonZero<u64> {
11    fn from(control: Control) -> Self {
12        control.0
13    }
14}
15
16impl Control {
17    pub(crate) const fn new(kind: kind::Code, slot: indices::SlotIndex) -> Self {
18        let raw = token::metadata(Route::FRAMEWORK, kind, slot);
19        Self(num::NonZero::<u64>::MIN.saturating_add(raw - 1))
20    }
21
22    pub const fn slot(self) -> indices::SlotIndex {
23        indices::SlotIndex::from_field(self.0.get())
24    }
25}
26
27impl TryFrom<u64> for Control {
28    type Error = u64;
29
30    fn try_from(raw: u64) -> Result<Self, Self::Error> {
31        match num::NonZero::new(raw) {
32            Some(word) if (word.get() >> route::SHIFT) as u8 == route::FRAMEWORK => Ok(Self(word)),
33            _ => Err(raw),
34        }
35    }
36}
37
38const _: () = {
39    assert!(mem::size_of::<Control>() == mem::size_of::<u64>());
40    assert!(mem::size_of::<Option<Control>>() == mem::size_of::<Control>());
41};