dope-core 0.75.0

Owned native I/O and completion driver for dope
Documentation
use std::time;

use crate::{
    driver::schedule,
    executor::{
        self, drain,
        drive::{
            self,
            clock::{self, Bound, End, Time as _},
            turn::scope,
        },
        raw,
    },
};

pub(crate) fn run<'app, 'd: 'app, D, S, C, Q, Report: crate::io::Report>(
    mut state: scope::Scope<'_, 'app, 'd, D, S, Report>,
    source: Bound<C, End>,
    observer: &Q,
) -> Result<drain::Outcome, drive::Error>
where
    D: raw::Drain<'d>,
    C: clock::Source,
    Q: executor::Startup,
{
    {
        let (mut core, pending) = state.begin(source.start());
        // Unhandled events remain in pending; shutdown does not cancel draining.
        let (_events, _shutdown) = core.dispatch_native::<Q, Q>(pending, observer)?;
        core.activate(None);
        core.begin_drain();
    }
    loop {
        let tick = match source.elapsed() {
            Ok(tick) => tick,
            Err(clock::Elapsed) => {
                let (mut core, pending) = state.begin(source.start());
                let (_events, _shutdown) = core.dispatch_native::<Q, Q>(pending, observer)?;
                core.activate(None);
                core.force_drain();
                return Ok(drain::Outcome::Forced);
            }
        };
        let (mut core, pending) = state.begin(tick);
        let (events, _) = core.dispatch_native::<Q, Q>(pending, observer)?;
        core.activate(None);
        if !core.quiesce(events)? {
            continue;
        }
        let mut waits = schedule::Waits::new();
        let drain = core.drain(Some(&mut waits));
        let span = if core.quiet() {
            if drain.is_continue() && waits.as_ref().is_none() {
                return Ok(drain::Outcome::Quiesced);
            }
            let app = core.progress::<false>(if drain.is_continue() {
                Some(&mut waits)
            } else {
                None
            });
            if app.is_break() || drain.is_break() {
                Some(time::Duration::ZERO)
            } else {
                match waits.as_ref() {
                    Some(schedule::Wait::Until(due)) => Some(source.timeout(*due)),
                    _ => None,
                }
            }
        } else {
            Some(time::Duration::ZERO)
        };
        core.wait_for(span, source)?;
    }
}