@@ -180,10 +180,12 @@ module Helper = struct
180180
181181 (* * Map fields names of a [Record_out] structure *)
182182 let rec map_record_out : type t a. (string -> string) -> (t, a, t) Record_out.t -> (t, a, t) Record_out.t =
183- fun field -> function
184- | Record_out. Cons ((field_name , to_t , default ), xs ) ->
185- Record_out. Cons ((field field_name, to_t, default), map_record_out field xs)
186- | Record_out. Nil -> Record_out. Nil
183+ fun field ->
184+ let open Record_out in
185+ function
186+ | Cons ((field_name , to_t , default ), xs ) ->
187+ Cons ((field field_name, to_t, default), map_record_out field xs)
188+ | Nil -> Nil
187189
188190 type 't serialize_record = (string * 't ) list -> 't
189191
@@ -193,27 +195,20 @@ module Helper = struct
193195 *)
194196 let of_record: type t a t. omit_default:bool -> t serialize_record -> (t, a, t) Record_out. t -> a =
195197 fun ~omit_default serialize_record ->
196- let value =
197- match omit_default with
198- | false -> fun n f _d -> fun v acc -> (n, f v) :: acc
199- | true -> fun n f d -> begin match d with
200- | Some d -> begin
201- fun v acc -> match Poly. equal v d with
202- | true -> acc
203- | false -> (n, f v) :: acc
204- end
205- | None -> fun v acc -> (n, f v) :: acc
206- end
207- in
208198 let rec inner : type a. (t, a, t) Record_out.t -> (string * t) list -> a =
209199 let open Record_out in
210200 function
211- | Cons ((n1 , f1 , d1 ), xs ) ->
201+ | Cons ((n1 , f1 , Some d1 ), xs ) when omit_default ->
202+ begin
203+ let cont = inner xs in
204+ fun acc v1 -> match Poly. equal d1 v1 with
205+ | true -> cont acc
206+ | false -> cont ((n1, f1 v1) :: acc)
207+ end
208+ | Cons ((n1 , f1 , _ ), xs ) ->
212209 let cont = inner xs in
213- let vf1 = value n1 f1 d1 in
214210 fun acc v1 ->
215- cont (vf1 v1 acc)
216-
211+ cont ((n1, f1 v1) :: acc)
217212 | Record_out. Nil ->
218213 fun acc -> serialize_record acc
219214 in
@@ -223,56 +218,23 @@ module Helper = struct
223218 let rec to_tuple : type t a b. (t, a, b) Tuple_in.t -> a -> t list -> b =
224219 let open Tuple_in in
225220 function
226- | Cons (f1 , Cons (f2 , Cons (f3 , Cons (f4 , Cons (f5 , Nil))))) -> begin
227- fun constructor -> function
228- | [v1; v2; v3; v4; v5] -> constructor (f1 v1) (f2 v2) (f3 v3) (f4 v4) (f5 v5)
229- | _ :: _ :: _ :: _ :: _ :: _ :: _ -> raise_errorf " Too many elements when parsing tuple"
230- | _ -> raise_errorf " Too few elements when parsing tuple"
231- end
232- | Cons (f1 , Cons (f2 , Cons (f3 , Cons (f4 , Nil)))) -> begin
233- fun constructor -> function
234- | [v1; v2; v3; v4] -> constructor (f1 v1) (f2 v2) (f3 v3) (f4 v4)
235- | _ :: _ :: _ :: _ :: _ :: _ -> raise_errorf " Too many elements when parsing tuple"
236- | _ -> raise_errorf " Too few elements when parsing tuple"
237- end
238- | Cons (f1 , Cons (f2 , Cons (f3 , Nil))) -> begin
239- fun constructor -> function
240- | [v1; v2; v3] -> constructor (f1 v1) (f2 v2) (f3 v3)
241- | _ :: _ :: _ :: _ :: _ -> raise_errorf " Too many elements when parsing tuple"
242- | _ -> raise_errorf " Too few elements when parsing tuple"
243- end
244- | Cons (f1 , Cons (f2 , Nil)) -> begin
245- fun constructor -> function
246- | [v1; v2] -> constructor (f1 v1) (f2 v2)
247- | _ :: _ :: _ :: _ -> raise_errorf " Too many elements when parsing tuple"
248- | _ -> raise_errorf " Too few elements when parsing tuple"
249- end
250- | Cons (f1 , Nil) -> begin
251- fun constructor -> function
252- | [v1] -> constructor (f1 v1)
253- | _ :: _ :: _ -> raise_errorf " Too many elements when parsing tuple"
254- | _ -> raise_errorf " Too few elements when parsing tuple"
255- end
221+ | Cons (f1 , xs ) -> begin
222+ let cont = to_tuple xs in
223+ fun constructor -> function
224+ | v1 :: ts -> cont (constructor (f1 v1)) ts
225+ | _ -> raise_errorf " Too few elements when parsing tuple"
226+ end
256227 | Nil -> fun a -> begin
257228 function
258229 | [] -> a
259230 | _ -> raise_errorf " Too many elements when parsing tuple"
260231 end
261232
262- | Cons (f1 , Cons (f2 , Cons (f3 , Cons (f4 , Cons (f5 , xs ))))) -> begin
263- let cont = to_tuple xs in
264- fun constructor -> function
265- | v1 :: v2 :: v3 :: v4 :: v5 :: ts -> cont (constructor (f1 v1) (f2 v2) (f3 v3) (f4 v4) (f5 v5)) ts
266- | _ -> raise_errorf " Too few elements when parsing tuple"
267- end
268-
269233 type 't serialize_tuple = 't list -> 't
270234 let of_tuple: type t a. t serialize_tuple -> (t, a, t) Tuple_out. t -> a = fun serialize_tuple ->
271235 let rec inner : type a. (t, a, t) Tuple_out.t -> t list -> a =
272236 let open Tuple_out in
273237 function
274- | Cons (f1 , Cons (f2 , (Cons (f3 , (Cons (f4 , Cons (f5 , Nil))))))) ->
275- fun acc v1 v2 v3 v4 v5 -> List. rev_append acc [f1 v1; f2 v2; f3 v3; f4 v4; f5 v5] |> serialize_tuple
276238 | Cons (f1 , Cons (f2 , (Cons (f3 , (Cons (f4 , Nil)))))) ->
277239 fun acc v1 v2 v3 v4 -> List. rev_append acc [f1 v1; f2 v2; f3 v3; f4 v4] |> serialize_tuple
278240 | Cons (f1 , Cons (f2 , (Cons (f3 , Nil)))) ->
@@ -283,7 +245,6 @@ module Helper = struct
283245 fun acc v1 -> List. rev_append acc [f1 v1] |> serialize_tuple
284246 | Nil ->
285247 fun acc -> List. rev acc |> serialize_tuple
286-
287248 | Cons (f1 , Cons (f2 , (Cons (f3 , (Cons (f4 , Cons (f5 , xs ))))))) ->
288249 let cont = inner xs in
289250 fun acc v1 v2 v3 v4 v5 -> cont (f5 v5 :: f4 v4 :: f3 v3 :: f2 v2 :: f1 v1 :: acc)
0 commit comments