package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/batteries.unthreaded/BatMarshal/index.html
Module BatMarshal
Source
Marshaling of data structures.
This module provides functions to encode arbitrary data structures as sequences of bytes, which can then be written on a file or sent over a pipe or network connection. The bytes can then be read back later, possibly in another process, and decoded back into a data structure. The format for the byte sequences is compatible across all machines for a given version of OCaml.
Warning: marshaling is currently not type-safe. The type of marshaled data is not transmitted along the value of the data, making it impossible to check that the data read back possesses the type expected by the context. In particular, the result type of the Marshal.from_*
functions is given as 'a
, but this is misleading: the returned OCaml value does not possess type 'a
for all 'a
; it has one, unique type which cannot be determined at compile-type. The programmer should explicitly give the expected type of the returned value, using the following syntax:
(Marshal.from_channel chan : type)
. Anything can happen at run-time if the object in the file does not belong to the given type.
The representation of marshaled values is not human-readable, and uses bytes that are not printable characters. Therefore, input and output channels used in conjunction with Marshal.output
and Marshal.input
must be opened in binary mode, using e.g. BatPervasives.open_out_bin
or BatPervasives.open_in_bin
; channels opened in text mode will cause unmarshaling errors on platforms where text channels behave differently than binary channels, e.g. Windows.
The flags to the Marshal.to_*
functions below.
output out v
writes the representation of v
on chan
.
Marshal.to_bytes v flags
returns a byte sequence containing the representation of v
. The flags
argument has the same meaning as for Marshal.output
.
Same as to_bytes
but return the result as a string instead of a byte sequence.
Marshal.to_buffer buff ofs len v flags
marshals the value v
, storing its byte representation in the sequence buff
, starting at index ofs
, and writing at most len
bytes. It returns the number of bytes actually written to the sequence. If the byte representation of v
does not fit in len
characters, the exception Failure
is raised.
input inp
reads from inp
the byte representation of a structured value, as produced by one of the Marshal.to_*
functions, and reconstructs and returns the corresponding value.
Marshal.from_bytes buff ofs
unmarshals a structured value like Marshal.from_channel
does, except that the byte representation is not read from a channel, but taken from the byte sequence buff
, starting at position ofs
. The byte sequence is not mutated.
Same as from_bytes
but take a string as argument instead of a byte sequence.
The bytes representing a marshaled value are composed of a fixed-size header and a variable-sized data part, whose size can be determined from the header. Marshal.header_size
is the size, in bytes, of the header. Marshal.data_size
buff ofs
is the size, in bytes, of the data part, assuming a valid header is stored in buff
starting at position ofs
. Finally, Marshal.total_size
buff ofs
is the total size, in bytes, of the marshaled value. Both Marshal.data_size
and Marshal.total_size
raise Failure
if buff
, ofs
does not contain a valid header.
To read the byte representation of a marshaled value into a byte sequence, the program needs to read first Marshal.header_size
bytes into the sequence, then determine the length of the remainder of the representation using Marshal.data_size
, make sure the sequence is large enough to hold the remaining data, then read it, and finally call Marshal.from_bytes
to unmarshal the value.
See Marshal.header_size
.
See Marshal.header_size
.