-
Notifications
You must be signed in to change notification settings - Fork 0
/
it_Lwt_IO.ml
101 lines (76 loc) · 2.28 KB
/
it_Lwt_IO.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
open It_Types
;
(* Lwt IO *)
module It_Lwt_IO
:
sig
type m +'a = Lwt.t 'a;
value return : 'a -> m 'a;
value bind : ('a -> m 'b) -> m 'a -> m 'b;
value bind_rev : m 'a -> ('a -> m 'b) -> m 'b;
value catch : (unit -> m 'a) -> (exn -> m 'a) -> m 'a;
value error : exn -> m 'a;
type output_channel = Lwt_io.output_channel;
value stdout : output_channel;
value write : output_channel -> string -> m unit;
value flush : output_channel -> m unit;
type input_channel = Lwt_io.input_channel;
value open_in : string -> m input_channel;
value close_in : input_channel -> m unit; (* Lwt_io.close inch *)
value read_into : input_channel -> string -> int -> int -> m int;
(* read_into ic buffer offset length *)
value runIO : m 'a -> res 'a;
value with_file_in_bin : string -> (input_channel -> m 'a) -> m 'a;
value with_file_out_bin : string -> (output_channel -> m 'a) -> m 'a;
end
=
struct
type m +'a = Lwt.t 'a;
value return = Lwt.return;
value bind f m = Lwt.bind m f;
value bind_rev = Lwt.bind;
value catch = Lwt.catch;
value wrap_exn place = fun e ->
Lwt.fail (EIO (e, place))
;
value wrap1 place f = fun a ->
catch (fun () -> f a)
(wrap_exn place)
;
value wrap2 place f = fun a b ->
catch (fun () -> f a b)
(wrap_exn place)
;
value wrap4 place f = fun a b c d ->
catch (fun () -> f a b c d)
(wrap_exn place)
;
value read_into = wrap4 "read_into" Lwt_io.read_into;
value error = Lwt.fail;
type output_channel = Lwt_io.output_channel;
value stdout = Lwt_io.stdout;
value write = wrap2 "write" Lwt_io.write;
type input_channel = Lwt_io.input_channel;
value open_in = wrap1 "open_in" (
fun fn ->
Lwt_io.open_file
~mode:Lwt_io.input
~flags:[Unix.O_RDONLY]
fn
)
;
value close_in = wrap1 "close_in" Lwt_io.close;
value runIO x : res 'a =
try `Ok (Lwt_main.run x)
with [e -> `Error e]
;
value with_file_in_bin filename func =
Lwt_io.with_file ~mode:Lwt_io.input filename func
;
value with_file_out_bin filename func =
Lwt_io.with_file ~mode:Lwt_io.output filename func
;
value flush = Lwt_io.flush
;
end
;