use core::{mem, num};
use crate::driver::route::{self, Route, indices, kind, token};
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[repr(transparent)]
pub struct Control(num::NonZero<u64>);
impl From<Control> for num::NonZero<u64> {
fn from(control: Control) -> Self {
control.0
}
}
impl Control {
pub(crate) const fn new(kind: kind::Code, slot: indices::SlotIndex) -> Self {
let raw = token::metadata(Route::FRAMEWORK, kind, slot);
Self(num::NonZero::<u64>::MIN.saturating_add(raw - 1))
}
pub const fn slot(self) -> indices::SlotIndex {
indices::SlotIndex::from_field(self.0.get())
}
}
impl TryFrom<u64> for Control {
type Error = u64;
fn try_from(raw: u64) -> Result<Self, Self::Error> {
match num::NonZero::new(raw) {
Some(word) if (word.get() >> route::SHIFT) as u8 == route::FRAMEWORK => Ok(Self(word)),
_ => Err(raw),
}
}
}
const _: () = {
assert!(mem::size_of::<Control>() == mem::size_of::<u64>());
assert!(mem::size_of::<Option<Control>>() == mem::size_of::<Control>());
};