package bin_prot

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

'a Blob.t is type-equivalent to 'a, but has different bin-prot serializers that prefix the representation with the size of 'a.

To understand where this is useful, imagine we have an event type where many applications look at some parts of an event, but not all applications need to deal with all parts of an event. We might define:

type 'a event =
  { time : Time.t
  ; source : string
  ; details : 'a
  } [@@deriving bin_io]

Applications that need to understand all the details of an event could use:

type concrete_event = Details.t Blob.t event [@@deriving bin_io] 

An application that filters events to downsteam consumers based on just source or time (but doesn't need to parse details) may use:

type opaque_event = Blob.Opaque.Bigstring.t event [@@deriving bin_io] 

This has two advantages:

  • (de)serializing messages is faster because potentially costly (de)serialization of details is avoided
  • the application can be compiled without any knowledge of any conrete Details.t type, so it's robust to changes in Details.t

An application that's happy to throw away details may use:

type ignored_event = Blob.Ignored.t event [@@deriving bin_read] 

Whereas opaque_events roundtrip, ignored_events actually drop the bytes representing details when deserializing, and therefore do not roundtrip.

type 'a id = 'a
type 'a t = 'a
include Ppx_compare_lib.Comparable.S1 with type 'a t := 'a t
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
val sexp_of_t : ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.Sexp.t
include Binable.S1 with type 'a t := 'a id
val bin_shape_t : Shape.t -> Shape.t
val bin_size_t : ('a, 'a id) Size.sizer1
val bin_write_t : ('a, 'a id) Write.writer1
val bin_read_t : ('a, 'a id) Read.reader1
val __bin_read_t__ : ('a, int -> 'a id) Read.reader1
val bin_writer_t : ('a, 'a id) Type_class.S1.writer
val bin_reader_t : ('a, 'a id) Type_class.S1.reader
val bin_t : ('a, 'a id) Type_class.S1.t
module Opaque : sig ... end

An Opaque.Bigstring.t or Opaque.String.t is an arbitrary piece of bin-prot. The bin-prot (de-)serializers simply read/write the data, prefixed with its size.

module Ignored : sig ... end

An Ignored.t is an unusable value with special bin-prot converters. The reader reads the size and drops that much data from the buffer. Writing is not supported, however the size of t is kept, so bin_size_t is available.

OCaml

Innovation. Community. Security.