package ppx_bin_prot

  1. Overview
  2. Docs
Generation of bin_prot readers and writers from types


Dune Dependency






Part of the Jane Street's PPX rewriters collection.

Published: 04 Jun 2019



Generation of binary serialization and deserialization functions from type definitions. There's more information about:

A note about signatures

In signatures, ppx_bin_prot tries to generate an include of a named interface, instead of a list of value bindings. That is:

type 'a t [@@deriving bin_io]

will generate:

include Binable.S1 with type 'a t := 'a t

instead of:

val bin_t : 'a Bin_prot.Type_class.t ‑> 'a t Bin_prot.Type_class.t
val bin_read_t : 'a Bin_prot.Read.reader ‑> 'a t Bin_prot.Read.reader
val __bin_read_t__ : 'a Bin_prot.Read.reader ‑> (int ‑> 'a t) Bin_prot.Read.reader
val bin_reader_t : 'a Bin_prot.Type_class.reader ‑> 'a t Bin_prot.Type_class.reader
val bin_size_t : 'a Bin_prot.Size.sizer ‑> 'a t Bin_prot.Size.sizer
val bin_write_t : 'a Bin_prot.Write.writer ‑> 'a t Bin_prot.Write.writer
val bin_writer_t : 'a Bin_prot.Type_class.writer ‑> 'a t Bin_prot.Type_class.writer
val bin_shape_t : Bin_prot.Shape.t ‑> Bin_prot.Shape.t

There are however a number of limitations:

  • the type has to be named t

  • the type can only have up to 3 parameters

  • there shouldn't be any constraint on the type parameters

If these aren't met, then ppx_bin_prot will simply generate a list of value bindings.

Weird looking type errors

In some cases, a type can meet all the conditions listed above, in which case the rewriting will apply, but lead to a type error. This happens when the type t is an alias to a type which does have constraints on the parameters, for instance:

type 'a s constraint 'a = [> `read ]
val bin_s : ([> `read ] as 'a) Type_class.t0 -> 'a s Type_class.t0
val bin_read_s : ([> `read ] as 'a) Read.reader -> 'a s Read.reader
val bin_reader_s : ([> `read ] as 'a) Type_class.reader0 -> 'a s Type_class.reader0
val bin_size_s : ([> `read ] as 'a) Size.sizer -> 'a s Size.sizer
val bin_write_s : ([> `read ] as 'a) Write.writer -> 'a s Write.writer
val bin_writer_s : ([> `read ] as 'a) Type_class.writer0 -> 'a s Type_class.writer0
val bin_shape_s : Shape.t -> Shape.t

type 'a t = 'a s [@@deriving_inline bin_io]
include Binable.S1 with type 'a t := 'a t

will give an error looking like:

Error: In this `with' constraint, the new definition of t
       does not match its original definition in the constrained signature:
       Type declarations do not match:
         type 'a t = 'a t constraint 'a = [> `read ]
       is not included in
         type 'a t
       File "", line 34, characters 2-11: Expected declaration
       Their constraints differ.

To workaround that error, simply copy the constraint on the type which has the [@@deriving] annotation. This will force generating a list of value bindings.

Dependencies (6)

  1. ppxlib >= "0.7.0" & < "0.9.0"
  2. dune >= "1.5.1"
  3. ppx_here >= "v0.12" & < "v0.13"
  4. bin_prot >= "v0.12" & < "v0.13"
  5. base >= "v0.12" & < "v0.13"
  6. ocaml >= "4.04.2"

Dev Dependencies


Used by (4)

  1. key-parsers >= "0.8.1" & < "1.0.0"
  2. oci
  3. ppx_bap < "v0.14.0"
  4. ppx_jane = "v0.12.0"




Innovation. Community. Security.