mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 19:25:28 -05:00
feat(Ref): add protect function
This commit is contained in:
parent
d535cfe677
commit
43f82d7668
2 changed files with 16 additions and 0 deletions
|
|
@ -32,6 +32,17 @@ let swap a b =
|
||||||
a := !b;
|
a := !b;
|
||||||
b := x
|
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_list r = [ !r ]
|
||||||
let to_iter r yield = yield !r
|
let to_iter r yield = yield !r
|
||||||
let pp pp_x out r = pp_x out !r
|
let pp pp_x out r = pp_x out !r
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,11 @@ val swap : 'a t -> 'a t -> unit
|
||||||
(** [swap t1 t2] puts [!t2] in [t1] and [!t1] in [t2].
|
(** [swap t1 t2] puts [!t2] in [t1] and [!t1] in [t2].
|
||||||
@since 1.4 *)
|
@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 compare : 'a ord -> 'a t ord
|
||||||
val equal : 'a eq -> 'a t eq
|
val equal : 'a eq -> 'a t eq
|
||||||
val to_list : 'a t -> 'a list
|
val to_list : 'a t -> 'a list
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue