diff --git a/src/core/CCRef.ml b/src/core/CCRef.ml index 5b5ef201..adc336b6 100644 --- a/src/core/CCRef.ml +++ b/src/core/CCRef.ml @@ -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 diff --git a/src/core/CCRef.mli b/src/core/CCRef.mli index 8c6eb221..e48deb45 100644 --- a/src/core/CCRef.mli +++ b/src/core/CCRef.mli @@ -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