pub type BoxFuture =
core::pin::Pin<alloc::boxed::Box<dyn core::future::Future<Output = ()> + Send + 'static>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
Debug,
Info,
Warn,
Error,
}
pub trait RuntimeOps: Send + Sync {
fn name(&self) -> &'static str;
fn sleep(&self, d: core::time::Duration) -> BoxFuture;
fn log(&self, level: LogLevel, msg: &str);
fn new_clock(&self) -> alloc::sync::Arc<dyn InstanceClock>;
}
use crate::clock::InstanceClock;
pub type ExecutorResult<T> = Result<T, ExecutorError>;
#[derive(Debug, thiserror::Error)]
pub enum ExecutorError {
#[error("Join queue closed")]
QueueClosed,
}
#[doc(hidden)]
pub mod test_support {
use super::{LogLevel, RuntimeOps};
use crate::clock::{InstanceClock, Stamp};
use alloc::sync::Arc;
#[cfg(target_has_atomic = "64")]
use portable_atomic::{AtomicU64, Ordering};
#[derive(Clone, Copy, Debug, Default)]
pub struct ZeroClock;
impl InstanceClock for ZeroClock {
fn now(&self) -> Stamp {
Stamp::from_nanos(0)
}
fn unix_time(&self) -> Option<(u64, u32)> {
None
}
}
#[cfg(target_has_atomic = "64")]
#[derive(Debug, Default)]
pub struct ManualClock(AtomicU64);
#[cfg(target_has_atomic = "64")]
impl ManualClock {
pub fn new() -> Self {
Self::default()
}
pub fn advance(&self, d: core::time::Duration) {
let ns = crate::clock::stamp_from_duration(d).as_nanos();
self.0.fetch_add(ns, Ordering::SeqCst);
}
}
#[cfg(target_has_atomic = "64")]
impl InstanceClock for ManualClock {
fn now(&self) -> Stamp {
Stamp::from_nanos(self.0.load(Ordering::SeqCst))
}
fn unix_time(&self) -> Option<(u64, u32)> {
None
}
}
#[cfg(target_has_atomic = "64")]
#[derive(Clone, Debug, Default)]
pub struct ManualClockRuntimeOps {
pub clock: Arc<ManualClock>,
}
#[cfg(target_has_atomic = "64")]
impl RuntimeOps for ManualClockRuntimeOps {
fn name(&self) -> &'static str {
"manual-clock"
}
fn sleep(&self, _d: core::time::Duration) -> super::BoxFuture {
alloc::boxed::Box::pin(core::future::ready(()))
}
fn log(&self, _level: LogLevel, _msg: &str) {}
fn new_clock(&self) -> Arc<dyn InstanceClock> {
self.clock.clone()
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct NoopRuntimeOps;
impl RuntimeOps for NoopRuntimeOps {
fn name(&self) -> &'static str {
"noop"
}
fn sleep(&self, _d: core::time::Duration) -> super::BoxFuture {
alloc::boxed::Box::pin(core::future::ready(()))
}
fn log(&self, _level: LogLevel, _msg: &str) {}
fn new_clock(&self) -> Arc<dyn InstanceClock> {
Arc::new(ZeroClock)
}
}
pub async fn assert_runtime_ops_contract(ops: &dyn RuntimeOps) {
assert!(!ops.name().is_empty(), "name() must be non-empty");
let clock = ops.new_clock();
let s0 = clock.now();
let s1 = clock.now();
assert!(
s1 >= s0,
"instance clock must be nondecreasing ({s1:?} < {s0:?})"
);
ops.log(LogLevel::Debug, "runtime-ops contract: debug");
ops.log(LogLevel::Info, "runtime-ops contract: info");
ops.log(LogLevel::Warn, "runtime-ops contract: warn");
ops.log(LogLevel::Error, "runtime-ops contract: error");
ops.sleep(core::time::Duration::ZERO).await;
if let Some((secs, nanos)) = clock.unix_time() {
assert!(
nanos < 1_000_000_000,
"unix_time nanos out of range: {nanos}"
);
assert!(
secs > 1_600_000_000,
"unix_time seconds implausible: {secs}"
);
}
}
}