use crate::work::Work;
use crate::work_result::WorkResult;
use async_trait::async_trait;
use std::future::Future;
pub struct WorkFn<F> {
f: F,
}
#[async_trait]
impl<F, Fut> Work for WorkFn<F>
where
F: Fn() -> Fut + Send + Sync,
Fut: Future<Output = WorkResult<()>> + Send,
{
async fn execute(&self) -> WorkResult<()> {
(self.f)().await
}
}
pub fn work<F, Fut>(f: F) -> WorkFn<F>
where
F: Fn() -> Fut + Send + Sync,
Fut: Future<Output = WorkResult<()>> + Send,
{
WorkFn { f }
}
pub(crate) type BoxWork = Box<dyn Work + Send + Sync>;
#[async_trait]
impl Work for Box<dyn Work + Send + Sync> {
async fn execute(&self) -> WorkResult<()> {
(**self).execute().await
}
}