| 1 | (** ['a Blob.t] is type-equivalent to ['a], but has different bin-prot serializers that |
| 2 | prefix the representation with the size of ['a]. |
| 3 | |
| 4 | To understand where this is useful, imagine we have an event type where many |
| 5 | applications look at some parts of an event, but not all applications need to deal |
| 6 | with all parts of an event. We might define: |
| 7 | |
| 8 | {[ |
| 9 | type 'a event = |
| 10 | { time : Time.t |
| 11 | ; source : string |
| 12 | ; details : 'a |
| 13 | } [@@deriving bin_io] |
| 14 | ]} |
| 15 | |
| 16 | Applications that need to understand all the details of an event could use: |
| 17 | |
| 18 | {[ type concrete_event = Details.t Blob.t event [@@deriving bin_io] ]} |
| 19 | |
| 20 | An application that filters events to downsteam consumers based on just [source] or |
| 21 | [time] (but doesn't need to parse [details]) may use: |
| 22 | |
| 23 | {[ type opaque_event = Blob.Opaque.Bigstring.t event [@@deriving bin_io] ]} |
| 24 | |
| 25 | This has two advantages: |
| 26 | - (de)serializing messages is faster because potentially costly (de)serialization of |
| 27 | [details] is avoided |
| 28 | - the application can be compiled without any knowledge of any conrete [Details.t] |
| 29 | type, so it's robust to changes in [Details.t] |
| 30 | |
| 31 | An application that's happy to throw away [details] may use: |
| 32 | |
| 33 | {[ type ignored_event = Blob.Ignored.t event [@@deriving bin_read] ]} |
| 34 | |
| 35 | Whereas [opaque_event]s roundtrip, [ignored_event]s actually drop the bytes |
| 36 | representing [details] when deserializing, and therefore do not roundtrip. |
| 37 | *) |
| 38 | |
| 39 | type 'a id = 'a |
| 40 | |
| 41 | (*_ This type synonym is introduced because older versions of OCaml |
| 42 | do not support destructive substitutions with `type 'a t1 = t2`. *) |
| 43 | |
| 44 | type 'a t = 'a [@@deriving compare, sexp_of] |
| 45 | |
| 46 | include Binable.S1 with type 'a t := 'a id |
| 47 | |
| 48 | (** An [Opaque.Bigstring.t] or [Opaque.String.t] is an arbitrary piece of bin-prot. The |
| 49 | bin-prot (de-)serializers simply read/write the data, prefixed with its size. |
| 50 | |
| 51 | When reading bin-prot data, sometimes you won't care about deserializing a particular |
| 52 | piece: perhaps you want to operate on a bin-prot stream, transforming some bits of |
| 53 | the stream and passing the others through untouched. In these cases you can |
| 54 | deserialize using the bin-prot converters for a type involving [Opaque.t]. This is |
| 55 | analogous to reading a sexp file / operating on a sexp stream and using |
| 56 | (de-)serialization functions for a type involving [Sexp.t]. |
| 57 | |
| 58 | The internal representation of [Opaque.Bigstring.t] is a Bigstring, while |
| 59 | [Opaque.String.t] is a string. |
| 60 | *) |
| 61 | module Opaque : sig |
| 62 | module Bigstring : sig |
| 63 | type t [@@deriving compare, sexp_of] |
| 64 | |
| 65 | include Binable.S with type t := t |
| 66 | |
| 67 | val to_opaque : 'a -> 'a Type_class.writer -> t |
| 68 | val of_opaque_exn : t -> 'a Type_class.reader -> 'a |
| 69 | end |
| 70 | |
| 71 | module String : sig |
| 72 | type t [@@deriving compare, sexp_of] |
| 73 | |
| 74 | include Binable.S with type t := t |
| 75 | |
| 76 | (** For performance's concern, we require caller of [to_opaque] and [of_opaque_exn] to |
| 77 | pass in the [buf] as the intermediate buffer for bin_prot conversion. These two |
| 78 | functions will write bytes into the buffer, but will not resize the buffer. So the |
| 79 | caller should prepare big enough buffer for their need. |
| 80 | |
| 81 | For [of_opaque_exn t], the minimum buffer size should be [length t]. |
| 82 | |
| 83 | For [to_opaque] the necessary buffer size can be computed using [size] from |
| 84 | Type_class.writer or you can catch the exception [Bin_prot.Common.Buffer_short] |
| 85 | (although the latter is not very reliable because some custom bin_io |
| 86 | implementations raise a different exception). |
| 87 | |
| 88 | Additional caveat: if the opaque blob is malformed/partial then [of_opaque_exn] |
| 89 | can read past the end of the blob, which can result in: |
| 90 | - confusing/non-deterministic error messages (referring to the contents of [buf] |
| 91 | rather than the contents of the blob) |
| 92 | - degraded performance (having to read through the buffer just to fail at the end) |
| 93 | *) |
| 94 | val length : t -> int |
| 95 | |
| 96 | val to_opaque : buf:Common.buf -> 'a -> 'a Type_class.writer -> t |
| 97 | val of_opaque_exn : buf:Common.buf -> t -> 'a Type_class.reader -> 'a |
| 98 | end |
| 99 | end |
| 100 | |
| 101 | (** An [Ignored.t] is an unusable value with special bin-prot converters. The reader reads |
| 102 | the size and drops that much data from the buffer. Writing is not supported, however |
| 103 | the size of [t] is kept, so [bin_size_t] is available. |
| 104 | |
| 105 | This can be used in similar situations to [Opaque.t]. If instead of transforming a |
| 106 | bin-prot stream, you are simply consuming it (and not passing it on anywhere), there |
| 107 | is no need to remember the bin-prot representation for the bits you're ignoring. E.g. |
| 108 | if you wish to extract a subset of information from a bin-prot file, which contains |
| 109 | the serialized representation of some type T (or a bunch of Ts in a row, or something |
| 110 | similar), you can define a type which is similar to T but has various components |
| 111 | replaced with [Ignored.t]. |
| 112 | *) |
| 113 | module Ignored : sig |
| 114 | type t |
| 115 | |
| 116 | val bin_size_t : t Size.sizer |
| 117 | val bin_read_t : t Read.reader |
| 118 | val __bin_read_t__ : (int -> t) Read.reader |
| 119 | val bin_reader_t : t Type_class.reader |
| 120 | end |