Skip to content

Commit 05cc457

Browse files
committed
Translate 'Lwt_io.read' without '~count'
1 parent 935fc5e commit 05cc457

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

bin/lwt_to_direct_style/ast_rewrite.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ let lwt_io_open ~backend ~state ~mode src =
160160
[Lwt_io.output].";
161161
None
162162

163+
let lwt_io_read ~backend ~state:_ count in_chan =
164+
match count with
165+
| Some count_arg -> backend#io_read_string_count in_chan count_arg
166+
| None -> Some (backend#io_read_all in_chan)
167+
163168
let mk_cstr c = Some (mk_constr_exp [ c ])
164169

165170
(* Rewrite calls to functions from the [Lwt] module. See [rewrite_apply] for
@@ -371,6 +376,9 @@ let rewrite_apply ~backend ~state full_ident args =
371376
return (lwt_io_open ~backend ~state ~mode (`Fname fname))
372377
| "Lwt_io", "read_line" ->
373378
take @@ fun in_chan -> return (Some (backend#io_read_line in_chan))
379+
| "Lwt_io", "read" ->
380+
take_lblopt "count" @@ fun count ->
381+
take @@ fun in_chan -> return (lwt_io_read ~backend ~state count in_chan)
374382
| "Lwt_io", "write" ->
375383
take @@ fun chan ->
376384
take @@ fun str -> return (Some (backend#io_write_str chan str))

bin/lwt_to_direct_style/concurrency_backend.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
219219
add_comment_dropped_exp ~label:"buffer length" buf_len;
220220
mk_apply_simple [ "Eio"; "Flow"; "single_read" ] [ input; buffer ]
221221

222+
method io_read_all input =
223+
mk_apply_simple [ "Eio"; "Buf_read"; "take_all" ] [ input ]
224+
225+
method io_read_string_count _input _count_arg =
226+
add_comment
227+
"Eio doesn't have a direct equivalent of [Lwt_io.read ~count]. Rewrite \
228+
the code using [Eio.Buf_read]'s lower level API or switch to \
229+
unbuffered IO.";
230+
None
231+
222232
method fd_close fd =
223233
(* TODO: See [of_unix_file_descr]. mk_apply_simple [ "Eio_unix"; "Fd" ] [ fd ] *)
224234
mk_apply_simple [ "Unix"; "close" ] [ fd ]

test/lwt_to_direct_style/eio-switch.t/run.t

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ Make a writable directory tree:
55
$ dune build @ocaml-index
66
$ lwt-to-direct-style --migrate --eio-sw-as-fiber-var Fiber_var.sw --eio-env-as-fiber-var Fiber_var.env
77
Formatted 1 files
8-
Warning: main.ml: 2 occurrences have not been rewritten.
9-
Lwt_io.read (line 15 column 12)
8+
Warning: main.ml: 1 occurrences have not been rewritten.
109
Lwt_io.printf (line 16 column 3)
1110

1211
$ cat main.ml
@@ -39,7 +38,7 @@ Make a writable directory tree:
3938
~close_unix:true fd
4039
: [ `R | `Flow | `Close ] r)
4140
in
42-
let s = Lwt_io.read in_chan in
41+
let s = Eio.Buf_read.take_all in_chan in
4342
Lwt_io.printf "%s" s
4443
4544
let () =

test/lwt_to_direct_style/to_direct_style.t/run.t

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ 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: (35 occurrences)
231+
lib/test_lwt_unix.ml: (38 occurrences)
232232
Lwt_io (line 7 column 8)
233233
Lwt.return (line 11 column 3)
234234
Lwt.let* (line 10 column 3)
@@ -248,6 +248,9 @@ Make a writable directory tree:
248248
Lwt_io.of_fd (line 22 column 13)
249249
Lwt_io.of_fd (line 23 column 13)
250250
Lwt_io.read_line (line 28 column 15)
251+
Lwt_io.read (line 41 column 15)
252+
Lwt_io.read (line 42 column 15)
253+
Lwt_io.read (line 43 column 15)
251254
Lwt_io.read_into (line 10 column 19)
252255
Lwt_io.write (line 24 column 19)
253256
Lwt_io.length (line 36 column 3)
@@ -294,8 +297,10 @@ Make a writable directory tree:
294297
Lwt.Fail (line 147 column 5)
295298
Lwt.let* (line 163 column 21)
296299
Lwt.Fail (line 179 column 9)
297-
Warning: lib/test_lwt_unix.ml: 1 occurrences have not been rewritten.
300+
Warning: lib/test_lwt_unix.ml: 3 occurrences have not been rewritten.
298301
Lwt_io.stdout (line 26 column 33)
302+
Lwt_io.read (line 42 column 15)
303+
Lwt_io.read (line 43 column 15)
299304
Warning: lib/test.mli: 2 occurrences have not been rewritten.
300305
Lwt_mutex.t (line 2 column 10)
301306
Lwt_mutex.t (line 3 column 10)
@@ -772,3 +777,17 @@ Make a writable directory tree:
772777
(Eio.Path.( / ) env#cwd
773778
(* TODO: lwt-to-direct-style: [env] must be propagated from the main loop *)
774779
fname)
780+
781+
let _f chan = Eio.Buf_read.take_all chan
782+
783+
let _f chan =
784+
Lwt_io.read
785+
(* TODO: lwt-to-direct-style: Eio doesn't have a direct equivalent of [Lwt_io.read ~count]. Rewrite the code using [Eio.Buf_read]'s lower level API or switch to unbuffered IO. *)
786+
(* TODO: lwt-to-direct-style: Eio doesn't have a direct equivalent of [Lwt_io.read ~count]. Rewrite the code using [Eio.Buf_read]'s lower level API or switch to unbuffered IO. *)
787+
~count:42 chan
788+
789+
let _f chan =
790+
Lwt_io.read
791+
(* TODO: lwt-to-direct-style: Eio doesn't have a direct equivalent of [Lwt_io.read ~count]. Rewrite the code using [Eio.Buf_read]'s lower level API or switch to unbuffered IO. *)
792+
(* TODO: lwt-to-direct-style: Eio doesn't have a direct equivalent of [Lwt_io.read ~count]. Rewrite the code using [Eio.Buf_read]'s lower level API or switch to unbuffered IO. *)
793+
?count:(Some 42) chan

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ let _f fname =
3737

3838
let _f fname = Lwt_unix.stat fname
3939
let _f fname = Lwt_unix.lstat fname
40+
41+
let _f chan = Lwt_io.read chan
42+
let _f chan = Lwt_io.read ~count:42 chan
43+
let _f chan = Lwt_io.read ?count:(Some 42) chan

0 commit comments

Comments
 (0)