Shells in OCaml
at main 67 lines 2.1 kB view raw
1(* Some random utils for debugging *) 2open Eio.Std 3 4let traced_sink tag 5 (Eio.Resource.T (t, handler) : Eio_unix.sink_ty Eio.Flow.sink) : 6 Eio_unix.sink_ty r = 7 let module Sink = (val Eio.Resource.get handler Eio.Flow.Pi.Sink) in 8 let close = Eio.Resource.get handler Eio.Resource.Close in 9 let buf = Cstruct.create 4096 in 10 let copy () ~src = 11 try 12 while true do 13 match Eio.Flow.single_read src buf with 14 | i -> 15 Eio.traceln ">>>>> %s Single read: %s" tag (Cstruct.to_string buf); 16 let bufs = [ Cstruct.sub buf 0 i ] in 17 Sink.copy ~src:(Eio.Flow.cstruct_source bufs) t 18 done 19 with End_of_file -> Eio.traceln ">>>>>> EOF" 20 in 21 let single_write () x = 22 Eio.traceln ">>>>> single write: %s" (Cstruct.concat x |> Cstruct.to_string); 23 Sink.single_write t x 24 in 25 let module T = struct 26 type t = unit 27 28 let single_write = single_write 29 let copy = copy 30 end in 31 let t = 32 Eio.Resource.handler 33 [ 34 H (Eio.Flow.Pi.Sink, (module T)); 35 H (Eio.Resource.Close, fun () -> close t); 36 ] 37 in 38 Eio.Resource.T ((), t) 39 40let traced_sink_flow tag 41 (Eio.Resource.T (t, handler) : Eio.Flow.sink_ty Eio.Flow.sink) : 42 Eio.Flow.sink_ty r = 43 let module Sink = (val Eio.Resource.get handler Eio.Flow.Pi.Sink) in 44 let buf = Cstruct.create 4096 in 45 let copy () ~src = 46 try 47 while true do 48 match Eio.Flow.single_read src buf with 49 | i -> 50 Eio.traceln ">>>>> %s Single read: %s" tag (Cstruct.to_string buf); 51 let bufs = [ Cstruct.sub buf 0 i ] in 52 Sink.copy ~src:(Eio.Flow.cstruct_source bufs) t 53 done 54 with End_of_file -> Eio.traceln ">>>>>> EOF" 55 in 56 let single_write () x = 57 Eio.traceln ">>>>> single write: %s" (Cstruct.concat x |> Cstruct.to_string); 58 Sink.single_write t x 59 in 60 let module T = struct 61 type t = unit 62 63 let single_write = single_write 64 let copy = copy 65 end in 66 let t = Eio.Resource.handler [ H (Eio.Flow.Pi.Sink, (module T)) ] in 67 Eio.Resource.T ((), t)