Skip to content

Commit 21826bb

Browse files
committed
Document Io_utils functions
1 parent d7d4631 commit 21826bb

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/utils/odoc_utils.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ module Forest = Tree.Forest
8080
module Json = Json
8181

8282
module Io_utils = struct
83+
(** [with_open_*] are resource safe wrappers around opening and closing
84+
channels. They are equivalent to the same functions in OCaml 4.14's
85+
[In_channel] and [Out_channel]. *)
86+
8387
let _with_resource res ~close f =
8488
Fun.protect ~finally:(fun () -> close res) (fun () -> f res)
8589

@@ -89,6 +93,7 @@ module Io_utils = struct
8993
let with_open_in_bin fname f =
9094
_with_resource (open_in_bin fname) ~close:close_in_noerr f
9195

96+
(** Read a file line-by-line by folding [f]. *)
9297
let fold_lines fname f acc =
9398
_with_resource (open_in fname) ~close:close_in_noerr (fun ic ->
9499
let rec loop acc =
@@ -98,6 +103,7 @@ module Io_utils = struct
98103
in
99104
loop acc)
100105

106+
(** Read a file as a list of lines. *)
101107
let read_lines fname =
102108
List.rev (fold_lines fname (fun line acc -> line :: acc) [])
103109

@@ -107,16 +113,15 @@ module Io_utils = struct
107113
let with_open_out_bin fname f =
108114
_with_resource (open_out_bin fname) ~close:close_out_noerr f
109115

116+
(** Like [with_open_out] but operate on a [Format] buffer. *)
110117
let with_formatter_out fname f =
111118
with_open_out fname (fun oc -> f (Format.formatter_of_out_channel oc))
112119

120+
(** Shortcuts for composing [with_open_*] functions and [Marshal]. *)
113121
let marshal fname v =
114-
_with_resource (open_out_bin fname) ~close:close_out_noerr (fun oc ->
115-
Marshal.to_channel oc v [])
122+
with_open_out_bin fname (fun oc -> Marshal.to_channel oc v [])
116123

117-
let unmarshal fname =
118-
_with_resource (open_in_bin fname) ~close:close_in_noerr
119-
Marshal.from_channel
124+
let unmarshal fname = with_open_in_bin fname Marshal.from_channel
120125
end
121126

122127
include Astring

0 commit comments

Comments
 (0)