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

Use bigstringaf instead of Lwt_bytes to remove unix dependencies #107

Merged
merged 3 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
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
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