pi-async-runtime 0.1.4

单线程串行及并发异步运行时的静态组合
Documentation
//! 计时、只读查询与关闭控制的直接组合,不复制状态或启动后台服务。
use pi_async_executor::ExecutorTimer;
use pi_async_task_queue::{ExternalQueueManager, ExternalTaskQueue, IdleQueueManager, IdleTaskQueue};
use crate::{instance::*, mode::RuntimeMode};
use pi_async_executor::{ExecutorClock, ExecutorHooks, IdlePolicy};
use pi_async_runtime_core::{
    CloseRequestStatus, CloseState, IdleState, LocalRuntimeAccessError, RuntimeControl, RuntimeId,
    RuntimeQuery, RuntimeStatus, RuntimeTime, RuntimeWait, Sleep, Timeout, TimeoutOn,
};
use pi_clock_manual::global::GlobalManualInstant;
use pi_result::RawResult;
use std::{future::Future, time::Duration};

fn local_timer_access<C, E, I, T>() -> RawResult<T::Access, LocalRuntimeAccessError>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
    crate::access::with_local::<C, E, I, T, _>(|handle, _| handle.shared().inner.timer_access())
}

impl<C, E, I, T> RuntimeTime for RuntimeHandle<C, E, I, T>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
    type Timer = T::Access;
    fn timeout(&self, duration: Duration) -> Timeout<Self::Timer> {
        Timeout::new(self.shared().inner.timer_access(), duration)
    }
    fn timeout_on<F: Future>(&self, future: F, duration: Duration) -> TimeoutOn<Self::Timer, F> {
        TimeoutOn::new(self.shared().inner.timer_access(), future, duration)
    }
    fn sleep(&self, duration: Duration) -> Sleep<Self::Timer> {
        Sleep::new(self.shared().inner.timer_access(), duration)
    }
    fn timeout_local(duration: Duration) -> RawResult<Timeout<Self::Timer>, LocalRuntimeAccessError> {
        Ok(Timeout::new_local(local_timer_access::<C, E, I, T>()?, duration))
    }
    fn timeout_on_local<F: Future>(
        future: F,
        duration: Duration,
    ) -> RawResult<TimeoutOn<Self::Timer, F>, LocalRuntimeAccessError> {
        Ok(TimeoutOn::new_local(local_timer_access::<C, E, I, T>()?, future, duration))
    }
    fn sleep_local(duration: Duration) -> RawResult<Sleep<Self::Timer>, LocalRuntimeAccessError> {
        Ok(Sleep::new_local(local_timer_access::<C, E, I, T>()?, duration))
    }
}
impl<C, E, I, T> RuntimeWait for RuntimeHandle<C, E, I, T>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{}
impl<C, E, I, T> RuntimeQuery for RuntimeHandle<C, E, I, T>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
    fn id(&self) -> RuntimeId {
        self.shared().inner.id()
    }
    fn status(&self) -> RuntimeStatus {
        self.shared().inner.status()
    }
    fn is_driving(&self) -> bool {
        self.shared().inner.is_driving()
    }
    fn last_idle_state(&self) -> IdleState {
        self.shared().inner.last_idle_state()
    }
    fn close_state(&self) -> CloseState {
        self.shared().inner.close_state()
    }
    fn external_task_count(&self) -> usize {
        self.shared().inner.external_task_count()
    }
    fn internal_task_count(&self) -> usize {
        self.shared().inner.internal_task_count()
    }
    fn timer_registration_count(&self) -> usize {
        self.shared().inner.timer_registration_count()
    }
    fn idle_task_count(&self) -> usize {
        self.shared().inner.idle_task_count()
    }
    fn in_flight_task_count(&self) -> usize {
        self.shared().inner.in_flight_task_count()
    }
    fn waiting_for_wake_count(&self) -> usize {
        self.shared().inner.waiting_for_wake_count()
    }
    fn pending_cancel_task_count(&self) -> usize {
        self.shared().inner.pending_cancel_task_count()
    }
}
impl<C, E, I, T> RuntimeControl for RuntimeHandle<C, E, I, T>
where
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalTaskQueue,
    I: IdleTaskQueue,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
{
    fn request_close(&self) -> CloseRequestStatus {
        self.shared().inner.request_close()
    }
}
impl<M, C, P, H, E, I, T> RuntimeTime for ConfiguredRuntime<M, C, P, H, E, I, T>
where
    M: RuntimeMode,
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalQueueManager,
    I: IdleQueueManager,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
    P: IdlePolicy,
    H: ExecutorHooks,
{
    type Timer = T::Access;
    fn timeout(&self, duration: Duration) -> Timeout<Self::Timer> {
        Timeout::new(self.shared().inner.timer_access(), duration)
    }
    fn timeout_on<F: Future>(&self, future: F, duration: Duration) -> TimeoutOn<Self::Timer, F> {
        TimeoutOn::new(self.shared().inner.timer_access(), future, duration)
    }
    fn sleep(&self, duration: Duration) -> Sleep<Self::Timer> {
        Sleep::new(self.shared().inner.timer_access(), duration)
    }
    fn timeout_local(duration: Duration) -> RawResult<Timeout<Self::Timer>, LocalRuntimeAccessError> {
        RuntimeHandle::<C, E::Handle, I::Handle, T>::timeout_local(duration)
    }
    fn timeout_on_local<F: Future>(
        future: F,
        duration: Duration,
    ) -> RawResult<TimeoutOn<Self::Timer, F>, LocalRuntimeAccessError> {
        RuntimeHandle::<C, E::Handle, I::Handle, T>::timeout_on_local(future, duration)
    }
    fn sleep_local(duration: Duration) -> RawResult<Sleep<Self::Timer>, LocalRuntimeAccessError> {
        RuntimeHandle::<C, E::Handle, I::Handle, T>::sleep_local(duration)
    }
}
impl<M, C, P, H, E, I, T> RuntimeWait for ConfiguredRuntime<M, C, P, H, E, I, T>
where
    M: RuntimeMode,
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalQueueManager,
    I: IdleQueueManager,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
    P: IdlePolicy,
    H: ExecutorHooks,
{
}
impl<M, C, P, H, E, I, T> RuntimeQuery for ConfiguredRuntime<M, C, P, H, E, I, T>
where
    M: RuntimeMode,
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalQueueManager,
    I: IdleQueueManager,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
    P: IdlePolicy,
    H: ExecutorHooks,
{
    fn id(&self) -> RuntimeId {
        self.shared().inner.id()
    }
    fn status(&self) -> RuntimeStatus {
        self.shared().inner.status()
    }
    fn is_driving(&self) -> bool {
        self.shared().inner.is_driving()
    }
    fn last_idle_state(&self) -> IdleState {
        self.shared().inner.last_idle_state()
    }
    fn close_state(&self) -> CloseState {
        self.shared().inner.close_state()
    }
    fn external_task_count(&self) -> usize {
        self.shared().inner.external_task_count()
    }
    fn internal_task_count(&self) -> usize {
        self.shared().inner.internal_task_count()
    }
    fn timer_registration_count(&self) -> usize {
        self.shared().inner.timer_registration_count()
    }
    fn idle_task_count(&self) -> usize {
        self.shared().inner.idle_task_count()
    }
    fn in_flight_task_count(&self) -> usize {
        self.shared().inner.in_flight_task_count()
    }
    fn waiting_for_wake_count(&self) -> usize {
        self.shared().inner.waiting_for_wake_count()
    }
    fn pending_cancel_task_count(&self) -> usize {
        self.shared().inner.pending_cancel_task_count()
    }
}
impl<M, C, P, H, E, I, T> RuntimeControl for ConfiguredRuntime<M, C, P, H, E, I, T>
where
    M: RuntimeMode,
    C: ExecutorClock<Instant = GlobalManualInstant>,
    E: ExternalQueueManager,
    I: IdleQueueManager,
    T: ExecutorTimer<Instant = GlobalManualInstant> + 'static,
    P: IdlePolicy,
    H: ExecutorHooks,
{
    fn request_close(&self) -> CloseRequestStatus {
        self.shared().inner.request_close()
    }
}