Skip to content

Commit

Permalink
Merge pull request #596 from SGrondin/add-flow-load
Browse files Browse the repository at this point in the history
Add Flow.read_all
  • Loading branch information
talex5 authored Oct 11, 2023
2 parents 669c2e8 + c6067a9 commit ee51b04
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib_eio/eio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ module Lazy = Lazy
module Pool = Pool
module Exn = Exn
module Resource = Resource
module Flow = Flow
module Buf_read = Buf_read
module Flow = struct
include Flow

let read_all flow =
Buf_read.(parse_exn take_all) flow ~max_size:max_int
end
module Buf_write = Buf_write
module Net = Net
module Process = Process
Expand Down
11 changes: 10 additions & 1 deletion lib_eio/eio.mli
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ module Std = Std
module Resource = Resource

(** Byte streams. *)
module Flow = Flow
module Flow : sig
include module type of Flow (** @inline *)

(** {2 Convenience wrappers} *)

val read_all : _ source -> string
(** [read_all src] is a convenience wrapper to read an entire flow.
It is the same as [Buf_read.(parse_exn take_all) src ~max_size:max_int] *)
end

(** Buffered input and parsing *)
module Buf_read = Buf_read
Expand Down
3 changes: 2 additions & 1 deletion tests/buf_reader.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ Exception: End_of_file.

```ocaml
# let bflow = R.of_flow mock_flow ~max_size:100 |> R.as_flow;;
val bflow : Eio.Flow.source_ty Eio.Std.r = Eio__.Resource.T (<poly>, <abstr>)
val bflow : Eio__Flow.source_ty Eio.Std.r =
Eio__.Resource.T (<poly>, <abstr>)
# next := ["foo"; "bar"]; read bflow 2;;
+mock_flow returning 3 bytes
+Read "fo"
Expand Down
16 changes: 16 additions & 0 deletions tests/flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ Copying from src using `Read_source_buffer`:
- : unit = ()
```

## read_all

```ocaml
# run @@ fun () ->
let each = String.init 256 Char.chr in
let data = List.init 40 (fun _ -> Cstruct.of_string each) in
let got = Eio.Flow.read_all (mock_source data) in
traceln "Input length: %d\nOutput length: %d\nEqual: %b"
(Cstruct.lenv data) (String.length got) (String.equal got (Cstruct.copyv data));
;;
+Input length: 10240
+Output length: 10240
+Equal: true
- : unit = ()
```

## write

```ocaml
Expand Down

0 comments on commit ee51b04

Please sign in to comment.