package bin_there

  1. Overview
  2. Docs

Module Bin_there.SerializationSource

Serialization state and serializer combinators.

This module provides the concrete serialization context and first-class serializers. Unlike previous versions, this is not a functor - it uses existential types to wrap output sinks.

A t is the mutable context that accumulates bytes into an output sink and maintains the per-type sharing tables used to detect aliased or cyclic values. It is created once per top-level serialization and threaded through every serializer.

A 'a serializer is an opaque value that knows how to write one 'a into the sink, including the sharing-id prefix required by the wire format.

Sourcetype output = {
  1. add_uint8 : int -> unit;
  2. add_bytes : bytes -> unit;
}

Existential wrapper for output sinks. Use output_of_buffer or output_of_out_channel to create one, or build your own by constructing the record directly.

Sourceval output_of_buffer : Buffer.t -> output

Create an output wrapper from a Buffer.t.

Sourceval output_of_out_channel : out_channel -> output

Create an output wrapper from an out_channel.

Sourceval output_of : add_uint8:(int -> unit) -> add_bytes:(bytes -> unit) -> output

Create an output wrapper from individual functions.

Sourcetype t

The mutable serialization context. One value per top-level serialization.

Sourceval create : ?deduplicate_strings:bool -> output:output -> unit -> t

create ?deduplicate_strings ~output () creates a fresh serialization context targeting output.

  • parameter deduplicate_strings

    When true (default), physically-equal strings are written only once across the entire serialization.

Sourcetype 'a serializer

An opaque serializer for values of type 'a.

Sourcetype 'a content_writer = t -> 'a -> unit

The type of the function that writes a single value's content. It must not emit a sharing prefix - that is handled by write_value.

Sourceval make_serializer : serialization:t -> ?sharing_detector:'a Bin_there__.Sharing_encoder.state -> shared:bool -> type_id:Type_id.t -> content_writer:'a content_writer -> unit -> 'a serializer

make_serializer ~serialization ?sharing_detector ~shared ~type_id ~content_writer () constructs a serializer.

  • parameter sharing_detector

    Explicit sharing state to use instead of the per-type_id one stored in serialization.

  • parameter shared

    When true the serializer will track aliasing.

  • parameter type_id

    Identifies the type, used to key the per-type sharing table.

  • parameter content_writer

    The function that writes the actual payload bytes.

Sourceval string_deduplicator : t -> (string, string) Hashtbl.t option

Returns the optional string-deduplication state, or None if string deduplication was not requested.

Sourceval write_value : serialization:t -> serializer:'a serializer -> v:'a -> unit

write_value ~serialization ~serializer ~v writes v using serializer, emitting the sharing prefix (if any) before delegating to the content_writer.

Sourceval write_raw_std_header : serialization:t -> unit

write_raw_std_header ~serialization writes the standard file header.

Sourceval write_raw_varint : serialization:t -> int -> unit

write_raw_varint ~serialization n appends n as an unsigned LEB128.

Sourceval write_raw_bytes : serialization:t -> bytes -> unit

write_raw_bytes ~serialization b appends b verbatim.

Sourceval write_raw_int32_be : serialization:t -> int32 -> unit

write_raw_int32_be ~serialization n writes n as big-endian 32-bit.

Sourceval write_raw_uint8 : serialization:t -> int -> unit

write_raw_uint8 ~serialization b appends one byte.

Sourceval write_raw_bigarray1 : serialization:t -> ('a, 'b, 'c) Bigarray.Array1.t -> unit

write_raw_bigarray1 ~serialization b writes a bigarray.

Sourceval through : serialization:t -> post:(serialization:t -> 'b serializer) -> converter:('a -> 'b) -> 'a serializer

through ~serialization ~post ~converter builds a serializer for 'a by first mapping through converter then delegating to post.

Sourceval serialize_mutable_collection : serialization:t -> element_serializer:'elem serializer -> length:('coll -> int) -> iter:(('elem -> unit) -> 'coll -> unit) -> v:'coll -> unit

serialize_mutable_collection ... serializes a mutable collection.

Sourceval serialize_persistent_collection : serialization:t -> element_serializer:'elem serializer -> length:('coll -> int) -> iter:(('elem -> unit) -> 'coll -> unit) -> v:'coll -> unit

serialize_persistent_collection ... serializes a persistent collection.