Introduce and_then methods for fallible Tasks
This commit is contained in:
parent
65c8e08b44
commit
bdd30f7ab8
1 changed files with 33 additions and 0 deletions
|
|
@ -242,6 +242,39 @@ impl<T> Task<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Task<Option<T>> {
|
||||||
|
/// Executes a new [`Task`] after this one, only when it produces `Some` value.
|
||||||
|
///
|
||||||
|
/// The value is provided to the closure to create the subsequent [`Task`].
|
||||||
|
pub fn and_then<A>(
|
||||||
|
self,
|
||||||
|
f: impl Fn(T) -> Task<A> + MaybeSend + 'static,
|
||||||
|
) -> Task<A>
|
||||||
|
where
|
||||||
|
T: MaybeSend + 'static,
|
||||||
|
A: MaybeSend + 'static,
|
||||||
|
{
|
||||||
|
self.then(move |option| option.map_or_else(Task::none, &f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, E> Task<Result<T, E>> {
|
||||||
|
/// Executes a new [`Task`] after this one, only when it succeeds with an `Ok` value.
|
||||||
|
///
|
||||||
|
/// The success value is provided to the closure to create the subsequent [`Task`].
|
||||||
|
pub fn and_then<A>(
|
||||||
|
self,
|
||||||
|
f: impl Fn(T) -> Task<A> + MaybeSend + 'static,
|
||||||
|
) -> Task<A>
|
||||||
|
where
|
||||||
|
T: MaybeSend + 'static,
|
||||||
|
E: MaybeSend + 'static,
|
||||||
|
A: MaybeSend + 'static,
|
||||||
|
{
|
||||||
|
self.then(move |option| option.map_or_else(|_| Task::none(), &f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> From<()> for Task<T>
|
impl<T> From<()> for Task<T>
|
||||||
where
|
where
|
||||||
T: MaybeSend + 'static,
|
T: MaybeSend + 'static,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue