···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. 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
1919- Maximum bytes per slice (default: 65536, which is
2020- {!Bytes.Slice.unix_io_buffer_size}) *)
2121-let bytes_reader_of_flow ?(slice_length = Bytes.Slice.unix_io_buffer_size)
2222- (flow : _ Eio.Flow.source) : Bytes.Reader.t =
2323- let buf_size = Bytes.Slice.check_length slice_length in
2424- let read () =
2525- let cstruct = Cstruct.create buf_size in
2626- match Eio.Flow.single_read flow cstruct with
2727- | 0 -> Bytes.Slice.eod
2828- | count ->
2929- let data_cs = Cstruct.sub cstruct 0 count in
3030- let buf = Cstruct.to_bytes data_cs in
3131- Bytes.Slice.make buf ~first:0 ~length:count
3232- | exception End_of_file -> Bytes.Slice.eod
3333- in
3434- Bytes.Reader.make ~slice_length read
3535-3636-(** Create a [Bytes.Writer.t] from an Eio sink flow.
3737-3838- Writes directly to the flow without intermediate buffering.
3939-4040- @param slice_length
4141- Suggested slice length for upstream (default: 65536, which is
4242- {!Bytes.Slice.unix_io_buffer_size}) *)
4343-let bytes_writer_of_flow ?(slice_length = Bytes.Slice.unix_io_buffer_size)
4444- (flow : _ Eio.Flow.sink) : Bytes.Writer.t =
4545- let rec write slice =
4646- if Bytes.Slice.is_eod slice then ()
4747- else begin
4848- let bytes = Bytes.Slice.bytes slice in
4949- let first = Bytes.Slice.first slice in
5050- let length = Bytes.Slice.length slice in
5151- let cstruct = Cstruct.of_bytes ~off:first ~len:length bytes in
5252- match Eio.Flow.single_write flow [ cstruct ] with
5353- | count when count = length -> ()
5454- | count -> write (Option.get (Bytes.Slice.drop count slice))
5555- end
5656- in
5757- Bytes.Writer.make ~slice_length write
-33
src/bytesrw_eio.mli
···11-(*---------------------------------------------------------------------------
22- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. 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- Unlike the Buf_read/Buf_write wrappers, these adapters read and write
1313- directly to the flow, allowing bytesrw to handle its own buffering. *)
1414-1515-(** {1 Readers} *)
1616-1717-val bytes_reader_of_flow :
1818- ?slice_length:int -> _ Eio.Flow.source -> Bytesrw.Bytes.Reader.t
1919-(** [bytes_reader_of_flow flow] creates a reader from an Eio source flow.
2020-2121- Reads directly from the flow without intermediate buffering.
2222-2323- @param slice_length Maximum bytes per slice (default: 65536) *)
2424-2525-(** {1 Writers} *)
2626-2727-val bytes_writer_of_flow :
2828- ?slice_length:int -> _ Eio.Flow.sink -> Bytesrw.Bytes.Writer.t
2929-(** [bytes_writer_of_flow flow] creates a writer from an Eio sink flow.
3030-3131- Writes directly to the flow without intermediate buffering.
3232-3333- @param slice_length Suggested slice length for upstream (default: 65536) *)