pub mod middleware;
mod outcome;
use std::fmt;
use std::future::Future;
pub use middleware::WorkerExt;
pub use outcome::{IntoOutcome, Outcome, Scope};
use crate::context::Context;
pub trait Worker<C>: Send + Sync + 'static {
fn handle(&self, cx: Context<C>) -> impl Future<Output = Outcome> + Send;
}
pub const fn from_fn<F>(f: F) -> FromFn<F> {
FromFn(f)
}
#[derive(Clone, Copy)]
pub struct FromFn<F>(F);
impl<C, F, Fut> Worker<C> for FromFn<F>
where
C: Send + 'static,
F: Fn(Context<C>) -> Fut + Send + Sync + 'static,
Fut: Future<Output: IntoOutcome> + Send,
{
fn handle(&self, cx: Context<C>) -> impl Future<Output = Outcome> + Send {
let future = (self.0)(cx);
async move { future.await.into_outcome() }
}
}
impl<F> fmt::Debug for FromFn<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FromFn").finish_non_exhaustive()
}
}