use crate::clock::{InstanceClock, Stamp};
use crate::executor::{BoxFuture, LogLevel, RuntimeOps};
use alloc::sync::Arc;
#[derive(Clone)]
pub struct RuntimeContext {
ops: Arc<dyn RuntimeOps>,
clock: Arc<dyn InstanceClock>,
}
impl RuntimeContext {
pub fn new(ops: Arc<dyn RuntimeOps>) -> Self {
let clock = ops.new_clock();
Self { ops, clock }
}
pub fn with_clock(ops: Arc<dyn RuntimeOps>, clock: Arc<dyn InstanceClock>) -> Self {
Self { ops, clock }
}
pub fn time(&self) -> Time<'_> {
Time {
ops: &*self.ops,
clock: &*self.clock,
}
}
pub fn clock(&self) -> &Arc<dyn InstanceClock> {
&self.clock
}
pub fn log(&self) -> Log<'_> {
Log { ops: &*self.ops }
}
pub fn ops(&self) -> &Arc<dyn RuntimeOps> {
&self.ops
}
}
pub struct Time<'a> {
ops: &'a dyn RuntimeOps,
clock: &'a dyn InstanceClock,
}
impl Time<'_> {
pub fn now(&self) -> Stamp {
self.clock.now()
}
pub fn unix_time(&self) -> Option<(u64, u32)> {
self.clock.unix_time()
}
pub fn sleep(&self, duration: core::time::Duration) -> BoxFuture {
self.ops.sleep(duration)
}
pub fn sleep_millis(&self, ms: u64) -> BoxFuture {
self.ops.sleep(core::time::Duration::from_millis(ms))
}
pub fn sleep_secs(&self, secs: u64) -> BoxFuture {
self.ops.sleep(core::time::Duration::from_secs(secs))
}
}
pub struct Log<'a> {
ops: &'a dyn RuntimeOps,
}
impl Log<'_> {
pub fn info(&self, message: &str) {
self.ops.log(LogLevel::Info, message)
}
pub fn debug(&self, message: &str) {
self.ops.log(LogLevel::Debug, message)
}
pub fn warn(&self, message: &str) {
self.ops.log(LogLevel::Warn, message)
}
pub fn error(&self, message: &str) {
self.ops.log(LogLevel::Error, message)
}
}