Bytesrw adapter for Eio
ocaml codec

initial import

+1
.gitignore
···
···
+
_build
+27
bytesrw-eio.opam
···
···
+
# This file is generated by dune, edit dune-project instead
+
opam-version: "2.0"
+
synopsis: "Bytesrw readers and writers for Eio"
+
description:
+
"Provides Bytesrw.Bytes.Reader and Writer adapters for Eio Flows"
+
depends: [
+
"dune" {>= "3.18"}
+
"ocaml" {>= "5.0"}
+
"bytesrw" {>= "0.2"}
+
"eio" {>= "1.0"}
+
"odoc" {with-doc}
+
]
+
build: [
+
["dune" "subst"] {dev}
+
[
+
"dune"
+
"build"
+
"-p"
+
name
+
"-j"
+
jobs
+
"@install"
+
"@runtest" {with-test}
+
"@doc" {with-doc}
+
]
+
]
+
x-maintenance-intent: ["(latest)"]
+13
dune-project
···
···
+
(lang dune 3.18)
+
(name bytesrw-eio)
+
+
(generate_opam_files true)
+
+
(package
+
(name bytesrw-eio)
+
(synopsis "Bytesrw readers and writers for Eio")
+
(description "Provides Bytesrw.Bytes.Reader and Writer adapters for Eio Flows")
+
(depends
+
(ocaml (>= 5.0))
+
(bytesrw (>= 0.2))
+
(eio (>= 1.0))))
+54
src/bytesrw_eio.ml
···
···
+
(*---------------------------------------------------------------------------
+
Copyright (c) 2025 Anil Madhavapeddy. All rights reserved.
+
SPDX-License-Identifier: ISC
+
---------------------------------------------------------------------------*)
+
+
(** Bytesrw adapters for Eio
+
+
This module provides adapters to create {!Bytesrw.Bytes.Reader.t} and
+
{!Bytesrw.Bytes.Writer.t} from Eio flows, mirroring the API of
+
{!Bytesrw_unix} for Eio's effect-based I/O. *)
+
+
open Bytesrw
+
+
(** Create a [Bytes.Reader.t] from an Eio source flow.
+
+
Reads directly from the flow without intermediate buffering.
+
+
@param slice_length Maximum bytes per slice (default: 65536, which is {!Bytes.Slice.unix_io_buffer_size}) *)
+
let bytes_reader_of_flow
+
?(slice_length = Bytes.Slice.unix_io_buffer_size)
+
(flow : _ Eio.Flow.source)
+
: Bytes.Reader.t =
+
let buf = Bytes.create (Bytes.Slice.check_length slice_length) in
+
let cstruct = Cstruct.of_bytes buf in
+
let read () =
+
match Eio.Flow.single_read flow cstruct with
+
| 0 -> Bytes.Slice.eod
+
| count -> Bytes.Slice.make buf ~first:0 ~length:count
+
| exception End_of_file -> Bytes.Slice.eod
+
in
+
Bytes.Reader.make ~slice_length read
+
+
(** Create a [Bytes.Writer.t] from an Eio sink flow.
+
+
Writes directly to the flow without intermediate buffering.
+
+
@param slice_length Suggested slice length for upstream (default: 65536, which is {!Bytes.Slice.unix_io_buffer_size}) *)
+
let bytes_writer_of_flow
+
?(slice_length = Bytes.Slice.unix_io_buffer_size)
+
(flow : _ Eio.Flow.sink)
+
: Bytes.Writer.t =
+
let rec write slice =
+
if Bytes.Slice.is_eod slice then ()
+
else begin
+
let bytes = Bytes.Slice.bytes slice in
+
let first = Bytes.Slice.first slice in
+
let length = Bytes.Slice.length slice in
+
let cstruct = Cstruct.of_bytes ~off:first ~len:length bytes in
+
match Eio.Flow.single_write flow [cstruct] with
+
| count when count = length -> ()
+
| count -> write (Option.get (Bytes.Slice.drop count slice))
+
end
+
in
+
Bytes.Writer.make ~slice_length write
+32
src/bytesrw_eio.mli
···
···
+
(** Bytesrw adapters for Eio
+
+
This module provides adapters to create {!Bytesrw.Bytes.Reader.t} and
+
{!Bytesrw.Bytes.Writer.t} from Eio flows, mirroring the API of
+
{!Bytesrw_unix} for Eio's effect-based I/O.
+
+
Unlike the Buf_read/Buf_write wrappers, these adapters read and write
+
directly to the flow, allowing bytesrw to handle its own buffering. *)
+
+
(** {1 Readers} *)
+
+
val bytes_reader_of_flow :
+
?slice_length:int ->
+
_ Eio.Flow.source ->
+
Bytesrw.Bytes.Reader.t
+
(** [bytes_reader_of_flow flow] creates a reader from an Eio source flow.
+
+
Reads directly from the flow without intermediate buffering.
+
+
@param slice_length Maximum bytes per slice (default: 65536) *)
+
+
(** {1 Writers} *)
+
+
val bytes_writer_of_flow :
+
?slice_length:int ->
+
_ Eio.Flow.sink ->
+
Bytesrw.Bytes.Writer.t
+
(** [bytes_writer_of_flow flow] creates a writer from an Eio sink flow.
+
+
Writes directly to the flow without intermediate buffering.
+
+
@param slice_length Suggested slice length for upstream (default: 65536) *)
+4
src/dune
···
···
+
(library
+
(name bytesrw_eio)
+
(public_name bytesrw-eio)
+
(libraries bytesrw eio))