dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use core::{mem, num};

use crate::driver::route::{self, Route, indices, kind, token};

/// One framework control decoded without manufacturing a keyed identity.
#[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);
        // SAFETY: the nonzero framework route occupies the high byte.
        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);
        }
        // SAFETY: a framework-tagged high byte makes the complete word nonzero.
        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>());
};