@@ -12,7 +12,11 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
1212 in
1313 let fiber_ident = eio_std_ident " Fiber"
1414 and promise_ident = eio_std_ident " Promise"
15- and switch_ident = eio_std_ident " Switch" in
15+ and switch_ident = eio_std_ident " Switch"
16+ and std_ident i =
17+ used_eio_std := true ;
18+ [ i ]
19+ in
1620 let add_comment fmt = Format. kasprintf add_comment fmt in
1721 let add_comment_dropped_exp ~label exp =
1822 add_comment " Dropped expression (%s): [%s]." label
@@ -47,6 +51,39 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
4751 in
4852 Exp. send env_exp (mk_loc field)
4953 in
54+ let buf_read_of_flow flow =
55+ mk_apply_ident
56+ [ " Eio" ; " Buf_read" ; " of_flow" ]
57+ [
58+ (Labelled (mk_loc " max_size" ), mk_const_int " 1_000_000" ); (Nolabel , flow);
59+ ]
60+ in
61+ let buf_write_of_flow flow =
62+ add_comment
63+ " Write operations to buffered IO should be moved inside [with_flow]." ;
64+ mk_apply_simple
65+ [ " Eio" ; " Buf_write" ; " with_flow" ]
66+ [
67+ flow;
68+ mk_fun ~arg_name: " outbuf" (fun _outbuf ->
69+ mk_variant_exp " Move_writing_code_here" );
70+ ]
71+ in
72+ let import_socket_stream ~r_or_w fd =
73+ (* Used by [input_io] and [output_io]. *)
74+ Exp. constraint_
75+ (mk_apply_ident
76+ [ " Eio_unix" ; " Net" ; " import_socket_stream" ]
77+ [
78+ get_current_switch_arg () ;
79+ (Labelled (mk_loc " close_unix" ), mk_constr_exp [ " true" ]);
80+ (Nolabel , fd);
81+ ])
82+ (mk_typ_constr
83+ ~params:
84+ [ mk_poly_variant [ (r_or_w, [] ); (" Flow" , [] ); (" Close" , [] ) ] ]
85+ (std_ident " r" ))
86+ in
5087 object
5188 method both ~left ~right =
5289 mk_apply_simple (fiber_ident " pair" ) [ left; right ]
@@ -150,31 +187,51 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
150187
151188 method direct_style_type param = param
152189
153- method of_unix_file_descr ?blocking fd =
154- let blocking_arg =
155- let lbl = mk_loc " blocking" in
156- match blocking with
157- | Some (expr , `Lbl) -> [ (Labelled lbl, expr) ]
158- | Some (expr , `Opt) -> [ (Optional lbl, expr) ]
159- | None -> []
160- in
161- mk_apply_ident
162- [ " Eio_unix" ; " Fd" ; " of_unix" ]
163- ([ get_current_switch_arg () ]
164- @ blocking_arg
165- @ [
166- (Labelled (mk_loc " close_unix" ), mk_constr_exp [ " true" ]);
167- (Nolabel , fd);
168- ])
190+ method of_unix_file_descr ?blocking : _ fd =
191+ (* TODO: We don't use [Eio_unix.Fd.t] because there is no conversion to [Flow.sink]. *)
192+ (* let blocking_arg = *)
193+ (* let lbl = mk_loc "blocking" in *)
194+ (* match blocking with *)
195+ (* | Some (expr, `Lbl) -> [ (Labelled lbl, expr) ] *)
196+ (* | Some (expr, `Opt) -> [ (Optional lbl, expr) ] *)
197+ (* | None -> [] *)
198+ (* in *)
199+ (* mk_apply_ident *)
200+ (* [ "Eio_unix"; "Fd"; "of_unix" ] *)
201+ (* ([ get_current_switch_arg () ] *)
202+ (* @ blocking_arg *)
203+ (* @ [ *)
204+ (* (Labelled (mk_loc "close_unix"), mk_constr_exp [ "true" ]); *)
205+ (* (Nolabel, fd); *)
206+ (* ]) *)
207+ fd
169208
170209 method io_read input buffer buf_offset buf_len =
171210 add_comment " [%s] should be a [Cstruct.t]."
172211 (Ocamlformat_utils. format_expression buffer);
212+ add_comment
213+ " [Eio.Flow.single_read] operates on a [Flow.source] but [%s] is likely \
214+ of type [Eio.Buf_read.t]. Rewrite this code to use [Buf_read] (which \
215+ contains an internal buffer) or change the call to \
216+ [Eio.Buf_read.of_flow] used to create the buffer."
217+ (Ocamlformat_utils. format_expression input);
173218 add_comment_dropped_exp ~label: " buffer offset" buf_offset;
174219 add_comment_dropped_exp ~label: " buffer length" buf_len;
175220 mk_apply_simple [ " Eio" ; " Flow" ; " single_read" ] [ input; buffer ]
176221
177- method fd_close fd = mk_apply_simple [ " Eio_unix" ; " Fd" ] [ fd ]
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+
232+ method fd_close fd =
233+ (* TODO: See [of_unix_file_descr]. mk_apply_simple [ "Eio_unix"; "Fd" ] [ fd ] *)
234+ mk_apply_simple [ " Unix" ; " close" ] [ fd ]
178235
179236 method main_run promise =
180237 let with_binding var_ident x body =
@@ -209,29 +266,56 @@ let eio ~eio_sw_as_fiber_var ~eio_env_as_fiber_var add_comment =
209266 wrap_env_fiber_var env (wrap_sw_fiber_var promise));
210267 ]
211268
212- method input_io_of_fd fd =
213- Exp. constraint_
214- (mk_apply_simple [ " Eio_unix" ; " Net" ; " import_socket_stream" ] [ fd ])
215- (mk_typ_constr
216- ~params:
217- [ mk_poly_variant [ (" R" , [] ); (" Flow" , [] ); (" Close" , [] ) ] ]
218- [ " Std" ; " r" ])
219-
220- method output_io_of_fd fd =
221- add_comment
222- " This creates a closeable [Flow.sink] resource but write operations \
223- are rewritten to calls to [Buf_write].\n \
224- \ You might want to use [Buf_write.with_flow sink (fun \
225- buf_write -> ...)]." ;
226- Exp. constraint_
227- (mk_apply_simple [ " Eio_unix" ; " Net" ; " import_socket_stream" ] [ fd ])
228- (mk_typ_constr
229- ~params:
230- [ mk_poly_variant [ (" W" , [] ); (" Flow" , [] ); (" Close" , [] ) ] ]
231- [ " Std" ; " r" ])
269+ method input_io =
270+ function
271+ | `Of_fd fd -> buf_read_of_flow (import_socket_stream ~r_or_w: " R" fd)
272+ | `Fname fname ->
273+ buf_read_of_flow
274+ @@ mk_apply_ident
275+ [ " Eio" ; " Path" ; " open_in" ]
276+ [
277+ get_current_switch_arg () ;
278+ ( Nolabel ,
279+ mk_apply_simple [ " Eio" ; " Path" ; " /" ] [ env " cwd" ; fname ]
280+ );
281+ ]
282+
283+ method output_io =
284+ function
285+ | `Of_fd fd -> buf_write_of_flow (import_socket_stream ~r_or_w: " W" fd)
286+ | `Fname fname ->
287+ add_comment
288+ " [flags] and [perm] arguments were dropped. The [~create] was \
289+ added by default and might not match the previous flags. Use \
290+ [~append:true] for [O_APPEND]." ;
291+ buf_write_of_flow
292+ @@ mk_apply_ident
293+ [ " Eio" ; " Path" ; " open_out" ]
294+ [
295+ get_current_switch_arg () ;
296+ ( Labelled (mk_loc " create" ),
297+ mk_variant_exp ~arg: (mk_const_int " 0o666" ) " If_missing" );
298+ ( Nolabel ,
299+ mk_apply_simple [ " Eio" ; " Path" ; " /" ] [ env " cwd" ; fname ]
300+ );
301+ ]
302+
303+ method io_read_line chan =
304+ mk_apply_simple [ " Eio" ; " Buf_read" ; " line" ] [ chan ]
305+
306+ (* This is of type [Optint.Int63.t] instead of [int] with Lwt. *)
307+ method io_length fd = mk_apply_simple [ " Eio" ; " File" ; " size" ] [ fd ]
232308
233309 method io_write_str chan str =
234310 mk_apply_simple [ " Eio" ; " Buf_write" ; " string" ] [ chan; str ]
235311
312+ method io_close fd = mk_apply_simple [ " Eio" ; " Resource" ; " close" ] [ fd ]
236313 method type_out_channel = mk_typ_constr [ " Eio" ; " Buf_write" ; " t" ]
314+
315+ method path_stat ~follow path =
316+ mk_apply_ident [ " Eio" ; " Path" ; " stat" ]
317+ [
318+ (Labelled (mk_loc " follow" ), mk_constr_of_bool follow);
319+ (Nolabel , mk_apply_simple [ " Eio" ; " Path" ; " /" ] [ env " cwd" ; path ]);
320+ ]
237321 end
0 commit comments