use crate::{
combinators,
mode::{
Mode,
runtime,
},
};
pub type TaskOutput<'src, M, O> = <M as Mode<'src>>::Output<O>;
pub trait Task<'src, I, O>
where
O: Send + 'src,
{
#[doc(hidden)]
fn go<M>(self, mode: &'src M, input: I) -> TaskOutput<'src, M, O>
where
Self: Sized,
M: Mode<'src> + Send + Sync;
#[doc(hidden)]
#[cfg(feature = "blocking")]
fn go_blocking(
self,
mode: &'src runtime::Blocking,
input: I,
) -> TaskOutput<'src, runtime::Blocking, O>;
#[doc(hidden)]
#[cfg(feature = "async")]
fn go_async(self, mode: &'src runtime::Async, input: I) -> TaskOutput<'src, runtime::Async, O>
where
'src: 'static;
#[cfg(feature = "blocking")]
fn run_blocking(self, input: I) -> O
where
Self: Sized,
{
self.go_blocking(&runtime::Blocking, input)
}
#[cfg(feature = "async")]
fn run_async(self, input: I) -> impl Future<Output = O>
where
Self: Sized,
'src: 'static,
{
self.go_async(&runtime::Async, input)
}
#[inline(always)]
fn then<BO>(self, next: impl Task<'src, O, BO> + Send + 'src) -> impl Task<'src, I, BO>
where
Self: Sized,
BO: Send + 'src,
{
combinators::then(self, next)
}
}
pub trait TaskResultExt<'src, I, E, O>
where
Self: Task<'src, I, Result<O, E>> + Sized,
E: Send + 'src,
O: Send + 'src,
{
#[inline(always)]
fn map<BO>(
self,
next: impl Task<'src, O, BO> + Send + 'src,
) -> impl Task<'src, I, Result<BO, E>>
where
BO: Send + 'src,
{
combinators::map_result(self, next)
}
#[inline(always)]
fn and_then<BO>(
self,
next: impl Task<'src, O, Result<BO, E>> + Send + 'src,
) -> impl Task<'src, I, Result<BO, E>>
where
BO: Send + 'src,
{
combinators::and_then_result(self, next)
}
}
impl<'src, I, E, O, T> TaskResultExt<'src, I, E, O> for T
where
E: Send + 'src,
O: Send + 'src,
T: Task<'src, I, Result<O, E>>,
{
}
pub trait TaskOptionExt<'src, I, O>
where
Self: Task<'src, I, Option<O>> + Sized,
O: Send + 'src,
{
#[inline(always)]
fn map<BO>(self, next: impl Task<'src, O, BO> + Send + 'src) -> impl Task<'src, I, Option<BO>>
where
BO: Send + 'src,
{
combinators::map_option(self, next)
}
#[inline(always)]
fn and_then<BO>(
self,
next: impl Task<'src, O, Option<BO>> + Send + 'src,
) -> impl Task<'src, I, Option<BO>>
where
BO: Send + 'src,
{
combinators::and_then_option(self, next)
}
}
impl<'src, I, O, T> TaskOptionExt<'src, I, O> for T
where
O: Send + 'src,
T: Task<'src, I, Option<O>>,
{
}
pub trait TaskIteratorExt<'src, I, O, IO>
where
IO: Iterator<Item = O> + Send + 'src,
Self: Task<'src, I, IO> + Sized,
{
fn map<BO, IBO>(self, next: impl Task<'src, O, BO> + Clone + Send + 'src) -> impl Task<'src, I, IBO>
where
BO: Send + 'src,
IBO: Iterator<Item = BO> + Send + 'src,
{
combinators::map_iterator(self, next)
}
}
impl<'src, I, O, IO, T> TaskIteratorExt<'src, I, O, IO> for T
where
O: Send + 'src,
IO: Iterator<Item = O> + Send + 'src,
T: Task<'src, I, IO> + Sized,
{
}