Skip to content

Commit

Permalink
Use bigstringaf instead of Lwt_bytes to remove unix dependencies (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinosaure committed Jul 5, 2021
1 parent 8b33007 commit dd2b8dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/pure/body.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@



type bigstring = Lwt_bytes.t
type bigstring = Bigstringaf.t

(* The stream representation can be replaced by a record with mutable fields for
0-allocation streaming. *)
Expand Down Expand Up @@ -110,10 +110,10 @@ let body : body_cell -> string Lwt.t = fun body_cell ->
let promise, resolver = Lwt.wait () in

let length = ref 0 in
let buffer = ref (Lwt_bytes.create 4096) in
let buffer = ref (Bigstringaf.create 4096) in

let close () =
let result = Lwt_bytes.to_string (Lwt_bytes.proxy !buffer 0 !length) in
let result = Bigstringaf.to_string (Bigstringaf.sub !buffer ~off:0 ~len:!length) in

if !length = 0 then
body_cell := `Empty
Expand All @@ -134,28 +134,28 @@ let body : body_cell -> string Lwt.t = fun body_cell ->
and bigstring chunk offset chunk_length =
let new_length = !length + chunk_length in

if new_length > Lwt_bytes.length !buffer then begin
let new_buffer = Lwt_bytes.create (new_length * 2) in
Lwt_bytes.blit !buffer 0 new_buffer 0 !length;
if new_length > Bigstringaf.length !buffer then begin
let new_buffer = Bigstringaf.create (new_length * 2) in
Bigstringaf.blit !buffer ~src_off:0 new_buffer ~dst_off:0 ~len:!length;
buffer := new_buffer
end;

Lwt_bytes.blit chunk offset !buffer !length chunk_length;
Bigstringaf.blit chunk ~src_off:offset !buffer ~dst_off:!length ~len:chunk_length;
length := new_length;

loop ()

and string chunk offset chunk_length =
let new_length = !length + chunk_length in

if new_length > Lwt_bytes.length !buffer then begin
let new_buffer = Lwt_bytes.create (new_length * 2) in
Lwt_bytes.blit !buffer 0 new_buffer 0 !length;
if new_length > Bigstringaf.length !buffer then begin
let new_buffer = Bigstringaf.create (new_length * 2) in
Bigstringaf.blit !buffer ~src_off:0 new_buffer ~dst_off:0 ~len:!length;
buffer := new_buffer
end;

Lwt_bytes.blit_from_bytes
(Bytes.unsafe_of_string chunk) offset !buffer !length chunk_length;
Bigstringaf.blit_from_bytes
(Bytes.unsafe_of_string chunk) ~src_off:offset !buffer ~dst_off:!length ~len:chunk_length;
length := new_length;

loop ()
Expand Down Expand Up @@ -201,7 +201,7 @@ let read : body_cell -> string option Lwt.t = fun body_cell ->

and bigstring chunk offset length =
Lwt.wakeup_later resolver
(Some (Lwt_bytes.to_string (Lwt_bytes.proxy chunk offset length)))
(Some (Bigstringaf.to_string (Bigstringaf.sub chunk ~off:offset ~len:length)))

and string chunk offset length =
let chunk =
Expand Down
1 change: 1 addition & 0 deletions src/pure/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(name dream__pure)
(libraries
base64
bigstringaf
dream.cipher
multipart_form
hmap
Expand Down

0 comments on commit dd2b8dc

Please sign in to comment.