Skip to content

Commit

Permalink
Tail Modulo Constructor is only available from ocaml 4.13. Convert to…
Browse files Browse the repository at this point in the history
… use seq under the assumption that List.of_seq is well optimized.
  • Loading branch information
andersfugmann committed Jan 7, 2024
1 parent aa14d10 commit e0fefbe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/ocaml_protoc_plugin/extensions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let set: ('a -> Writer.t, Writer.t) Serialize.S.compound_list -> t -> 'a -> t =
let writer = Serialize.serialize [] spec [] writer v in
let reader = Writer.contents writer |> Reader.create in
match Reader.to_list reader with
| (((index, _) :: _) as fields) ->
(List.filter ~f:(fun (i, _) -> i != index) t) @ fields
| ((index, _) :: _) as fields ->
(List.filter ~f:(fun (i, _) -> i != index) t) @ fields
| [] -> t
| exception Result.Error _ -> failwith "Internal serialization fail"
12 changes: 5 additions & 7 deletions src/ocaml_protoc_plugin/reader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,11 @@ let read_field : boxed -> t -> int * Field.t = fun boxed ->
let (field_type, field_number) = read_field_header t in
field_number, read_field_content field_type t


let to_list: t -> (int * Field.t) list =
let read_field = read_field Boxed in
fun t ->
(* Make this tailrec *)
let[@tail_mod_cons] rec inner () = match has_more t with
| true -> read_field t :: inner ()
| false -> []
let rec next t () = match has_more t with
| true -> Seq.Cons (read_field t, next t)
| false -> Seq.Nil
in
inner ()
fun t ->
next t |> List.of_seq

0 comments on commit e0fefbe

Please sign in to comment.