Streamed equivalent of read
.
read_stream e
is Await k
and you can use k
to feed the first bytes to be decoded.
If you feed invalid bytes (i.e., bytes that do not match e
) to k
, it returns Error
.
If you feed bytes that form a valid strict prefix for e
, then it returns Await k
, and you can use k
to feed it more bytes.
If you feed it sufficient bytes to decode a whole value described by e
, then it returns Success s
where s.result
is the decoded value, s.size
is the number of bytes that were read to decode this value, and s.stream
is the left-over stream.
The leftover stream may contain more bytes which you may treat however you like depending on your application. You may ignore padding bytes reserved for extensions (in which case you can simply ignore the stream). You may use the leftover stream to call read_stream
again to decode the rest of the value.
read_stream ~init e
is the same as let (Await k) = read_stream e in k b
where b
are the leftover bytes of init
.
E.g., reading multiple values from a socket or some such source of bytes that becomes available as time goes by.
let iter socket encoding f =
let rec loop = function
| Success {result; size; stream} ->
log "read %d bytes" size;
f result (* apply iterator function on each decoded value *);
loop (read_stream ~init:stream e) (* continue with leftover *)
| Await k ->
loop (k (read_more_bytes_from socket))
| Error e ->
log "error: %a" pp_read_error e
in
loop (read_stream e)