Bytesrw adapter for Eio
ocaml codec

initial import

+131
+1
.gitignore
···
··· 1 + _build
+27
bytesrw-eio.opam
···
··· 1 + # This file is generated by dune, edit dune-project instead 2 + opam-version: "2.0" 3 + synopsis: "Bytesrw readers and writers for Eio" 4 + description: 5 + "Provides Bytesrw.Bytes.Reader and Writer adapters for Eio Flows" 6 + depends: [ 7 + "dune" {>= "3.18"} 8 + "ocaml" {>= "5.0"} 9 + "bytesrw" {>= "0.2"} 10 + "eio" {>= "1.0"} 11 + "odoc" {with-doc} 12 + ] 13 + build: [ 14 + ["dune" "subst"] {dev} 15 + [ 16 + "dune" 17 + "build" 18 + "-p" 19 + name 20 + "-j" 21 + jobs 22 + "@install" 23 + "@runtest" {with-test} 24 + "@doc" {with-doc} 25 + ] 26 + ] 27 + x-maintenance-intent: ["(latest)"]
+13
dune-project
···
··· 1 + (lang dune 3.18) 2 + (name bytesrw-eio) 3 + 4 + (generate_opam_files true) 5 + 6 + (package 7 + (name bytesrw-eio) 8 + (synopsis "Bytesrw readers and writers for Eio") 9 + (description "Provides Bytesrw.Bytes.Reader and Writer adapters for Eio Flows") 10 + (depends 11 + (ocaml (>= 5.0)) 12 + (bytesrw (>= 0.2)) 13 + (eio (>= 1.0))))
+54
src/bytesrw_eio.ml
···
··· 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
···
··· 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) *)
+4
src/dune
···
··· 1 + (library 2 + (name bytesrw_eio) 3 + (public_name bytesrw-eio) 4 + (libraries bytesrw eio))