From bdd30f7ab8c2280c368b86cb12baec0578669d44 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 20 Jun 2024 01:11:21 +0200 Subject: Introduce `and_then` methods for fallible `Task`s --- runtime/src/task.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'runtime') 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 Task { } } +impl Task> { + /// 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( + self, + f: impl Fn(T) -> Task + MaybeSend + 'static, + ) -> Task + where + T: MaybeSend + 'static, + A: MaybeSend + 'static, + { + self.then(move |option| option.map_or_else(Task::none, &f)) + } +} + +impl Task> { + /// 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( + self, + f: impl Fn(T) -> Task + MaybeSend + 'static, + ) -> Task + where + T: MaybeSend + 'static, + E: MaybeSend + 'static, + A: MaybeSend + 'static, + { + self.then(move |option| option.map_or_else(|_| Task::none(), &f)) + } +} + impl From<()> for Task where T: MaybeSend + 'static, -- cgit