#![cfg_attr(not(feature = "std"), no_std)]
use core::future::Future;
pub mod join;
pub use join::{JoinFanInRuntime, JoinQueue, JoinReceiver, JoinSender};
pub type ExecutorResult<T> = Result<T, ExecutorError>;
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum ExecutorError {
#[cfg_attr(feature = "std", error("Spawn failed: {message}"))]
SpawnFailed {
#[cfg(feature = "std")]
message: String,
#[cfg(not(feature = "std"))]
message: &'static str,
},
#[cfg_attr(feature = "std", error("Runtime unavailable: {message}"))]
RuntimeUnavailable {
#[cfg(feature = "std")]
message: String,
#[cfg(not(feature = "std"))]
message: &'static str,
},
#[cfg_attr(feature = "std", error("Task join failed: {message}"))]
TaskJoinFailed {
#[cfg(feature = "std")]
message: String,
#[cfg(not(feature = "std"))]
message: &'static str,
},
#[cfg_attr(feature = "std", error("Join queue closed"))]
QueueClosed,
}
pub trait RuntimeAdapter: Send + Sync + 'static {
fn runtime_name() -> &'static str
where
Self: Sized;
}
pub trait TimeOps: RuntimeAdapter {
type Instant: Clone + Send + Sync + core::fmt::Debug + 'static;
type Duration: Clone + Send + Sync + core::fmt::Debug + 'static;
fn now(&self) -> Self::Instant;
fn duration_since(
&self,
later: Self::Instant,
earlier: Self::Instant,
) -> Option<Self::Duration>;
fn millis(&self, ms: u64) -> Self::Duration;
fn secs(&self, secs: u64) -> Self::Duration;
fn micros(&self, micros: u64) -> Self::Duration;
fn sleep(&self, duration: Self::Duration) -> impl Future<Output = ()> + Send;
fn duration_as_nanos(&self, duration: Self::Duration) -> u64;
}
pub trait Logger: RuntimeAdapter {
fn info(&self, message: &str);
fn debug(&self, message: &str);
fn warn(&self, message: &str);
fn error(&self, message: &str);
}
pub trait Spawn: RuntimeAdapter {
type SpawnToken: Send + 'static;
fn spawn<F>(&self, future: F) -> ExecutorResult<Self::SpawnToken>
where
F: Future<Output = ()> + Send + 'static;
}
pub trait Runtime: RuntimeAdapter + TimeOps + Logger + Spawn {
fn runtime_info(&self) -> RuntimeInfo
where
Self: Sized,
{
RuntimeInfo {
name: Self::runtime_name(),
}
}
}
impl<T> Runtime for T where T: RuntimeAdapter + TimeOps + Logger + Spawn {}
#[derive(Debug, Clone)]
pub struct RuntimeInfo {
pub name: &'static str,
}