Eio backend for bytesrw forked from anil.recoil.org/ocaml-bytesrw-eio
README.md

bytesrw-eio #

Adapters between Bytesrw readers and writers and Eio flows.

Bytesrw codecs (compression, JSON, CBOR and others) read from a Bytesrw.Bytes.Reader.t and write to a Bytesrw.Bytes.Writer.t, while Eio code works with flows and files. Bytesrw_eio converts between the two, in the way Bytesrw_unix does for Unix file descriptors. The adapters read from and write to the flow directly, without Eio.Buf_read or Eio.Buf_write in between, so Bytesrw does all the buffering.

Installation #

Install with opam:

$ opam install bytesrw-eio

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install bytesrw-eio

Usage #

Wrap a flow as a reader or a writer:

open Eio.Std

(* Create a reader from an Eio flow *)
let read_from_flow flow =
  let reader = Bytesrw_eio.bytes_reader_of_flow flow in
  (* Use reader with Bytesrw decoders *)
  reader

(* Create a writer to an Eio flow *)
let write_to_flow flow =
  let writer = Bytesrw_eio.bytes_writer_of_flow flow in
  (* Use writer with Bytesrw encoders *)
  writer

Choose the slice length:

(* Specify custom slice length for reading *)
let reader flow = Bytesrw_eio.bytes_reader_of_flow ~slice_length:4096 flow

(* Specify custom slice length for writing *)
let writer flow = Bytesrw_eio.bytes_writer_of_flow ~slice_length:4096 flow

Read a file from an offset, with one pread per slice:

(* Read from a specific offset in a file *)
let reader file = Bytesrw_eio.pread_reader ~offset:1024 file

API #

  • bytes_reader_of_flow and bytes_writer_of_flow turn an Eio source or sink into a Bytesrw reader or writer.
  • flow_source_of_bytes_reader turns a reader back into an Eio source, and flow_of_bytes_reader_writer turns a reader and a writer into a two-way flow, for libraries such as tls-eio that expect one.
  • bytes_reader_of_stream reads chunks that another fiber pushes on an Eio.Stream.t.
  • pread_reader reads a range of a file with pread, without loading the file into memory.

The interface is documented in lib/bytesrw_eio.mli, and odig doc bytesrw-eio renders it once the package is installed.

Licence #

ISC. See LICENSE.md.