From 9cb7781a2e7fc7378600ef921a6ae47145e2d84c Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 7 Nov 2023 20:37:39 -0500 Subject: [PATCH] feat: fut: add `spawn_on_current_runner` --- src/fut.ml | 5 +++++ src/fut.mli | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/fut.ml b/src/fut.ml index 639a503b..ad34cefe 100644 --- a/src/fut.ml +++ b/src/fut.ml @@ -100,6 +100,11 @@ let spawn ~on f : _ t = Runner.run_async on task; fut +let spawn_on_current_runner f : _ t = + match Runner.get_current_runner () with + | None -> failwith "Fut.spawn_on_current_runner: not running on a runner" + | Some on -> spawn ~on f + let reify_error (f : 'a t) : 'a or_error t = match peek f with | Some res -> return res diff --git a/src/fut.mli b/src/fut.mli index 944a9525..99b04a8d 100644 --- a/src/fut.mli +++ b/src/fut.mli @@ -85,6 +85,15 @@ val spawn : on:Runner.t -> (unit -> 'a) -> 'a t (** [spaw ~on f] runs [f()] on the given runner [on], and return a future that will hold its result. *) +val spawn_on_current_runner : (unit -> 'a) -> 'a t +(** This must be run from inside a runner, and schedules + the new task on it as well. + + See {!Runner.get_current_runner} to see how the runner is found. + + @since NEXT_RELEASE + @raise Failure if run from outside a runner. *) + val reify_error : 'a t -> 'a or_error t (** [reify_error fut] turns a failing future into a non-failing one that contain [Error (exn, bt)]. A non-failing future