Skip to content

Commit

Permalink
Add Async_cstruct.Pipe to map pipes of Cstruct buffers to strings…
Browse files Browse the repository at this point in the history
… or `Bigsubstring`
  • Loading branch information
avsm committed Feb 25, 2013
1 parent 501c952 commit 2ff20d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES
@@ -1,3 +1,6 @@
0.7.1 (trunk):
* Add `Async_cstruct.Pipe` to map pipes of `Cstruct` buffers to strings or `Bigsubstring`.

0.7.0 (25-Feb-2013):

* Add zero-copy conversion functions to/from the Core `Bigsubstring`.
Expand Down
14 changes: 14 additions & 0 deletions async/async_cstruct.ml
Expand Up @@ -35,3 +35,17 @@ let read rd t =
let schedule_write wr t =
let open Cstruct in
Writer.schedule_bigstring ~pos:t.off ~len:t.len wr t.buffer

module Pipe = struct
let map_string rd wr =
let rd = Pipe.map rd ~f:Cstruct.to_string in
let rd',wr' = Pipe.create () in
don't_wait_for (Pipe.transfer rd' wr ~f:Cstruct.of_string);
rd,wr'

let map_bigsubstring rd wr =
let rd = Pipe.map rd ~f:to_bigsubstring in
let rd',wr' = Pipe.create () in
don't_wait_for (Pipe.transfer rd' wr ~f:of_bigsubstring);
rd,wr'
end
12 changes: 12 additions & 0 deletions async/async_cstruct.mli
Expand Up @@ -22,3 +22,15 @@ val of_bigsubstring : Bigsubstring.t -> Cstruct.t

val read: Reader.t -> Cstruct.t -> int Reader.Read_result.t Deferred.t
val schedule_write: Writer.t -> Cstruct.t -> unit

module Pipe : sig
val map_string :
Cstruct.t Pipe.Reader.t ->
Cstruct.t Pipe.Writer.t ->
(string Pipe.Reader.t * string Pipe.Writer.t)

val map_bigsubstring :
Cstruct.t Pipe.Reader.t ->
Cstruct.t Pipe.Writer.t ->
(Bigsubstring.t Pipe.Reader.t * Bigsubstring.t Pipe.Writer.t)
end

0 comments on commit 2ff20d0

Please sign in to comment.