dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use dope_sys::{io::reactor::event, thread::signal::Source};
use std::{
    cell::Cell,
    convert::Infallible,
    ops::{self, ControlFlow},
    os::fd,
    task::Poll,
};

use crate::{
    Backend, Errno,
    driver::schedule::ready::wake::{self, store::Slot},
    executor::{
        self,
        drive::{self, clock},
        root, session,
    },
};

/// Owns the process signal across executor generations.
pub struct Signal<'process> {
    source: Source<'process, Backend>,
    result: Cell<Option<Result<(), Errno>>>,
}

impl<'process> Signal<'process> {
    pub const fn new(source: Source<'process, Backend>) -> Self {
        Self {
            source,
            result: Cell::new(None),
        }
    }

    pub fn result(&self) -> Option<Result<(), Errno>> {
        self.result.get()
    }
}

impl fd::AsFd for Signal<'_> {
    fn as_fd(&self) -> fd::BorrowedFd<'_> {
        self.source.as_fd()
    }
}

impl executor::Startup for &mut Signal<'_> {
    type Error = Infallible;
    type Target = Infallible;

    fn failed(target: &Self::Target) {
        match *target {}
    }

    fn observed(&self, _request: executor::Requested<Self>) -> ControlFlow<()> {
        if self.result.get().is_none() {
            self.result.set(Some(self.source.wait()));
        }
        ControlFlow::Break(())
    }
}

impl executor::Source for &mut Signal<'_> {}

/// One reborrowable waiter inside its live application.
pub struct Wait<'signal, 'd> {
    result: &'signal Cell<Option<Result<(), Errno>>>,
    wake: &'signal Slot<'d>,
}

impl<'d> Wait<'_, 'd> {
    pub fn poll(&mut self, wake: wake::Waker<'d>) -> Poll<Result<(), Errno>> {
        match self.result.get() {
            Some(result) => Poll::Ready(result),
            None => {
                self.wake.set(wake);
                Poll::Pending
            }
        }
    }
}

/// Keeps the root alive to perform its own shutdown cleanup.
pub struct App<'signal, 'app, 'env: 'd, 'd: 'app, D: executor::raw::Layout<'d>, Report, Q> {
    app: &'signal mut session::App<'app, 'env, 'd, D, Report, Q>,
    wake: &'signal Slot<'d>,
}

impl<'app, 'env, 'd, 'source, 'process, D, Report: event::Report<Event = crate::Reported>>
    App<'_, 'app, 'env, 'd, D, Report, &'source mut Signal<'process>>
where
    D: executor::raw::App<'d>,
{
    pub fn with<R>(
        app: &mut session::App<'app, 'env, 'd, D, Report, &'source mut Signal<'process>>,
        run: impl for<'signal> FnOnce(
            App<'signal, 'app, 'env, 'd, D, Report, &'source mut Signal<'process>>,
            Wait<'signal, 'd>,
        ) -> R,
    ) -> R {
        let wake = Slot::empty();
        let source = app.core.source;
        let result = &source.result;
        run(
            App { app, wake: &wake },
            Wait {
                result,
                wake: &wake,
            },
        )
    }
}

impl<'d, D, Report: event::Report<Event = crate::Reported>, Q> App<'_, '_, '_, 'd, D, Report, Q>
where
    D: executor::raw::App<'d>,
    Q: executor::Startup,
{
    pub fn drive<R>(&mut self, root: R) -> Result<R::Output, drive::Error>
    where
        R: root::Root<'d>,
    {
        let source = self.app.core.source;
        self.app
            .poll_root(clock::wall(), root, &(source, self.wake))?
            .complete()
    }
}

impl<'app, 'env, 'd, D: executor::raw::Layout<'d>, Report, Q> ops::Deref
    for App<'_, 'app, 'env, 'd, D, Report, Q>
{
    type Target = session::App<'app, 'env, 'd, D, Report, Q>;

    fn deref(&self) -> &Self::Target {
        self.app
    }
}

impl<'d, D: executor::raw::Layout<'d>, Report, Q> ops::DerefMut
    for App<'_, '_, '_, 'd, D, Report, Q>
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.app
    }
}