mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
update of Bij.version
This commit is contained in:
parent
a893e6c0eb
commit
5c11f945b9
2 changed files with 18 additions and 15 deletions
26
bij.ml
26
bij.ml
|
|
@ -67,17 +67,6 @@ let guard f t = Guard (f, t)
|
|||
let map ~inject ~extract b = Map (inject, extract, b)
|
||||
let switch ~inject ~extract = Switch (inject, extract)
|
||||
|
||||
(** {2 Helpers} *)
|
||||
|
||||
let fix f =
|
||||
let rec bij = lazy (f (fun () -> Lazy.force bij)) in
|
||||
Lazy.force bij
|
||||
|
||||
type 'a versioned = string * 'a
|
||||
|
||||
let with_version v t =
|
||||
pair (guard (fun v' -> v = v') string_) t
|
||||
|
||||
(** {2 Exceptions} *)
|
||||
|
||||
exception EOF
|
||||
|
|
@ -88,6 +77,21 @@ exception EncodingError of string
|
|||
exception DecodingError of string
|
||||
(** Raised when decoding is impossible *)
|
||||
|
||||
(** {2 Helpers} *)
|
||||
|
||||
let fix f =
|
||||
let rec bij = lazy (f (fun () -> Lazy.force bij)) in
|
||||
Lazy.force bij
|
||||
|
||||
let with_version v t =
|
||||
map
|
||||
~inject:(fun x -> v, x)
|
||||
~extract:(fun (v', x) ->
|
||||
if v = v'
|
||||
then x
|
||||
else raise (DecodingError ("expected version " ^ v)))
|
||||
(pair string_ t)
|
||||
|
||||
(** {2 Source of parsing} *)
|
||||
|
||||
module type SOURCE = sig
|
||||
|
|
|
|||
7
bij.mli
7
bij.mli
|
|
@ -64,10 +64,9 @@ val switch : inject:('a -> char * 'a inject_branch) ->
|
|||
val fix : ((unit -> 'a t) -> 'a t) -> 'a t
|
||||
(** Helper for recursive encodings *)
|
||||
|
||||
type 'a versioned = string * 'a
|
||||
|
||||
val with_version : string -> 'a t -> 'a versioned t
|
||||
(** Guards the values with a given version *)
|
||||
val with_version : string -> 'a t -> 'a t
|
||||
(** Guards the values with a given version. Only values encoded with
|
||||
the same version will fit. *)
|
||||
|
||||
(** {2 Exceptions} *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue