Module Core__.Iobuf
type nonrec seek= Core.Iobuf_intf.seek
val sexp_of_seek : seek -> Ppx_sexp_conv_lib.Sexp.t
type nonrec no_seek= Core.Iobuf_intf.no_seek
val sexp_of_no_seek : no_seek -> Ppx_sexp_conv_lib.Sexp.t
type t_reprThis type is a compiler witness that 'rw and 'seek do not affect layout; it enables wider use of unboxed GADTs.
type (-'data_perm_read_write, +'seek_permission) t= private t_reprThe 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
Permsmodule for information on how the first type parameter is used.To allow
no_seekorseekaccess, a function's type uses_rather thanno_seekas the type argument tot. Using_allows the function to be directly applied to either permission. Using a specific permission would require code to use coercion:>.There is no
t_of_sexp. One should useIobuf.Hexdump.t_of_sexpor@sexp.opaqueas desired.
include Core__.Import.Invariant.S2 with type ('rw, 'seek) t := ('rw, 'seek) t
val invariant : 'a Base__.Invariant_intf.inv -> 'b Base__.Invariant_intf.inv -> ('a, 'b) t Base__.Invariant_intf.inv
module Window : Core__.Import.Hexdump.S2 with type ('rw, 'seek) t := ('rw, 'seek) tProvides a
Window.Hexdumpsubmodule that renders the contents oft's window.
module Limits : Core__.Import.Hexdump.S2 with type ('rw, 'seek) t := ('rw, 'seek) tProvides a
Limits.Hexdumpsubmodule that renders the contents oft's limits.
Provides a Hexdump submodule that renders the contents of t's window and limits using indices relative to the limits.
include Core.Iobuf_intf.Compound_hexdump with type ('rw, 'seek) t := ('rw, 'seek) t
module Hexdump : sig ... endmodule Debug : Core.Iobuf_intf.Compound_hexdump with type ('rw, 'seek) t := ('rw, 'seek) tProvides a
Debug.Hexdumpsubmodule that renders the contents oft's window, limits, and underlying bigstring using indices relative to the bigstring.
Creation
val create : len:int -> (_, _) tcreate ~lencreates a new iobuf, backed by a bigstring of lengthlen, with the limits and window set to the entire bigstring.
val of_bigstring : ?pos:int -> ?len:int -> Core__.Import.Bigstring.t -> ([< Core__.Import.read_write ], _) tof_bigstring bigstring ~pos ~lenreturns an iobuf backed bybigstring, with the window and limits specified starting atposand of lengthlen.forbid
immutableto prevent aliasing
val of_string : string -> (_, _) tof_string sreturns a new iobuf whose contents ares.
sub_shared t ~pos ~lenreturns a new iobuf with limits and window set to the subrange oft's window specified byposandlen.sub_sharedpreserves data permissions, but allows arbitrary seek permissions on the resulting iobuf.
val set_bounds_and_buffer : src:([> Core__.Import.write ] as 'data, _) t -> dst:('data, seek) t -> unitset_bounds_and_buffer ~src ~dstcopies bounds metadata (i.e., limits and window) and shallowly copies the buffer (data pointer) fromsrctodst. It does not access data, but does allow access throughdst. This makesdstan alias ofsrc.Because
set_bounds_and_buffercreates an alias, we disallow immutablesrcanddstusing[> write]. Otherwise, one ofsrcordstcould beread_write :> readand the otherimmutable :> read, which would allow you to write theimmutablealias's data through theread_writealias.set_bounds_and_bufferis typically used with a frame iobuf that need only be allocated once. This frame can be updated repeatedly and handed to users, without further allocation. Allocation-sensitive applications need this.
val set_bounds_and_buffer_sub : pos:int -> len:int -> src:([> Core__.Import.write ] as 'data, _) t -> dst:('data, seek) t -> unitset_bounds_and_buffer_sub ~pos ~len ~src ~dstis a more efficient version ofset_bounds_and_buffer ~src:(Iobuf.sub_shared ~pos ~len src) ~dst.set_bounds_and_buffer ~src ~dstis not the same asset_bounds_and_buffer_sub ~dst ~src ~len:(Iobuf.length src)because the limits are narrowed in the latter case.~lenand~posare mandatory for performance reasons, in concert with@@inline. If they were optional, allocation would be necessary when passing a non-default, non-constant value, which is an important use case.
val read_only : ([> Core__.Import.read ], 's) t -> (Core__.Import.read, 's) tGeneralization
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 somef : (_, _) t -> unitof your own that can be conveniently applied toseekiobufs without the user having to castseekup, you need thisno_seekfunction.read_onlyis more of a historical convenience now thatread_writeis a polymorphic variant, as one can now explicitly specify the general type for an argument with something liket : (_ perms, _) t :> (read, _) t.
Accessors
val capacity : (_, _) t -> intcapacity treturns the size oft's limits subrange. The capacity of an iobuf can be reduced vianarrow.
val length : (_, _) t -> intlength treturns the size oft's window.
val length_lo : (_, _) t -> intlength_lo treturns the length thatt's window would have after callingflip_lo, without actually changing the window. This is the number of bytes between the lower limit and the start of the window.When you're writing to the window, you can think of this as the number of bytes already written. When reading from the window, this can mean the number of bytes already consumed.
This is equivalent to:
Iobuf.Expert.(lo t - lo_min t).
val length_hi : (_, _) t -> intlength_hi treturns the length thatt's window would have after callingflip_hi, without actually changing the window. This is the number of bytes between the end of the window and the upper limit of the buffer.This is equivalent to:
Iobuf.Expert.(hi_max t - hi t).
val is_empty : (_, _) t -> boolis_empty tislength t = 0.
Changing the limits
Comparison
Changing the window
module type Bound = Core.Iobuf_intf.Bound with type ('d, 'w) iobuf := ('d, 'w) tval advance : (_, seek) t -> int -> unitadvance t amountadvances the lower bound of the window byamount. It is an error to advance past the upper bound of the window or the lower limit.
val unsafe_advance : (_, seek) t -> int -> unitunsafe_advanceis likeadvancebut with no bounds checking, so incorrect usage can easily cause segfaults.
val resize : (_, seek) t -> len:int -> unitresize tsets the length oft's window, provided it does not exceed limits.
val unsafe_resize : (_, seek) t -> len:int -> unitunsafe_resizeis likeresizebut with no bounds checking, so incorrect usage can easily cause segfaults.
val flip_lo : (_, seek) t -> unitflip_lo tsets the window to range from the lower limit to the lower bound of the old window. This is typically called after a series ofFills, to reposition the window in preparation toConsumethe newly written data.The bounded version narrows the effective limit. This can preserve some data near the limit, such as a hypothetical packet header (in the case of
bounded_flip_lo) or unfilled suffix of a buffer (inbounded_flip_hi).
val bounded_flip_lo : (_, seek) t -> Lo_bound.t -> unitval compact : (Core__.Import.read_write, seek) t -> unitcompact tcopies 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 ofConsumes to save unread data and prepare for the next series ofFills andflip_lo.
val bounded_compact : (Core__.Import.read_write, seek) t -> Lo_bound.t -> Hi_bound.t -> unitval flip_hi : (_, seek) t -> unitflip_hi tsets the window to range from the the upper bound of the current window to the upper limit. This operation is dual toflip_loand 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
bufranges over the remainder of the data.
Getting and setting data
"consume" and "fill" functions access data at the lower bound of the window and advance the lower bound of the window. "peek" and "poke" functions access data but do not advance the window.
val to_string : ?len:int -> ([> Core__.Import.read ], _) t -> stringto_string treturns the bytes intas a string. It does not alter the window.
val to_string_hum : ?max_lines:int -> (_, _) t -> stringEquivalent to
Hexdump.to_string_hum. Renderst's windows and limits.
val to_bytes : ?len:int -> (_, _) t -> Core__.Import.Bytes.tto_bytes treturns the bytes intas a bytes. It does not alter the window.
val of_bytes : Core__.Import.Bytes.t -> (_, _) tof_bytes breturns a new iobuf whose contents isb.
module Consume : sig ... endConsume.string t ~lenreadslencharacters (all, by default) fromtinto a new string and advances the lower bound of the window accordingly.
module Fill : sig ... endFill.bin_prot X.bin_write_t t xwritesxtotin bin-prot form, advancing past the bytes written.
module Peek : sig ... endPeekandPokefunctions access a value atposfrom the lower bound of the window and do not advance.
module Poke : sig ... endPoke.bin_prot X.bin_write_t t xwritesxto the beginning oftin binary form without advancing. You can useX.bin_size_tto tell how long it was.X.bin_write_tis only allowed to write that portion of the buffer you have access to.
module Unsafe : sig ... endUnsafehas submodules that are like their corresponding module, except with no range checks. Hence, mistaken uses can cause segfaults. Be careful!
val bin_prot_length_prefix_bytes : intThe number of bytes in the length prefix of
consume_bin_protandfill_bin_prot.
val fill_bin_prot : ([> Core__.Import.write ], seek) t -> 'a Bin_prot.Type_class.writer -> 'a -> unit Core__.Import.Or_error.tfill_bin_protwrites a bin-prot value to the lower bound of the window, prefixed by its length, and advances by the amount written.fill_bin_protreturns an error if the window is too small to write the value.consume_bin_prot t readerreads a bin-prot value from the lower bound of the window, which should have been written usingfill_bin_prot, and advances the window by the amount read.consume_bin_protreturns 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
ReaderandWriter. They use a 4-byte length rather than an 8-byte length.
val consume_bin_prot : ([> Core__.Import.read ], seek) t -> 'a Bin_prot.Type_class.reader -> 'a Core__.Import.Or_error.t
module Blit : sig ... endBlitcopies between iobufs and advances neithersrcnordst.
module Blit_consume : sig ... endBlit_consumecopies between iobufs and advancessrcbut does not advancedst.
module Blit_fill : sig ... endBlit_fillcopies between iobufs and advancesdstbut does not advancesrc.
module Blit_consume_and_fill : sig ... endBlit_consume_and_fillcopies between iobufs and advances bothsrcanddst.
I/O
val compare_ok_or_eof : ok_or_eof -> ok_or_eof -> intval sexp_of_ok_or_eof : ok_or_eof -> Ppx_sexp_conv_lib.Sexp.t
val input : ([> Core__.Import.write ], seek) t -> Core__.Import.In_channel.t -> ok_or_eofIobufhas analogs of variousBigstringfunctions. These analogs advance by the amount written/read.
val read : ([> Core__.Import.write ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> ok_or_eofval read_assume_fd_is_nonblocking : ([> Core__.Import.write ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> Core__.Syscall_result.Unit.tval pread_assume_fd_is_nonblocking : ([> Core__.Import.write ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> offset:int -> unitval recvfrom_assume_fd_is_nonblocking : ([> Core__.Import.write ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> Core.Iobuf_intf.Unix.sockaddr
module Recvmmsg_context : sig ... end with type ('rw, 'seek) iobuf := ('rw, 'seek) trecvmmsg's context comprises data needed by the system call. Setup can be expensive, particularly for many buffers.
val recvmmsg_assume_fd_is_nonblocking : (Core.Iobuf_intf.Unix.File_descr.t -> Recvmmsg_context.t -> Core.Iobuf_intf.Unix.Syscall_result.Int.t) Core__.Import.Or_error.trecvmmsg_assume_fd_is_nonblocking fd contextreturns the number ofcontextiobufs read into (orerrno).fdmust not block.THREAD_IO_CUTOFFis ignored.EINVALis returned if anIobufpassed toRecvmmsg_context.createhas itsbufor limits changed.
val send_nonblocking_no_sigpipe : unit -> (([> Core__.Import.read ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> Core.Iobuf_intf.Unix.Syscall_result.Unit.t) Core__.Import.Or_error.tval sendto_nonblocking_no_sigpipe : unit -> (([> Core__.Import.read ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> Core.Iobuf_intf.Unix.sockaddr -> Core.Iobuf_intf.Unix.Syscall_result.Unit.t) Core__.Import.Or_error.tval output : ([> Core__.Import.read ], seek) t -> Core__.Import.Out_channel.t -> unitval write : ([> Core__.Import.read ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> unitval write_assume_fd_is_nonblocking : ([> Core__.Import.read ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> unitval pwrite_assume_fd_is_nonblocking : ([> Core__.Import.read ], seek) t -> Core.Iobuf_intf.Unix.File_descr.t -> offset:int -> unit
Expert
module Expert : sig ... endThe
Expertmodule is for building efficient out-of-moduleIobufabstractions.