Bytesrw 5259ab3

Bytesrw extends the OCaml Bytes module with composable, memory efficient, byte stream readers and writers compatible with effect based concurrency.

Except for byte slice life-times, these abstractions intentionally separate away ressource management and the specifics of reading and writing bytes.

Manuals

The following manuals are available:

Library bytesrw

This library has the base definition of bytes reader and writers as an extension of the Stdlib.Bytes module.

Library bytesrw.kit

The following modules rely only the Stdlib:

Libraries bytesrw.{blake3,md,unix,xxhash,zlib,zstd}

Each of these modules lives in its corresponding library. Compression libraries depend on their canonical C library.

Quick start

The following example compresses standard input to standard output with zstd using either a compressing bytes reader (pull) or writer (push).

open Bytesrw

let stdio_compress_reads () =
  try
    let stdin = Bytes.Reader.of_in_channel In_channel.stdin in
    let stdout = Bytes.Writer.of_out_channel Out_channel.stdout in
    let params = Bytesrw_zstd.Cctx_params.make ~checksum:true () in
    let zstdr = Bytesrw_zstd.compress_reads ~params () stdin in
    Bytes.Writer.write_reader ~eod:true stdout zstdr;
    Ok ()
  with
  | Bytes.Stream.Error e -> Bytes.Stream.error_to_result e
  | Sys_error e -> Error e

let stdio_compress_writes () =
  try
    let stdin = Bytes.Reader.of_in_channel In_channel.stdin in
    let stdout = Bytes.Writer.of_out_channel Out_channel.stdout in
    let params = Bytesrw_zstd.Cctx_params.make ~checksum:true () in
    let zstdw = Bytesrw_zstd.compress_writes ~params () ~eod:true stdout in
    Bytes.Writer.write_reader ~eod:true zstdw stdin;
    Ok ()
  with
  | Bytes.Stream.Error e -> Bytes.Stream.error_to_result e
  | Sys_error e -> Error e

Read filters can easily be applied to your strings:

let id s =
  let filters = Bytesrw_zstd.[compress_reads (); decompress_reads ()] in
  Bytes.Reader.filter_string filters s

More in the tutorial.

TODO

See also the design notes.