Module Data_encoding.Binary

val length : 'a encoding -> 'a -> int

Compute the expected length of the binary representation of a value.

  • raises Write_error

    in case some size/length invariants are broken.

val fixed_length : 'a encoding -> int option

Returns the size of the binary representation that the given encoding might produce, only when this size does not depends of the value itself.

E.g., fixed_length (tup2 int64 (Fixed.string 2)) is Some _

E.g., fixed_length (result int64 (Fixed.string 2)) is None

E.g., fixed_length (list (tup2 int64 (Fixed.string 2))) is None

val maximum_length : 'a encoding -> int option

Returns the maximum size of the binary representation that the given encoding might produce, only when this maximum size does not depends of the value itself.

E.g., maximum_length (tup2 int64 (Fixed.string 2)) is Some _

E.g., maximum_length (result int64 (Fixed.string 2)) is Some _

E.g., maximum_length (list (tup2 int64 (Fixed.string 2))) is None

Note that the function assumes that recursive encodings (build using mu) are used for recursive data types. As a result, maximum_length will return None if given a recursive encoding.

If there are static guarantees about the maximum size of the representation for values of a given type, you can wrap your encoding in check_size. This will cause maximum_length to return Some _.

val of_bytes_opt : 'a encoding -> bytes -> 'a option
val of_string_opt : 'a encoding -> string -> 'a option
val to_bytes_opt : ?buffer_size:int -> 'a encoding -> 'a -> bytes option
val to_bytes_exn : ?buffer_size:int -> 'a encoding -> 'a -> bytes

to_bytes_exn enc v is equivalent to to_bytes enc v, except

  • raises [Write_error]

    instead of returning None in case of error.

val to_string_opt : ?buffer_size:int -> 'a encoding -> 'a -> string option
val to_string_exn : ?buffer_size:int -> 'a encoding -> 'a -> string
  • raises [Write_error]

    instead of returning None in case of error.