···1+(*---------------------------------------------------------------------------
2+ Copyright (c) 2025 Anil Madhavapeddy. All rights reserved.
3+ SPDX-License-Identifier: ISC
4+ ---------------------------------------------------------------------------*)
5+6+(** Bytesrw adapters for Eio
7+8+ This module provides adapters to create {!Bytesrw.Bytes.Reader.t} and
9+ {!Bytesrw.Bytes.Writer.t} from Eio flows, mirroring the API of
10+ {!Bytesrw_unix} for Eio's effect-based I/O. *)
11+12+open Bytesrw
13+14+(** Create a [Bytes.Reader.t] from an Eio source flow.
15+16+ Reads directly from the flow without intermediate buffering.
17+18+ @param slice_length Maximum bytes per slice (default: 65536, which is {!Bytes.Slice.unix_io_buffer_size}) *)
19+let bytes_reader_of_flow
20+ ?(slice_length = Bytes.Slice.unix_io_buffer_size)
21+ (flow : _ Eio.Flow.source)
22+ : Bytes.Reader.t =
23+ let buf = Bytes.create (Bytes.Slice.check_length slice_length) in
24+ let cstruct = Cstruct.of_bytes buf in
25+ let read () =
26+ match Eio.Flow.single_read flow cstruct with
27+ | 0 -> Bytes.Slice.eod
28+ | count -> Bytes.Slice.make buf ~first:0 ~length:count
29+ | exception End_of_file -> Bytes.Slice.eod
30+ in
31+ Bytes.Reader.make ~slice_length read
32+33+(** Create a [Bytes.Writer.t] from an Eio sink flow.
34+35+ Writes directly to the flow without intermediate buffering.
36+37+ @param slice_length Suggested slice length for upstream (default: 65536, which is {!Bytes.Slice.unix_io_buffer_size}) *)
38+let bytes_writer_of_flow
39+ ?(slice_length = Bytes.Slice.unix_io_buffer_size)
40+ (flow : _ Eio.Flow.sink)
41+ : Bytes.Writer.t =
42+ let rec write slice =
43+ if Bytes.Slice.is_eod slice then ()
44+ else begin
45+ let bytes = Bytes.Slice.bytes slice in
46+ let first = Bytes.Slice.first slice in
47+ let length = Bytes.Slice.length slice in
48+ let cstruct = Cstruct.of_bytes ~off:first ~len:length bytes in
49+ match Eio.Flow.single_write flow [cstruct] with
50+ | count when count = length -> ()
51+ | count -> write (Option.get (Bytes.Slice.drop count slice))
52+ end
53+ in
54+ Bytes.Writer.make ~slice_length write
+32
src/bytesrw_eio.mli
···00000000000000000000000000000000
···1+(** Bytesrw adapters for Eio
2+3+ This module provides adapters to create {!Bytesrw.Bytes.Reader.t} and
4+ {!Bytesrw.Bytes.Writer.t} from Eio flows, mirroring the API of
5+ {!Bytesrw_unix} for Eio's effect-based I/O.
6+7+ Unlike the Buf_read/Buf_write wrappers, these adapters read and write
8+ directly to the flow, allowing bytesrw to handle its own buffering. *)
9+10+(** {1 Readers} *)
11+12+val bytes_reader_of_flow :
13+ ?slice_length:int ->
14+ _ Eio.Flow.source ->
15+ Bytesrw.Bytes.Reader.t
16+(** [bytes_reader_of_flow flow] creates a reader from an Eio source flow.
17+18+ Reads directly from the flow without intermediate buffering.
19+20+ @param slice_length Maximum bytes per slice (default: 65536) *)
21+22+(** {1 Writers} *)
23+24+val bytes_writer_of_flow :
25+ ?slice_length:int ->
26+ _ Eio.Flow.sink ->
27+ Bytesrw.Bytes.Writer.t
28+(** [bytes_writer_of_flow flow] creates a writer from an Eio sink flow.
29+30+ Writes directly to the flow without intermediate buffering.
31+32+ @param slice_length Suggested slice length for upstream (default: 65536) *)