Skip to content

Commit 2fec339

Browse files
committed
Translate 'Lwt_io.read_into_exactly'
1 parent 98827ed commit 2fec339

File tree

4 files changed

+56
-40
lines changed

4 files changed

+56
-40
lines changed

bin/lwt_to_direct_style/ast_rewrite.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,13 @@ let rewrite_apply ~backend ~state full_ident args =
356356
| "Lwt_mutex", "with_lock" ->
357357
take @@ fun t ->
358358
take @@ fun f -> return (Some (backend#mutex_with_lock t f))
359-
| "Lwt_io", "read_into" ->
359+
| "Lwt_io", (("read_into" | "read_into_exactly") as ident) ->
360+
let exactly = ident = "read_into_exactly" in
360361
take @@ fun input ->
361362
take @@ fun buffer ->
362363
take @@ fun buf_off ->
363364
take @@ fun buf_len ->
364-
return (Some (backend#io_read input buffer buf_off buf_len))
365+
return (Some (backend#io_read ~exactly input buffer buf_off buf_len))
365366
| "Lwt_io", "of_fd" ->
366367
ignore_lblarg "buffer"
367368
@@ ignore_lblarg ~cmt:"Will behave as if it was [true]." "close"

bin/lwt_to_direct_style/concurrency_backend.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
1818
[ i ]
1919
in
2020
let add_comment fmt = Format.kasprintf add_comment fmt in
21-
let add_comment_dropped_exp ~label exp =
22-
add_comment "Dropped expression (%s): [%s]." label
21+
let add_comment_dropped_exp ~label ?(cmt = "") exp =
22+
add_comment "Dropped expression (%s): [%s].%s" label
2323
(Ocamlformat_utils.format_expression exp)
24+
cmt
2425
in
2526
(* If [--eio-sw-as-fiber-var] is passed on the command line, this will query
2627
the current switch. Otherwise, this will generate a comment.
@@ -206,7 +207,7 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
206207
(* ]) *)
207208
fd
208209

209-
method io_read input buffer buf_offset buf_len =
210+
method io_read ~exactly input buffer buf_offset buf_len =
210211
add_comment "[%s] should be a [Cstruct.t]."
211212
(Ocamlformat_utils.format_expression buffer);
212213
add_comment
@@ -215,9 +216,12 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
215216
contains an internal buffer) or change the call to \
216217
[Eio.Buf_read.of_flow] used to create the buffer."
217218
(Ocamlformat_utils.format_expression input);
218-
add_comment_dropped_exp ~label:"buffer offset" buf_offset;
219-
add_comment_dropped_exp ~label:"buffer length" buf_len;
220-
mk_apply_simple [ "Eio"; "Flow"; "single_read" ] [ input; buffer ]
219+
add_comment_dropped_exp ~label:"buffer offset"
220+
~cmt:" This will behave as if it was [0]." buf_offset;
221+
add_comment_dropped_exp ~label:"buffer length"
222+
~cmt:" This will behave as if it was [Cstruct.length buffer]." buf_len;
223+
let fun_ = if exactly then "read_exact" else "single_read" in
224+
mk_apply_simple [ "Eio"; "Flow"; fun_ ] [ input; buffer ]
221225

222226
method io_read_all input =
223227
mk_apply_simple [ "Eio"; "Buf_read"; "take_all" ] [ input ]

test/lwt_to_direct_style/to_direct_style.t/run.t

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -228,45 +228,47 @@ Make a writable directory tree:
228228
Lwt_mutex.lock (line 136 column 9)
229229
Lwt_mutex.unlock (line 137 column 9)
230230
Lwt_mutex.with_lock (line 138 column 9)
231-
lib/test_lwt_unix.ml: (38 occurrences)
231+
lib/test_lwt_unix.ml: (40 occurrences)
232232
Lwt_io (line 7 column 8)
233-
Lwt.return (line 11 column 3)
233+
Lwt.return (line 12 column 3)
234234
Lwt.let* (line 10 column 3)
235-
Lwt.let* (line 31 column 3)
236-
Lwt.let* (line 35 column 3)
235+
Lwt.let* (line 11 column 3)
236+
Lwt.let* (line 32 column 3)
237+
Lwt.let* (line 36 column 3)
237238
Lwt.Syntax (line 1 column 6)
238-
Lwt_io.Input (line 22 column 32)
239-
Lwt_io.Output (line 23 column 32)
239+
Lwt_io.Input (line 23 column 32)
240+
Lwt_io.Output (line 24 column 32)
240241
Lwt_io.input (line 7 column 28)
241-
Lwt_io.input (line 31 column 36)
242-
Lwt_io.output (line 21 column 32)
243-
Lwt_io.output (line 35 column 36)
244-
Lwt_io.output_channel (line 26 column 9)
245-
Lwt_io.close (line 32 column 3)
242+
Lwt_io.input (line 32 column 36)
243+
Lwt_io.output (line 22 column 32)
244+
Lwt_io.output (line 36 column 36)
245+
Lwt_io.output_channel (line 27 column 9)
246+
Lwt_io.close (line 33 column 3)
246247
Lwt_io.of_fd (line 7 column 16)
247-
Lwt_io.of_fd (line 21 column 13)
248248
Lwt_io.of_fd (line 22 column 13)
249249
Lwt_io.of_fd (line 23 column 13)
250-
Lwt_io.read_line (line 28 column 15)
251-
Lwt_io.read (line 41 column 15)
250+
Lwt_io.of_fd (line 24 column 13)
251+
Lwt_io.read_line (line 29 column 15)
252252
Lwt_io.read (line 42 column 15)
253253
Lwt_io.read (line 43 column 15)
254+
Lwt_io.read (line 44 column 15)
254255
Lwt_io.read_into (line 10 column 19)
255-
Lwt_io.write (line 24 column 19)
256-
Lwt_io.length (line 36 column 3)
257-
Lwt_io.stdout (line 26 column 33)
258-
Lwt_io.open_file (line 31 column 13)
259-
Lwt_io.open_file (line 35 column 13)
260-
Lwt_unix.Timeout (line 13 column 9)
256+
Lwt_io.read_into_exactly (line 11 column 13)
257+
Lwt_io.write (line 25 column 19)
258+
Lwt_io.length (line 37 column 3)
259+
Lwt_io.stdout (line 27 column 33)
260+
Lwt_io.open_file (line 32 column 13)
261+
Lwt_io.open_file (line 36 column 13)
262+
Lwt_unix.Timeout (line 14 column 9)
261263
Lwt_unix.of_unix_file_descr (line 6 column 8)
262-
Lwt_unix.stat (line 38 column 16)
263-
Lwt_unix.lstat (line 39 column 16)
264-
Lwt_unix.sockaddr (line 14 column 9)
265-
Lwt_unix.ADDR_UNIX (line 14 column 29)
266-
Lwt_unix.ADDR_UNIX (line 16 column 6)
267-
Lwt_unix.ADDR_INET (line 16 column 29)
268-
Lwt_unix.ADDR_INET (line 17 column 3)
269-
Lwt_unix.getaddrinfo (line 19 column 9)
264+
Lwt_unix.stat (line 39 column 16)
265+
Lwt_unix.lstat (line 40 column 16)
266+
Lwt_unix.sockaddr (line 15 column 9)
267+
Lwt_unix.ADDR_UNIX (line 15 column 29)
268+
Lwt_unix.ADDR_UNIX (line 17 column 6)
269+
Lwt_unix.ADDR_INET (line 17 column 29)
270+
Lwt_unix.ADDR_INET (line 18 column 3)
271+
Lwt_unix.getaddrinfo (line 20 column 9)
270272
lib/test.mli: (17 occurrences)
271273
Lwt (line 12 column 26)
272274
Lwt.t (line 1 column 35)
@@ -298,9 +300,9 @@ Make a writable directory tree:
298300
Lwt.let* (line 163 column 21)
299301
Lwt.Fail (line 179 column 9)
300302
Warning: lib/test_lwt_unix.ml: 3 occurrences have not been rewritten.
301-
Lwt_io.stdout (line 26 column 33)
302-
Lwt_io.read (line 42 column 15)
303+
Lwt_io.stdout (line 27 column 33)
303304
Lwt_io.read (line 43 column 15)
305+
Lwt_io.read (line 44 column 15)
304306
Warning: lib/test.mli: 2 occurrences have not been rewritten.
305307
Lwt_mutex.t (line 2 column 10)
306308
Lwt_mutex.t (line 3 column 10)
@@ -695,8 +697,16 @@ Make a writable directory tree:
695697
Eio.Flow.single_read
696698
(* TODO: lwt-to-direct-style: [buf] should be a [Cstruct.t]. *)
697699
(* TODO: lwt-to-direct-style: [Eio.Flow.single_read] operates on a [Flow.source] but [inp] is likely of type [Eio.Buf_read.t]. Rewrite this code to use [Buf_read] (which contains an internal buffer) or change the call to [Eio.Buf_read.of_flow] used to create the buffer. *)
698-
(* TODO: lwt-to-direct-style: Dropped expression (buffer offset): [0]. *)
699-
(* TODO: lwt-to-direct-style: Dropped expression (buffer length): [1024]. *)
700+
(* TODO: lwt-to-direct-style: Dropped expression (buffer offset): [0]. This will behave as if it was [0]. *)
701+
(* TODO: lwt-to-direct-style: Dropped expression (buffer length): [1024]. This will behave as if it was [Cstruct.length buffer]. *)
702+
inp buf
703+
in
704+
let () =
705+
Eio.Flow.read_exact
706+
(* TODO: lwt-to-direct-style: [buf] should be a [Cstruct.t]. *)
707+
(* TODO: lwt-to-direct-style: [Eio.Flow.single_read] operates on a [Flow.source] but [inp] is likely of type [Eio.Buf_read.t]. Rewrite this code to use [Buf_read] (which contains an internal buffer) or change the call to [Eio.Buf_read.of_flow] used to create the buffer. *)
708+
(* TODO: lwt-to-direct-style: Dropped expression (buffer offset): [0]. This will behave as if it was [0]. *)
709+
(* TODO: lwt-to-direct-style: Dropped expression (buffer length): [1024]. This will behave as if it was [Cstruct.length buffer]. *)
700710
inp buf
701711
in
702712
()

test/lwt_to_direct_style/to_direct_style.t/src/lib/test_lwt_unix.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let _f fname =
88
in
99
let buf = Bytes.create 1024 in
1010
let* _n : int = Lwt_io.read_into inp buf 0 1024 in
11+
let* () = Lwt_io.read_into_exactly inp buf 0 1024 in
1112
Lwt.return ()
1213

1314
let _ = Lwt_unix.Timeout

0 commit comments

Comments
 (0)