summaryrefslogtreecommitdiffstats
path: root/runtime
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-06-20 01:11:21 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-06-20 01:11:21 +0200
commitbdd30f7ab8c2280c368b86cb12baec0578669d44 (patch)
tree4772d7614af229f8203c888c5f6bfea57f6c6b4b /runtime
parent65c8e08b440f012e4a26cb55ff5554cd36fde614 (diff)
downloadiced-bdd30f7ab8c2280c368b86cb12baec0578669d44.tar.gz
iced-bdd30f7ab8c2280c368b86cb12baec0578669d44.tar.bz2
iced-bdd30f7ab8c2280c368b86cb12baec0578669d44.zip
Introduce `and_then` methods for fallible `Task`s
Diffstat (limited to 'runtime')
-rw-r--r--runtime/src/task.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/runtime/src/task.rs b/runtime/src/task.rs
index 740360ac..b8a83d6d 100644
--- a/runtime/src/task.rs
+++ b/runtime/src/task.rs
@@ -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>
where
T: MaybeSend + 'static,