Skip to content

Commit 9d62a6e

Browse files
committed
Add Flow.load
1 parent 23a1a84 commit 9d62a6e

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

lib_eio/eio.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ module Lazy = Lazy
1212
module Pool = Pool
1313
module Exn = Exn
1414
module Resource = Resource
15-
module Flow = Flow
1615
module Buf_read = Buf_read
16+
module Flow = struct
17+
include Flow
18+
19+
let read_all flow =
20+
Buf_read.(parse_exn take_all) flow ~max_size:max_int
21+
end
1722
module Buf_write = Buf_write
1823
module Net = Net
1924
module Process = Process

lib_eio/eio.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ module Std = Std
6060
module Resource = Resource
6161

6262
(** Byte streams. *)
63-
module Flow = Flow
63+
module Flow : sig
64+
include module type of Flow
65+
66+
val read_all : _ source -> string
67+
(** [read_all src] is a convenience wrapper to read an entire flow efficiently. *)
68+
end
6469

6570
(** Buffered input and parsing *)
6671
module Buf_read = Buf_read

tests/buf_reader.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ Exception: End_of_file.
241241

242242
```ocaml
243243
# let bflow = R.of_flow mock_flow ~max_size:100 |> R.as_flow;;
244-
val bflow : Eio.Flow.source_ty Eio.Std.r = Eio__.Resource.T (<poly>, <abstr>)
244+
val bflow : Eio__Flow.source_ty Eio.Std.r =
245+
Eio__.Resource.T (<poly>, <abstr>)
245246
# next := ["foo"; "bar"]; read bflow 2;;
246247
+mock_flow returning 3 bytes
247248
+Read "fo"

tests/flow.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ Copying from src using `Read_source_buffer`:
108108
- : unit = ()
109109
```
110110

111+
## read_all
112+
113+
```ocaml
114+
# run @@ fun () ->
115+
let each = String.init 256 Char.chr in
116+
let data = List.init 40 (fun _ -> Cstruct.of_string each) in
117+
let got = Eio.Flow.read_all (mock_source data) in
118+
traceln "Input length: %d\nOutput length: %d\nEqual: %b"
119+
(Cstruct.lenv data) (String.length got) (String.equal got (Cstruct.copyv data));
120+
;;
121+
+Input length: 10240
122+
+Output length: 10240
123+
+Equal: true
124+
- : unit = ()
125+
```
126+
111127
## write
112128

113129
```ocaml

0 commit comments

Comments
 (0)