package data-encoding
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=7b687e25619637d40d2bbcd2c21b00c2
sha512=65e33b1a56e9058a2e9c7f3dc18cb72c21270e0e5b9584fe856285d16e4cb8e98adac826373d4109a5e080486ec31cdd9870b402249ac5d55c7b0de6ffb68b0a
doc/data-encoding/Data_encoding/Binary/index.html
Module Data_encoding.BinarySource
type read_error = | Not_enough_data(*Decoding requires more bytes than are available in the source of the data. E.g., there are fewer bytes available in the reading buffer than is required by the length field of a dynamically-sized string.
*)| Extra_bytes(*Decoding requires fewer bytes than were provided.
*)| No_case_matched| Unexpected_tag of int| Invalid_int of {}(*An integer is out of range. E.g., an integer is beyond a user-provided range.
*)| Invalid_float of {}(*A float is out of range.
*)| Trailing_zero(*An arbitrary-precision number (N or Z) leads to a null byte.
*)| Size_limit_exceeded(*A value is encoded with more bytes than is allowed by the encoding. E.g., the constraints of a
*)check_sizeare being broken.| List_too_long(*A list contains more elements than is specified in its
*)max_lengthparameter.| Array_too_long(*An array contains more elements than is specified in its
*)max_lengthparameter.| Exception_raised_in_user_function of string| User_invariant_guard of string
All the errors that might be returned while reading a binary value
Read_error e is an exception wrapping the read_error e. It is used only by function suffixed by _exn where the suffix-less function would have returned Error e.
type write_error = All the errors that might be returned while writing a binary value
Compute the expected length of the binary representation of a value
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
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 _.
read enc buf ofs len tries to reconstruct a value from the bytes in buf starting at offset ofs and reading at most len bytes. This function also returns the offset of the first unread bytes in the buf.
The function will fail (returning Error _) if
- the bytes in
bufdesignated byofsandlenare not compatible with the encodingenc(e.g., an integer is out the range specified in the encoding, or a union's tag is not recognised), - it needs to read more than
lenbytes to decode the value, - a user-provided function (such as that of a
convordelayed) raises an exception, - etc. In which case the returned error contains minimal diagnosis information about the discrepancy between the bytes and the encoding.
Other reading functions (of_string, of_bytes) may fail for the same reasons.
The returned value contains no pointer back to buf (as a whole or as sub-strings), even in the case where the encoding is or includes Fixed.string or Fixed.bytes.
read_opt is like read but in case of failure, the error is ignored and None is returned instead.
read_exn is like read but in case of failure, the error is wrapped in Read_error which is raised.
type 'ret status = | Success of {}(*Fully decoded value, together with the total amount of bytes reads, and the remaining unread stream.
*)| Await of Bytes.t -> 'ret status(*Partially decoded value.
*)| Error of read_error(*Failure. The stream is garbled and it should be dropped.
*)
Return type for the function read_stream.
Streamed equivalent of read. This variant cannot be called on variable-size encodings.
The internal state that writers handle. It is presented explicitly as an abstract type so that you must use the constructor to obtain it. The constructor (make_writer_state) performs basic bound checks.
make_writer_state buffer ~offset ~allowed_bytes is None if allowed_bytes < 0, None if allowed_bytes > length buffer - offset, Some _ otherwise.
write enc v state where Some state is make_writer_state buffer ~offset ~allowed_bytes writes the binary enc representation of v onto buffer starting at offset. The function will fail (returning Error _) if the encoding would use more than allowed_bytes.
The function returns the offset of first unwritten bytes, or returns Error _ in case of failure. In the latter case, some data might have been written on the buffer.
of_bytes enc buf is equivalent to read enc buf 0 (length buf). The function fails if the buffer is not fully read.
of_bytes_exn enc buf is equivalent to of_bytes, except
of_string enc buf is like of_bytes enc buf but it reads bytes from a string.
to_bytes enc v is the equivalent of write env buf 0 len where buf is a newly allocated buffer. The parameter buffer_size controls the initial size of buf.
to_bytes_exn enc v is equivalent to to_bytes enc v, except
to_string enc v is like to_bytes but it returns a string.
Note: to_string enc v is always equal to Bytes.to_string (to_bytes enc v) but more efficient.