The first type parameter controls whether the iobuf can be written to. The second type parameter controls whether the window and limits can be changed.
See the Perms
module for information on how the first type parameter is used.
To allow no_seek
or seek
access, a function's type uses _
rather than no_seek
as the type argument to t
. Using _
allows the function to be directly applied to
either permission. Using a specific permission would require code to use coercion
:>
.
create ~len
creates a new iobuf, backed by a bigstring of length len
,
with the limits and window set to the entire bigstring.
set_bounds_and_buffer ~src ~dst
copies bounds metadata (i.e., limits and window) and
shallowly copies the buffer (data pointer) from src
to dst
. It does not access
data, but does allow access through dst
. This makes dst
an alias of src
.
Because set_bounds_and_buffer
creates an alias, we disallow immutable src
and
dst
using [> write]
. Otherwise, one of src
or dst
could be read_write :>
read
and the other immutable :> read
, which would allow to write the immutable
alias's data through the read_write
alias.
set_bounds_and_buffer
is typically used to allocate a frame iobuf only once. This
frame can be updated repeatedly and handed to users, without further allocation. Only
the most allocation-sensitive applications need this.
set_bounds_and_buffer_sub ?pos ?len ~src ~dst ()
is a more efficient version of:
set_bounds_and_buffer ~src:(Iobuf.sub ?pos ?len src) ~dst
.
set_bounds_and_buffer ~src ~dst
is not the same as set_bounds_and_buffer_sub ~dst
~src ()
because the limits are narrowed in the latter case.
One may wonder why you'd want to call no_seek
, given that a cast is already
possible, e.g. t : (_, seek) t :> (_, no_seek) t
. It turns out that if you want to
define some f : (_, _) t -> unit
of your own, which can be conveniently applied to
seek
iobufs without the user having to cast seek
up, you need this no_seek
function.
read_only
is more of an historical convenience now that read_write
is a
polymorphic variant, as one can now explicitly specify the general type for an
argument with something like t : (_ perms, _) t :> (read, _) t
.
capacity t
returns the size of t
's limits subrange. The capacity of an iobuf can
be reduced via narrow
.
flip_lo t
sets the window to range from the lower limit to the lower bound of the
old window. This is typically called after a series of Fill
s, to reposition the
window in preparation to Consume
the newly written data.
The bounded version narrows the effective limit. This can preserve some data near the
limit, such as an hypothetical packet header, in the case of bounded_flip_lo
or
unfilled suffix of a buffer, in bounded_flip_hi
.
compact t
copies data from the window to the lower limit of the iobuf and sets the
window to range from the end of the copied data to the upper limit. This is typically
called after a series of Consume
s to save unread data and prepare for the next
series of Fill
s and flip_lo
.
flip_hi t
sets the window to range from the the upper bound of the current window to
the upper limit. This operation is dual to flip_lo
and is typically called when the
data in the current (narrowed) window has been processed and the window needs to be
positioned over the remaining data in the buffer. For example:
(* ... determine initial_data_len ... *)
Iobuf.resize buf ~len:initial_data_len;
(* ... and process initial data ... *)
Iobuf.flip_hi buf;
Now the window of buf
ranges over the remainder of the data.
Peek
and Poke
functions access a value at pos
from the lower bound of the window
and do not advance.
Peek.bin_prot X.bin_read_t t
returns the initial X.t
in t
without advancing.
Following the bin_prot
protocol, the representation of x
is X.bin_size_t x
bytes
long. Peek.
, Poke.
, Consume.
, and Fill.bin_prot
do not add any size prefix or
other framing to the bin_prot
representation.
fill_bin_prot
writes a bin-prot value to the lower bound of the window, prefixed by
its length, and advances by the amount written. fill_bin_prot
returns an error if
the window is too small to write the value.
consume_bin_prot t reader
reads a bin-prot value from the lower bound of the window,
which should have been written using fill_bin_prot
, and advances the window by the
amount read. consume_bin_prot
returns an error if there is not a complete message
in the window and in that case the window is left unchanged.
Don't use these without a good reason, as they are incompatible with similar functions
in Reader
and Writer
. They use a 4-byte length rather than an 8-byte length.
Iobuf
has analogs of various Bigstring
functions. These analogs advance by the
amount written/read.