dope-core 0.77.0

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

use crate::driver::retained::flight::storage::is_layout_valid;

/// Packed index and generation bits echoed by an external completion source.
#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
pub struct Echo<T: Copy, const INDEX_BITS: u32 = 32, const GENERATION_BITS: u32 = 32> {
    pub(super) raw: num::NonZero<u64>,
    pub(super) value: marker::PhantomData<fn() -> T>,
}

impl<T: Copy, const INDEX_BITS: u32, const GENERATION_BITS: u32>
    Echo<T, INDEX_BITS, GENERATION_BITS>
{
    pub const fn expose(self) -> u64 {
        self.raw.get()
    }

    pub fn from_exposed(raw: u64) -> Option<Self> {
        if !is_layout_valid::<INDEX_BITS, GENERATION_BITS>() {
            return None;
        }
        let width = INDEX_BITS + GENERATION_BITS;
        if (width < u64::BITS && raw >= 1u64 << width) || raw >> INDEX_BITS == 0 {
            return None;
        }
        Some(Self {
            raw: num::NonZero::<u64>::new(raw)?,
            value: marker::PhantomData,
        })
    }

    pub(super) const fn index(self) -> u32 {
        let mask = (1u64 << INDEX_BITS) - 1;
        (self.raw.get() & mask) as u32
    }

    pub(super) const fn matches(self, epoch: num::NonZero<u32>) -> bool {
        self.raw.get() >> INDEX_BITS == epoch.get() as u64
    }
}

impl<T: Copy, const INDEX_BITS: u32, const GENERATION_BITS: u32> PartialEq
    for Echo<T, INDEX_BITS, GENERATION_BITS>
{
    fn eq(&self, other: &Self) -> bool {
        self.raw == other.raw
    }
}

impl<T: Copy, const INDEX_BITS: u32, const GENERATION_BITS: u32> Eq
    for Echo<T, INDEX_BITS, GENERATION_BITS>
{
}