feat: fut: add spawn_on_current_runner

This commit is contained in:
Simon Cruanes 2023-11-07 20:37:39 -05:00
parent 0a432585c6
commit 9cb7781a2e
2 changed files with 14 additions and 0 deletions

View file

@ -100,6 +100,11 @@ let spawn ~on f : _ t =
Runner.run_async on task; Runner.run_async on task;
fut 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 = let reify_error (f : 'a t) : 'a or_error t =
match peek f with match peek f with
| Some res -> return res | Some res -> return res

View file

@ -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 (** [spaw ~on f] runs [f()] on the given runner [on], and return a future that will
hold its result. *) 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 val reify_error : 'a t -> 'a or_error t
(** [reify_error fut] turns a failing future into a non-failing (** [reify_error fut] turns a failing future into a non-failing
one that contain [Error (exn, bt)]. A non-failing future one that contain [Error (exn, bt)]. A non-failing future