feat(Ref): add protect function

This commit is contained in:
Simon Cruanes 2022-08-04 11:58:39 -04:00
parent d535cfe677
commit 43f82d7668
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 16 additions and 0 deletions

View file

@ -32,6 +32,17 @@ let swap a b =
a := !b;
b := x
let protect r x f =
let old = !r in
r := x;
try
let res = f () in
r := old;
res
with e ->
r := old;
raise e
let to_list r = [ !r ]
let to_iter r yield = yield !r
let pp pp_x out r = pp_x out !r

View file

@ -33,6 +33,11 @@ val swap : 'a t -> 'a t -> unit
(** [swap t1 t2] puts [!t2] in [t1] and [!t1] in [t2].
@since 1.4 *)
val protect : 'a t -> 'a -> (unit -> 'b) -> 'b
(** [protect r x f] sets [r := x]; calls [f()]; restores [r] to its old value;
and returns the result of [f()].
@since NEXT_RELEASE *)
val compare : 'a ord -> 'a t ord
val equal : 'a eq -> 'a t eq
val to_list : 'a t -> 'a list