···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2025 Anil Madhavapeddy. All rights reserved.
33+ SPDX-License-Identifier: ISC
44+ ---------------------------------------------------------------------------*)
55+66+(** Bytesrw adapters for Eio
77+88+ This module provides adapters to create {!Bytesrw.Bytes.Reader.t} and
99+ {!Bytesrw.Bytes.Writer.t} from Eio flows, mirroring the API of
1010+ {!Bytesrw_unix} for Eio's effect-based I/O. *)
1111+1212+open Bytesrw
1313+1414+(** Create a [Bytes.Reader.t] from an Eio source flow.
1515+1616+ Reads directly from the flow without intermediate buffering.
1717+1818+ @param slice_length Maximum bytes per slice (default: 65536, which is {!Bytes.Slice.unix_io_buffer_size}) *)
1919+let bytes_reader_of_flow
2020+ ?(slice_length = Bytes.Slice.unix_io_buffer_size)
2121+ (flow : _ Eio.Flow.source)
2222+ : Bytes.Reader.t =
2323+ let buf = Bytes.create (Bytes.Slice.check_length slice_length) in
2424+ let cstruct = Cstruct.of_bytes buf in
2525+ let read () =
2626+ match Eio.Flow.single_read flow cstruct with
2727+ | 0 -> Bytes.Slice.eod
2828+ | count -> Bytes.Slice.make buf ~first:0 ~length:count
2929+ | exception End_of_file -> Bytes.Slice.eod
3030+ in
3131+ Bytes.Reader.make ~slice_length read
3232+3333+(** Create a [Bytes.Writer.t] from an Eio sink flow.
3434+3535+ Writes directly to the flow without intermediate buffering.
3636+3737+ @param slice_length Suggested slice length for upstream (default: 65536, which is {!Bytes.Slice.unix_io_buffer_size}) *)
3838+let bytes_writer_of_flow
3939+ ?(slice_length = Bytes.Slice.unix_io_buffer_size)
4040+ (flow : _ Eio.Flow.sink)
4141+ : Bytes.Writer.t =
4242+ let rec write slice =
4343+ if Bytes.Slice.is_eod slice then ()
4444+ else begin
4545+ let bytes = Bytes.Slice.bytes slice in
4646+ let first = Bytes.Slice.first slice in
4747+ let length = Bytes.Slice.length slice in
4848+ let cstruct = Cstruct.of_bytes ~off:first ~len:length bytes in
4949+ match Eio.Flow.single_write flow [cstruct] with
5050+ | count when count = length -> ()
5151+ | count -> write (Option.get (Bytes.Slice.drop count slice))
5252+ end
5353+ in
5454+ Bytes.Writer.make ~slice_length write
+32
src/bytesrw_eio.mli
···11+(** Bytesrw adapters for Eio
22+33+ This module provides adapters to create {!Bytesrw.Bytes.Reader.t} and
44+ {!Bytesrw.Bytes.Writer.t} from Eio flows, mirroring the API of
55+ {!Bytesrw_unix} for Eio's effect-based I/O.
66+77+ Unlike the Buf_read/Buf_write wrappers, these adapters read and write
88+ directly to the flow, allowing bytesrw to handle its own buffering. *)
99+1010+(** {1 Readers} *)
1111+1212+val bytes_reader_of_flow :
1313+ ?slice_length:int ->
1414+ _ Eio.Flow.source ->
1515+ Bytesrw.Bytes.Reader.t
1616+(** [bytes_reader_of_flow flow] creates a reader from an Eio source flow.
1717+1818+ Reads directly from the flow without intermediate buffering.
1919+2020+ @param slice_length Maximum bytes per slice (default: 65536) *)
2121+2222+(** {1 Writers} *)
2323+2424+val bytes_writer_of_flow :
2525+ ?slice_length:int ->
2626+ _ Eio.Flow.sink ->
2727+ Bytesrw.Bytes.Writer.t
2828+(** [bytes_writer_of_flow flow] creates a writer from an Eio sink flow.
2929+3030+ Writes directly to the flow without intermediate buffering.
3131+3232+ @param slice_length Suggested slice length for upstream (default: 65536) *)