Skip to content

Commit

Permalink
Merge pull request #308 from reynir/copy
Browse files Browse the repository at this point in the history
Deprecate copy
  • Loading branch information
dinosaure authored Feb 20, 2023
2 parents d35f6a1 + 55a0abe commit 2034628
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let err_of_bigarray t = err "Cstruct.of_bigarray off=%d len=%d" t
let err_sub t = err "Cstruct.sub: %a off=%d len=%d" pp_t t
let err_shift t = err "Cstruct.shift %a %d" pp_t t
let err_shiftv n = err "Cstruct.shiftv short by %d" n
let err_copy t = err "Cstruct.copy %a off=%d len=%d" pp_t t
let err_copy_to_string caller t = err "Cstruct.%s %a off=%d len=%d" caller pp_t t
let err_to_hex_string t = err "Cstruct.to_hex_string %a off=%d len=%d" pp_t t
let err_blit_src src dst =
err "Cstruct.blit src=%a dst=%a src-off=%d len=%d" pp_t src pp_t dst
Expand Down Expand Up @@ -200,15 +200,17 @@ external unsafe_compare_bigstring : buffer -> int -> buffer -> int -> int -> int

external unsafe_fill_bigstring : buffer -> int -> int -> int -> unit = "caml_fill_bigstring" [@@noalloc]

let copy src srcoff len =
let copy_to_string caller src srcoff len =
if len < 0 || srcoff < 0 || src.len - srcoff < len then
err_copy src srcoff len
err_copy_to_string caller src srcoff len
else
let b = Bytes.create len in
unsafe_blit_bigstring_to_bytes src.buffer (src.off+srcoff) b 0 len;
(* The following call is safe, since b is not visible elsewhere. *)
Bytes.unsafe_to_string b

let copy = copy_to_string "copy"

let blit src srcoff dst dstoff len =
if len < 0 || srcoff < 0 || src.len - srcoff < len then
err_blit_src src dst srcoff len
Expand Down Expand Up @@ -398,9 +400,7 @@ let fillv ~src ~dst =

let to_string ?(off=0) ?len:sz t =
let len = match sz with None -> length t - off | Some l -> l in
(* The following call is safe, since this is the only reference to the
freshly-created value built by [to_bytes t]. *)
copy t off len
copy_to_string "to_string" t off len

let to_hex_string ?(off=0) ?len:sz t : string =
let[@inline] nibble_to_char (i:int) : char =
Expand Down
4 changes: 3 additions & 1 deletion lib/cstruct.mli
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,10 @@ val shift: t -> int -> t
@raise Invalid_argument if the offset exceeds cstruct length. *)

val copy: t -> int -> int -> string
[@@ocaml.alert deprecated "this is just like [to_string] without defaults, were you looking for [sub_copy]?"]
(** [copy cstr off len] is the string representation of the segment of
[t] starting at [off] of size [len].
[t] starting at [off] of size [len]. It is equivalent to
[Cstruct.to_string cstr ~off ~len].
@raise Invalid_argument if [off] and [len] do not designate a
valid segment of [t]. *)

Expand Down
4 changes: 4 additions & 0 deletions lib/cstruct_cap.mli
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ val split : ?start:int -> 'a t -> int -> 'a t * 'a t
@raise Invalid_argument if [start] exceeds the length of [t],
or if there is a bounds violation of [t] via [len + start]. *)

val copy : 'a t -> int -> int -> string
[@@ocaml.alert deprecated "this is just like [to_string] without defaults, were you looking for [sub_copy]?"]
(** [copy cstr off len] is the same as [Cstruct.to_string cstr ~off ~len]. *)

(** {2 Construction from existing t} *)

val append : 'a rd t -> 'b rd t -> rdwr t
Expand Down
2 changes: 1 addition & 1 deletion ppx/ppx_cstruct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ let op_expr loc s = function
| Op_set f -> set_expr loc s f
| Op_copy f ->
let len = width_of_field f in
[%expr fun src -> Cstruct.copy src [%e Ast.eint ~loc f.off] [%e Ast.eint ~loc len] ]
[%expr fun src -> Cstruct.to_string src ~off:[%e Ast.eint ~loc f.off] ~len:[%e Ast.eint ~loc len] ]
| Op_blit f ->
let len = width_of_field f in
[%expr fun src srcoff dst ->
Expand Down

0 comments on commit 2034628

Please sign in to comment.