diff --git a/async/faraday_async.ml b/async/faraday_async.ml index 6397334..530b3db 100644 --- a/async/faraday_async.ml +++ b/async/faraday_async.ml @@ -53,7 +53,7 @@ let writev_of_fd fd = in fun iovecs -> let iovecs = Array.of_list_map iovecs ~f:(fun iovec -> - let { Faraday.buffer; off = pos; len } = iovec in + let { Faraday.IOVec.buffer; off = pos; len } = iovec in Unix.IOVec.of_bigstring ~pos ~len buffer) in if Fd.supports_nonblock fd then diff --git a/async/faraday_async.mli b/async/faraday_async.mli index 1be9d30..742f419 100644 --- a/async/faraday_async.mli +++ b/async/faraday_async.mli @@ -7,9 +7,9 @@ open Faraday val serialize : Faraday.t -> yield : (t -> unit Deferred.t) - -> writev : (bigstring iovec list -> [ `Ok of int | `Closed ] Deferred.t) + -> writev : (IOVec.t list -> [ `Ok of int | `Closed ] Deferred.t) -> unit Deferred.t val writev_of_fd : Fd.t - -> bigstring iovec list -> [ `Ok of int | `Closed ] Deferred.t + -> IOVec.t list -> [ `Ok of int | `Closed ] Deferred.t diff --git a/lib/faraday.ml b/lib/faraday.ml index f25db14..d850d7f 100644 --- a/lib/faraday.ml +++ b/lib/faraday.ml @@ -34,10 +34,30 @@ type bigstring = Bigstringaf.t -type 'a iovec = - { buffer : 'a - ; off : int - ; len : int } +module IOVec = struct + type t = + { buffer : bigstring + ; off : int + ; len : int } + + let create buffer ~off ~len = + { buffer; off; len } + + let length t = + t.len + + let shift { buffer; off; len } n = + assert (n < len); + { buffer; off = off + n; len = len - n } + + let lengthv ts = + let rec loop ts acc = + match ts with + | [] -> acc + | iovec::ts -> loop ts (length iovec + acc) + in + loop ts 0 +end exception Dequeue_empty @@ -120,34 +140,14 @@ end = struct !result end -module IOVec = struct - let create buffer ~off ~len = - { buffer; off; len } - - let length t = - t.len - - let shift { buffer; off; len } n = - assert (n < len); - { buffer; off = off + n; len = len - n } - - let lengthv ts = - let rec loop ts acc = - match ts with - | [] -> acc - | iovec::ts -> loop ts (length iovec + acc) - in - loop ts 0 -end - module Buffers = Deque(struct - type t = bigstring iovec + type t = IOVec.t let sentinel = let deadbeef = "\222\173\190\239" in let len = String.length deadbeef in let buffer = Bigstringaf.create len in String.iteri (Bigstringaf.unsafe_set buffer) deadbeef; - { buffer; off = 0; len } + { IOVec.buffer; off = 0; len } end) module Flushes = Deque(struct type t = int * (unit -> unit) @@ -167,7 +167,7 @@ type t = } type operation = [ - | `Writev of bigstring iovec list + | `Writev of IOVec.t list | `Yield | `Close ] @@ -368,7 +368,7 @@ let yield t = let rec shift_buffers t written = try - let { len; _ } as iovec = Buffers.dequeue_exn t.scheduled in + let { IOVec.len; _ } as iovec = Buffers.dequeue_exn t.scheduled in if len <= written then begin shift_buffers t (written - len) end else @@ -443,7 +443,7 @@ let serialize_to_string t = let bytes = Bytes.create len in let pos = ref 0 in List.iter (function - | { buffer; off; len } -> + | { IOVec.buffer; off; len } -> Bigstringaf.unsafe_blit_to_bytes buffer ~src_off:off bytes ~dst_off:!pos ~len; pos := !pos + len) iovecs; @@ -461,7 +461,7 @@ let serialize_to_bigstring t = let bs = Bigstringaf.create len in let pos = ref 0 in List.iter (function - | { buffer; off; len } -> + | { IOVec.buffer; off; len } -> Bigstringaf.unsafe_blit buffer ~src_off:off bs ~dst_off:!pos ~len; pos := !pos + len) iovecs; diff --git a/lib/faraday.mli b/lib/faraday.mli index 11412bd..d798d26 100644 --- a/lib/faraday.mli +++ b/lib/faraday.mli @@ -247,15 +247,19 @@ val drain : t -> int consider the Async and Lwt support that this library includes before attempting to use this these operations directly. *) -type 'a iovec = - { buffer : 'a - ; off : int - ; len : int } +module IOVec : sig (** A view into {!iovec.buffer} starting at {!iovec.off} and with length {!iovec.len}. *) + type t = + { buffer : bigstring + ; off : int + ; len : int } +end + + type operation = [ - | `Writev of bigstring iovec list + | `Writev of IOVec.t list | `Yield | `Close ] (** The type of operations that the serialier may wish to perform. @@ -282,7 +286,7 @@ val operation : t -> operation function. See the documentation for the {!type:operation} type for details on how callers should handle these operations. *) -val serialize : t -> (bigstring iovec list -> [`Ok of int | `Closed]) -> [`Yield | `Close] +val serialize : t -> (IOVec.t list -> [`Ok of int | `Closed]) -> [`Yield | `Close] (** [serialize t writev] sufaces the next operation of [t] to the caller, handling a [`Writev] operation with [writev] function and performing an additional bookkeeping on the caller's behalf. In the event that [writev]