1#![cfg_attr(not(feature = "std"), no_std)]
22
23use core::future::Future;
24
25pub type ExecutorResult<T> = Result<T, ExecutorError>;
30
31#[derive(Debug)]
32#[cfg_attr(feature = "std", derive(thiserror::Error))]
33pub enum ExecutorError {
34 #[cfg_attr(feature = "std", error("Spawn failed: {message}"))]
35 SpawnFailed {
36 #[cfg(feature = "std")]
37 message: String,
38 #[cfg(not(feature = "std"))]
39 message: &'static str,
40 },
41
42 #[cfg_attr(feature = "std", error("Runtime unavailable: {message}"))]
43 RuntimeUnavailable {
44 #[cfg(feature = "std")]
45 message: String,
46 #[cfg(not(feature = "std"))]
47 message: &'static str,
48 },
49
50 #[cfg_attr(feature = "std", error("Task join failed: {message}"))]
51 TaskJoinFailed {
52 #[cfg(feature = "std")]
53 message: String,
54 #[cfg(not(feature = "std"))]
55 message: &'static str,
56 },
57}
58
59pub trait RuntimeAdapter: Send + Sync + 'static {
65 fn runtime_name() -> &'static str
66 where
67 Self: Sized;
68}
69
70pub trait TimeOps: RuntimeAdapter {
72 type Instant: Clone + Send + Sync + core::fmt::Debug + 'static;
73 type Duration: Clone + Send + Sync + core::fmt::Debug + 'static;
74
75 fn now(&self) -> Self::Instant;
76 fn duration_since(
77 &self,
78 later: Self::Instant,
79 earlier: Self::Instant,
80 ) -> Option<Self::Duration>;
81 fn millis(&self, ms: u64) -> Self::Duration;
82 fn secs(&self, secs: u64) -> Self::Duration;
83 fn micros(&self, micros: u64) -> Self::Duration;
84 fn sleep(&self, duration: Self::Duration) -> impl Future<Output = ()> + Send;
85}
86
87pub trait Logger: RuntimeAdapter {
89 fn info(&self, message: &str);
90 fn debug(&self, message: &str);
91 fn warn(&self, message: &str);
92 fn error(&self, message: &str);
93}
94
95pub trait Spawn: RuntimeAdapter {
97 type SpawnToken: Send + 'static;
98 fn spawn<F>(&self, future: F) -> ExecutorResult<Self::SpawnToken>
99 where
100 F: Future<Output = ()> + Send + 'static;
101}
102
103pub trait Runtime: RuntimeAdapter + TimeOps + Logger + Spawn {
109 fn runtime_info(&self) -> RuntimeInfo
110 where
111 Self: Sized,
112 {
113 RuntimeInfo {
114 name: Self::runtime_name(),
115 }
116 }
117}
118
119impl<T> Runtime for T where T: RuntimeAdapter + TimeOps + Logger + Spawn {}
121
122#[derive(Debug, Clone)]
123pub struct RuntimeInfo {
124 pub name: &'static str,
125}