use std::convert::Infallible;
use either::Either;
use super::{DisabledJob, JobResult};
use crate::maybe_send::{MaybeSend, MaybeSendSync};
pub trait OpaqueJob: MaybeSendSync {
fn run(&mut self) -> impl Future<Output = JobResult> + MaybeSend;
fn name(&self) -> Option<&str> {
None
}
fn disable(self) -> DisabledJob<Self>
where
Self: Sized,
{
DisabledJob(self)
}
}
impl OpaqueJob for () {
async fn run(&mut self) -> JobResult {
JobResult::Ok
}
}
impl<J> OpaqueJob for Option<J>
where
J: OpaqueJob,
{
async fn run(&mut self) -> JobResult {
let Some(job) = self else {
return JobResult::Ok;
};
job.run().await
}
fn name(&self) -> Option<&str> {
self.as_ref().and_then(|x| x.name())
}
}
impl OpaqueJob for Infallible {
async fn run(&mut self) -> JobResult {
match *self {}
}
}
#[cfg(feature = "nightly")]
impl OpaqueJob for ! {
async fn run(&mut self) -> JobResult {
match *self {}
}
}
impl<J> OpaqueJob for &mut J
where
J: OpaqueJob,
{
fn run(&mut self) -> impl Future<Output = JobResult> + MaybeSend {
(*self).run()
}
}
impl<A, B> OpaqueJob for Either<A, B>
where
A: OpaqueJob,
B: OpaqueJob,
{
async fn run(&mut self) -> JobResult {
match self {
Either::Left(a) => a.run().await,
Either::Right(b) => b.run().await,
}
}
fn name(&self) -> Option<&str> {
match self {
Either::Left(a) => a.name(),
Either::Right(b) => b.name(),
}
}
}