dope-core 0.76.0

Owned native I/O and completion driver for dope
Documentation
use std::{mem, task, time};

use crate::{
    driver::schedule::clock::{self, timer},
    executor::task::{Fiber, context},
};

pub const MAXIMUM_DURATION: time::Duration = time::Duration::new(i64::MAX as u64 / 2, 999_999_999);

/// Sleep could not be represented or admitted to the fixed timer store.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum Error {
    Duration { actual: time::Duration },
    Capacity,
}

impl From<timer::Exhausted> for Error {
    fn from(_: timer::Exhausted) -> Self {
        Self::Capacity
    }
}

#[must_use = "a fiber does nothing unless it is driven"]
pub struct Sleep<'d, T = time::Duration> {
    alarm: timer::Alarm<'d>,
    start: T,
}

impl<'d> Sleep<'d> {
    pub fn new(timer: timer::Timer<'d>, duration: time::Duration) -> Result<Self, Error> {
        if duration > MAXIMUM_DURATION {
            return Err(Error::Duration { actual: duration });
        }
        Ok(Self {
            alarm: timer.try_alarm()?,
            start: duration,
        })
    }

    pub fn until(
        timer: timer::Timer<'d>,
        deadline: clock::Deadline<'d>,
    ) -> Result<Sleep<'d, clock::Deadline<'d>>, Error> {
        Ok(Sleep {
            alarm: timer.try_alarm()?,
            start: deadline,
        })
    }
}

impl<'d> Sleep<'d> {
    /// Polls one admitted transition; its owner ends polling on Ready.
    pub fn poll_sleep(&mut self, context: &mut context::Context<'_, 'd>) -> task::Poll<()> {
        let now = context.driver().driver_ref().clock().now();
        self.alarm
            .poll_after(&self.start, now, context.completion())
    }
}

impl<'d> Sleep<'d, clock::Deadline<'d>> {
    /// Polls one admitted transition; its owner ends polling on Ready.
    pub fn poll_sleep(&mut self, context: &mut context::Context<'_, 'd>) -> task::Poll<()> {
        let now = context.driver().driver_ref().clock().now();
        self.alarm
            .poll_until(&self.start, now, context.completion())
    }
}

impl<'d> Fiber<'d> for Sleep<'d> {
    type Output = ();

    fn poll(call: context::PollCall<'_, '_, 'd, Self>) -> task::Poll<()> {
        let (this, context) = call.into_parts();
        this.get_mut().poll_sleep(context)
    }
}

impl<'d> Fiber<'d> for Sleep<'d, clock::Deadline<'d>> {
    type Output = ();

    fn poll(call: context::PollCall<'_, '_, 'd, Self>) -> task::Poll<()> {
        let (this, context) = call.into_parts();
        this.get_mut().poll_sleep(context)
    }
}

const _: () = assert!(mem::size_of::<Sleep<'static>>() == 4 * mem::size_of::<usize>());
const _: () = assert!(mem::size_of::<Error>() == 3 * mem::size_of::<usize>());
const _: () =
    assert!(mem::size_of::<Result<Sleep<'static>, Error>>() == 4 * mem::size_of::<usize>());
const _: () = {
    assert!(
        mem::size_of::<Sleep<'static, clock::Deadline<'static>>>() == 4 * mem::size_of::<usize>()
    );
    assert!(
        mem::size_of::<Result<Sleep<'static, clock::Deadline<'static>>, Error>>()
            == 4 * mem::size_of::<usize>()
    );
};