package catapult

  1. Overview
  2. Docs

Encoder type and some encoding utils.

The type Encode.t is passed to type-specific encoders so they can serialize values into bytes. This module also provides a host of functions that encode specific types (mostly integers and strings) into the Encode.t.

type t

Encoding state.

val of_output : output -> t

Encoding state that writes into the given output.

  • since 0.2
val of_buffer : Buffer.t -> t

Encoding state that emits bytes into the given buffer. After using a type encoder on this encoder, the buffer will contain the bytes.

type 'a enc = t -> 'a -> unit

A type encoder for values of type 'a.

val uint : t -> int64 -> unit

Encode unsigned integers using the varint encoding

val int : t -> int64 -> unit

Encode signed integers using the varint encoding

val u8 : t -> char -> unit

Encode a single byte

val u16 : t -> int -> unit

Encode a two byte unsigned integers in little endian

val u32 : t -> int32 -> unit

Encode an unsigned int32 integer in little endian

val u64 : t -> int64 -> unit

Encode an unsigned int64 integer in little endian

val i8 : t -> char -> unit

Same as u8

val i16 : t -> int -> unit

Encode a two byte unsigned integers in little endian

val i32 : t -> int32 -> unit

Encode a int32 integer in little endian

val i64 : t -> int64 -> unit

Encode a int64 integer in little endian

val bool : t -> bool -> unit

Encode a boolean as a single byte 0 or 1.

val f64 : t -> float -> unit

Encode a float.

val string : t -> string -> unit

Encode a string, prefixed by its length

val data : t -> bytes -> unit

Encode a blob, prefixed by its length

val data_of : size:int -> t -> bytes -> unit

Encode a blob of fixed size.

  • raises Failure

    if the blob's length in bytes is not equal to size

val optional : 'a enc -> 'a option enc

Encode an optional value, as either 0 for None or 1 followed by the encoding of x for Some x.

OCaml

Innovation. Community. Security.