Skip to content

Custom drivers

Anders Peter Fugmann edited this page Apr 20, 2019 · 1 revision

All drivers must implement the Runtime.Driver interface.

It is recommended to make the type t inside the driver concrete rather than opaque by using the signature:

Protocol_conv.Runtime.Driver with type t := ...

A good starting point for writing you own driver, is to use the test driver used by the ppx itself: https://github.com/andersfugmann/ppx_protocol_conv/tree/master/ppx/test

The API for ppx_protocol_conv can be found at https://andersfugmann.github.io/ppx_protocol_conv/ppx_protocol_conv/index.html

Speed considerations

Records, Variants and Tuples takes multiple arguments including a spec. Throughout the library (ppx and the runtime), the code relies on partial evaluation. It is therefore recommended to reuse the result of partial evaluated functions, e.g.

let to_variant: (t, 'a) Variant_in.t list -> t -> 'a = fun spec -> function
  | Variant (name, args) -> Helper.to_variant spec name args
| t -> raise_errorf t "Variant expected"

is more efficiantly written as:

let to_variant: (t, 'a) Variant_in.t list -> t -> 'a = fun spec -> 
  let to_variant' = Helper.to_variant spec in 
  function
    | Variant (name, args) -> to_variant' spec name args  
    | t -> raise_errorf t "Variant expected"

As the first version needs to re-parse the given spec for each function invocation. The ppx assumes this behaviour, and stored the result of partially evaluated functions.

Clone this wiki locally