dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use std::{cell, pin::Pin};

use crate::{
    driver,
    driver::schedule::{app, ready::wake},
    executor::task::context,
};

mod capacity;
mod length;
mod sealed;

pub use capacity::Capacity;
pub use length::Length;
pub use sealed::links::{Queue, Slot, Waiter};

/// Exact registration state of a caller-owned waiter.
#[must_use = "registration state must be consumed"]
#[derive(Clone, Copy, Eq, PartialEq)]
#[repr(u8)]
pub enum Registration {
    Vacant,
    Active,
}

const _: () = assert!(std::mem::size_of::<Registration>() == core::mem::size_of::<u8>());

/// Exact reason a caller-owned waiter could not be registered.
pub enum Error<'d> {
    Foreign,
    QueueFull {
        capacity: Capacity,
        wake: wake::Waker<'d>,
    },
    SlotFull(wake::Waker<'d>),
}

/// Exact registration failure without moving the caller's retained wake capability.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Cause {
    Foreign,
    QueueFull { capacity: Capacity },
    SlotFull,
}

impl Error<'_> {
    pub const fn cause(&self) -> Cause {
        match self {
            Self::Foreign => Cause::Foreign,
            Self::QueueFull { capacity, .. } => Cause::QueueFull {
                capacity: *capacity,
            },
            Self::SlotFull(_) => Cause::SlotFull,
        }
    }
}

#[must_use = "Pending means the wake did not fit in this application turn"]
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum WakeStatus {
    Complete,
    Pending,
}

/// A notification snapshots the waiter count; re-registration cannot extend it.
pub struct Notifier<'d> {
    queue: Queue<'d>,
    remaining: cell::Cell<usize>,
}

impl<'d> Notifier<'d> {
    pub const fn new(driver: driver::Reference<'d>, capacity: Capacity) -> Self {
        Self {
            queue: Queue::new(driver, capacity),
            remaining: cell::Cell::new(0),
        }
    }

    pub fn try_register<'target, 'poll>(
        &'target self,
        waiter: Pin<&Waiter<'target, 'd>>,
        context: &context::Context<'poll, 'd>,
    ) -> Result<(), Error<'d>> {
        self.queue.try_register(waiter, context)
    }

    pub fn contains(&self, waiter: &Waiter<'_, 'd>) -> bool {
        self.queue.contains(waiter)
    }

    /// Starts a new round with the current waiter count.
    pub fn notify(&self) {
        self.remaining.set(self.queue.len().get());
    }

    /// Wakes at most one waiter within the application's work budget.
    pub fn flush(&self, work: app::Work<'_, 'd>) {
        let left = self.remaining.get();
        if left != 0 && self.queue.wake_one(work) == WakeStatus::Complete {
            self.remaining.set((left - 1).min(self.queue.len().get()));
        }
    }

    pub fn is_pending(&self) -> bool {
        self.remaining.get() != 0
    }
}

const _: () = assert!(
    std::mem::size_of::<Notifier<'static>>()
        == std::mem::size_of::<Queue<'static>>() + std::mem::size_of::<usize>()
);