add Fut.get_or_fail{,_exn}

This commit is contained in:
Simon Cruanes 2023-06-14 11:33:24 -04:00
parent 483392986c
commit dd5a177a5f
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 29 additions and 0 deletions

View file

@ -28,6 +28,19 @@ let[@inline] peek self : _ option =
| Done x -> Some x
| Waiting _ -> None
exception Not_ready
let[@inline] get_or_fail self =
match A.get self.st with
| Done x -> x
| Waiting _ -> raise Not_ready
let[@inline] get_or_fail_exn self =
match A.get self.st with
| Done (Ok x) -> x
| Done (Error (exn, bt)) -> Printexc.raise_with_backtrace exn bt
| Waiting _ -> raise Not_ready
let on_result (self : _ t) (f : _ waiter) : unit =
while
let st = A.get self.st in

View file

@ -59,6 +59,22 @@ val peek : 'a t -> 'a or_error option
(** [peek fut] returns [Some r] if [fut] is currently resolved with [r],
and [None] if [fut] is not resolved yet. *)
exception Not_ready
(** @since 0.2 *)
val get_or_fail : 'a t -> 'a or_error
(** [get_or_fail fut] obtains the result from [fut] if it's fulfilled
(i.e. if [peek fut] returns [Some res], [get_or_fail fut] returns [res]).
@raise Not_ready if the future is not ready.
@since 0.2 *)
val get_or_fail_exn : 'a t -> 'a
(** [get_or_fail_exn fut] obtains the result from [fut] if it's fulfilled,
like {!get_or_fail}. If the result is an [Error _], the exception inside
is re-raised.
@raise Not_ready if the future is not ready.
@since 0.2 *)
(** {2 Combinators} *)
val spawn : on:Pool.t -> (unit -> 'a) -> 'a t