dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use core::task;
use o3::base::Either;

use crate::{
    executor::task::context,
    executor::task::{Fiber, Side},
};

#[pin_project::pin_project]
#[must_use = "a fiber does nothing unless it is driven"]
/// Resolves with the first completed child, preferring `left` when both run.
/// A budget-skipped child resumes first on the next poll.
/// Completion ends the owner's polling authority for this race.
#[derive(Debug)]
pub struct Race<L, R> {
    #[pin]
    left: L,
    #[pin]
    right: R,
    resume: Side,
}

impl<L, R> Race<L, R> {
    pub const fn new(left: L, right: R) -> Self {
        Self {
            left,
            right,
            resume: Side::Left,
        }
    }
}

impl<'d, L, R> Fiber<'d> for Race<L, R>
where
    L: Fiber<'d>,
    R: Fiber<'d>,
{
    type Output = Either<L::Output, R::Output>;

    fn poll(call: context::PollCall<'_, '_, 'd, Self>) -> task::Poll<Self::Output> {
        use core::task::Poll;

        let (this, context) = call.into_parts();
        let this = this.project();
        match *this.resume {
            Side::Left => {
                if let Poll::Ready(output) = L::poll(context::PollCall {
                    fiber: this.left,
                    context: &mut *context,
                }) {
                    return Poll::Ready(Either::Left(output));
                }
                *this.resume = Side::Right;
                let context::Continue::Permit(_permit) = context.try_continue() else {
                    return Poll::Pending;
                };
                match R::poll(context::PollCall {
                    fiber: this.right,
                    context,
                }) {
                    Poll::Ready(output) => Poll::Ready(Either::Right(output)),
                    Poll::Pending => {
                        *this.resume = Side::Left;
                        Poll::Pending
                    }
                }
            }
            Side::Right => {
                if let Poll::Ready(output) = R::poll(context::PollCall {
                    fiber: this.right,
                    context: &mut *context,
                }) {
                    return Poll::Ready(Either::Right(output));
                }
                *this.resume = Side::Left;
                let context::Continue::Permit(_permit) = context.try_continue() else {
                    return Poll::Pending;
                };
                match L::poll(context::PollCall {
                    fiber: this.left,
                    context,
                }) {
                    Poll::Ready(output) => Poll::Ready(Either::Left(output)),
                    Poll::Pending => Poll::Pending,
                }
            }
        }
    }
}

const _: () = {
    assert!(core::mem::size_of::<Race<(), ()>>() == core::mem::size_of::<Side>());
    assert!(core::mem::size_of::<Option<Race<(), ()>>>() == core::mem::size_of::<Race<(), ()>>());
};