From 2a401231f28f5bc9ba4d0b085a1e54e73851078f Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 13 Oct 2023 12:29:08 -0400 Subject: [PATCH] add `Deser.return` --- src/core/cbor_pack.ml | 18 ++++++++---------- src/core/cbor_pack.mli | 2 ++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/core/cbor_pack.ml b/src/core/cbor_pack.ml index c7d6fa3..6cb36c8 100644 --- a/src/core/cbor_pack.ml +++ b/src/core/cbor_pack.ml @@ -117,13 +117,11 @@ module Ser = struct let[@inline] bytes x : cbor = `Bytes (Bytes.unsafe_to_string x) let list_of f st x = list (List.map (f st) x) let map_of fk fv st x = map (List.map (fun (k, v) -> fk st k, fv st v) x) + let[@inline] delay f st x = f () st x - let[@inline] delay f st x = f() st x - - let fix (f: 'a t -> 'a t) : 'a t = - let rec _self = lazy ( - fun st x -> f (Lazy.force _self) st x - ) in Lazy.force _self + let fix (f : 'a t -> 'a t) : 'a t = + let rec _self = lazy (fun st x -> f (Lazy.force _self) st x) in + Lazy.force _self let add_entry ?(hashcons = false) (self : state) (c : cbor) : ptr = match c with @@ -220,6 +218,7 @@ module Deser = struct exception Error = CBOR.Error + let return x _st _c = x let fail s = raise (Error s) let failf s = Printf.ksprintf fail s @@ -307,10 +306,9 @@ module Deser = struct let[@inline] delay f st x = (f ()) st x - let fix (f: 'a t -> 'a t) : 'a t = - let rec _self = lazy ( - fun st x -> f (Lazy.force _self) st x - ) in Lazy.force _self + let fix (f : 'a t -> 'a t) : 'a t = + let rec _self = lazy (fun st x -> f (Lazy.force _self) st x) in + Lazy.force _self let map_entry_no_deref_ ~k (c : cbor) : cbor = let m = to_map_no_deref_ c in diff --git a/src/core/cbor_pack.mli b/src/core/cbor_pack.mli index 4efa3b2..20a517d 100644 --- a/src/core/cbor_pack.mli +++ b/src/core/cbor_pack.mli @@ -145,6 +145,8 @@ module Deser : sig (** Get an item via its pointer. @raise Invalid_argument if the pointer is invalid. *) + val return : 'a -> 'a t + val fail : string -> 'a (** Fail to decode. *)