use core::future::Future;
use core::pin::Pin;
use core::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockOnError {
pub message: String,
}
impl BlockOnError {
pub fn not_supported(reason: &str) -> Self {
Self {
message: format!("Blocking not supported: {}", reason),
}
}
}
impl std::fmt::Display for BlockOnError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for BlockOnError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimeoutError;
impl std::fmt::Display for TimeoutError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Operation timed out")
}
}
impl std::error::Error for TimeoutError {}
pub trait FeagiAsyncRuntime: Send + Sync + 'static {
type TaskHandle<T: Send + 'static>: Future<Output = T> + Send + 'static;
fn spawn<F, T>(&self, fut: F) -> Self::TaskHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static;
fn delay(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
fn try_block_on<F, T>(&self, future: F) -> Result<T, BlockOnError>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static;
fn with_timeout<F, T>(
&self,
future: F,
timeout: Duration,
) -> Pin<Box<dyn Future<Output = Result<T, TimeoutError>> + Send + 'static>>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static;
}