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(unsafe { num::NonZero::<u64>::new_unchecked(raw) })
}
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> {
if (raw >> route::SHIFT) as u8 != route::FRAMEWORK {
return Err(raw);
}
Ok(Self(unsafe { num::NonZero::<u64>::new_unchecked(raw) }))
}
}
const _: () = {
assert!(mem::size_of::<Control>() == mem::size_of::<u64>());
assert!(mem::size_of::<Option<Control>>() == mem::size_of::<Control>());
};