Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Buf_write.of_flow #796

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions lib_eio/buf_write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -515,21 +515,16 @@ let copy t flow =
try aux ()
with End_of_file -> ()

let with_flow ?(initial_size=0x1000) flow fn =
Switch.run ~name:"Buf_write.with_flow" @@ fun sw ->
let of_flow ~sw ?(initial_size=0x1000) flow =
let t = create ~sw initial_size in
Switch.on_release sw (fun () -> close t);
Fiber.fork ~sw (fun () -> copy t flow);
match fn t with
| x ->
close t;
x
| exception ex ->
close t;
(* Raising the exception will cancel the writer thread, so do a flush first.
We don't want to flush if cancelled, but in that case the switch will
end the writer thread itself (and [flush] will raise). *)
flush t;
raise ex
t

let with_flow ?initial_size flow fn =
Switch.run ~name:"Buf_write.with_flow" @@ fun sw ->
let t = of_flow ~sw ?initial_size flow in
fn t

let rec serialize t writev =
match await_batch t with
Expand Down