Skip to content

Commit 8340544

Browse files
committed
Translate 'Lwt_io.with_connection'
1 parent 7198c32 commit 8340544

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

bin/lwt_to_direct_style/ast_rewrite.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,11 @@ let rewrite_apply ~backend ~state full_ident args =
399399
| "Lwt_io", "length" -> take @@ fun fd -> return (Some (backend#io_length fd))
400400
| "Lwt_io", "close" -> take @@ fun fd -> return (Some (backend#io_close fd))
401401
| "Lwt_io", "flush" -> take @@ fun fd -> return (Some (backend#io_flush fd))
402+
| "Lwt_io", "with_connection" ->
403+
ignore_lblarg "fd" @@ ignore_lblarg "in_buffer"
404+
@@ ignore_lblarg "out_buffer" @@ take
405+
@@ fun sockaddr ->
406+
take @@ fun f -> return (Some (backend#net_with_connection sockaddr f))
402407
| "Lwt_main", "run" ->
403408
take @@ fun promise -> return (Some (backend#main_run promise))
404409
| "Lwt_preemptive", "detach" ->

bin/lwt_to_direct_style/concurrency_backend.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,25 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
338338
[ "Eio"; "Domain_manager"; "run" ]
339339
[ env "domain_mgr"; thunk ]) );
340340
]
341+
342+
method net_with_connection sockaddr f =
343+
add_comment
344+
"[%s] is of type [Unix.sockaddr] but it should be a \
345+
[Eio.Net.Sockaddr.stream]."
346+
(Ocamlformat_utils.format_expression sockaddr);
347+
mk_apply_simple (switch_ident "run")
348+
[
349+
mk_fun ~arg_name:"sw" (fun sw ->
350+
Exp.apply f
351+
[
352+
( Nolabel,
353+
mk_apply_ident
354+
[ "Eio"; "Net"; "connect" ]
355+
[
356+
(Labelled (mk_loc "sw"), sw);
357+
(Nolabel, env "net");
358+
(Nolabel, sockaddr);
359+
] );
360+
]);
361+
]
341362
end

test/lwt_to_direct_style/to_direct_style.t/run.t

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,11 @@ Make a writable directory tree:
230230
Lwt_mutex.lock (line 136 column 9)
231231
Lwt_mutex.unlock (line 137 column 9)
232232
Lwt_mutex.with_lock (line 138 column 9)
233-
lib/test_lwt_unix.ml: (55 occurrences)
233+
lib/test_lwt_unix.ml: (57 occurrences)
234234
Lwt_io (line 7 column 8)
235235
Lwt.return (line 12 column 3)
236236
Lwt.return (line 46 column 11)
237+
Lwt.return (line 61 column 65)
237238
Lwt.return_unit (line 44 column 43)
238239
Lwt.let* (line 10 column 3)
239240
Lwt.let* (line 11 column 3)
@@ -268,6 +269,7 @@ Make a writable directory tree:
268269
Lwt_io.stdout (line 26 column 33)
269270
Lwt_io.open_file (line 30 column 13)
270271
Lwt_io.open_file (line 34 column 13)
272+
Lwt_io.with_connection (line 61 column 3)
271273
Lwt_preemptive.detach (line 44 column 10)
272274
Lwt_preemptive.detach (line 47 column 3)
273275
Lwt_unix.Timeout (line 14 column 9)
@@ -865,3 +867,11 @@ Make a writable directory tree:
865867
let _f a b = Unix.listen a b
866868
let _f a = Marshal.from_channel a
867869
let _f a b = Marshal.to_channel a b
870+
871+
let _f sockaddr =
872+
Switch.run (fun sw ->
873+
(fun (_in_chan, _out_chan) -> ())
874+
(Eio.Net.connect ~sw env#net
875+
(* TODO: lwt-to-direct-style: [sockaddr] is of type [Unix.sockaddr] but it should be a [Eio.Net.Sockaddr.stream]. *)
876+
(* TODO: lwt-to-direct-style: [env] must be propagated from the main loop *)
877+
sockaddr))

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
@@ -56,3 +56,7 @@ let _f a b = Lwt_unix.listen a b
5656

5757
let _f a = Lwt_io.read_value a
5858
let _f a b = Lwt_io.write_value a b
59+
60+
let _f sockaddr =
61+
Lwt_io.with_connection sockaddr (fun (_in_chan, _out_chan) -> Lwt.return ())
62+

0 commit comments

Comments
 (0)