diff --git a/src/fut.ml b/src/fut.ml index e5d7d235..0a16dcce 100644 --- a/src/fut.ml +++ b/src/fut.ml @@ -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 diff --git a/src/fut.mli b/src/fut.mli index b80e0c48..a0b10685 100644 --- a/src/fut.mli +++ b/src/fut.mli @@ -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