dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use core::{pin, task::Poll};

use crate::executor::task::{
    self,
    context::{Context, Continue, PollCall},
};

/// One admitted child descent, retaining the original poll context.
/// Consuming it transfers the parent's credit to a task that owns completion.
pub struct Admission<'call, 'turn, 'd: 'turn> {
    pub(super) context: &'call mut Context<'turn, 'd>,
}

impl<'call, 'turn, 'd> Admission<'call, 'turn, 'd> {
    pub(crate) fn into_context(self) -> &'call mut Context<'turn, 'd> {
        self.context
    }

    /// Borrows the context for setup before the admitted child descent.
    pub fn context(&mut self) -> &mut Context<'turn, 'd> {
        &mut *self.context
    }

    /// Transfers the existing credit exactly once and returns the context for
    /// subsequent work. A completed task remains pending without repolling its
    /// former fiber; additional child polls through the context pay a credit.
    pub fn poll<F: task::Fiber<'d>>(
        self,
        fiber: pin::Pin<&mut task::Task<F>>,
    ) -> (Poll<F::Output>, &'call mut Context<'turn, 'd>) {
        let result = fiber.poll(&mut *self.context);
        (result, self.context)
    }

    /// Polls the owner's child slot and drops a completed child before returning its output.
    pub fn poll_slot<F: task::Fiber<'d>>(self, slot: pin::Pin<&mut Option<F>>) -> Poll<F::Output> {
        task::Task::poll_slot(slot, self.context)
    }

    /// Transfers the existing credit to one child stream and returns the same
    /// context. End drops the child; later admitted calls return `Ready(None)`
    /// without polling it again.
    pub fn poll_next<S: task::Stream<'d>>(
        self,
        stream: pin::Pin<&mut task::Task<S>>,
    ) -> (Poll<Option<S::Item>>, &'call mut Context<'turn, 'd>) {
        let result = stream.poll_next(&mut *self.context);
        (result, self.context)
    }
}

impl<'call, 'turn, 'd> Admission<'call, 'turn, 'd> {
    /// # Safety
    /// Own the unfinished pinned fiber and drop it before exposing its Ready output.
    /// Delegation may return only to the enclosing owner that performs that drop.
    pub unsafe fn invoke<F: task::Fiber<'d>>(
        self,
        fiber: pin::Pin<&mut F>,
    ) -> (Poll<F::Output>, &'call mut Context<'turn, 'd>) {
        let result = F::poll(PollCall {
            fiber,
            context: &mut *self.context,
        });
        (result, self.context)
    }

    /// # Safety
    /// Retain the original pinned stream until EOF, then end polling and drop it.
    pub unsafe fn advance<S: task::Stream<'d>>(
        self,
        stream: pin::Pin<&mut S>,
    ) -> Poll<Option<S::Item>> {
        S::poll_next(PollCall {
            fiber: stream,
            context: self.context,
        })
    }

    /// # Safety
    /// Own the unfinished final Pin and drop it on Ready before output use.
    /// Delegation may return only to the enclosing owner that performs that drop.
    pub unsafe fn budgeted<F: task::Fiber<'d>>(
        fiber: pin::Pin<&mut F>,
        context: &'call mut Context<'turn, 'd>,
    ) -> Poll<F::Output> {
        let Continue::Permit(_permit) = context.try_continue() else {
            return Poll::Pending;
        };
        F::poll(PollCall { fiber, context })
    }
}