Skip to content

Commit 2034628

Browse files
authored
Merge pull request #308 from reynir/copy
Deprecate copy
2 parents d35f6a1 + 55a0abe commit 2034628

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

lib/cstruct.ml

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let err_of_bigarray t = err "Cstruct.of_bigarray off=%d len=%d" t
5353
let err_sub t = err "Cstruct.sub: %a off=%d len=%d" pp_t t
5454
let err_shift t = err "Cstruct.shift %a %d" pp_t t
5555
let err_shiftv n = err "Cstruct.shiftv short by %d" n
56-
let err_copy t = err "Cstruct.copy %a off=%d len=%d" pp_t t
56+
let err_copy_to_string caller t = err "Cstruct.%s %a off=%d len=%d" caller pp_t t
5757
let err_to_hex_string t = err "Cstruct.to_hex_string %a off=%d len=%d" pp_t t
5858
let err_blit_src src dst =
5959
err "Cstruct.blit src=%a dst=%a src-off=%d len=%d" pp_t src pp_t dst
@@ -200,15 +200,17 @@ external unsafe_compare_bigstring : buffer -> int -> buffer -> int -> int -> int
200200

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

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

212+
let copy = copy_to_string "copy"
213+
212214
let blit src srcoff dst dstoff len =
213215
if len < 0 || srcoff < 0 || src.len - srcoff < len then
214216
err_blit_src src dst srcoff len
@@ -398,9 +400,7 @@ let fillv ~src ~dst =
398400

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

405405
let to_hex_string ?(off=0) ?len:sz t : string =
406406
let[@inline] nibble_to_char (i:int) : char =

lib/cstruct.mli

+3-1
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,10 @@ val shift: t -> int -> t
292292
@raise Invalid_argument if the offset exceeds cstruct length. *)
293293

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

lib/cstruct_cap.mli

+4
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ val split : ?start:int -> 'a t -> int -> 'a t * 'a t
177177
@raise Invalid_argument if [start] exceeds the length of [t],
178178
or if there is a bounds violation of [t] via [len + start]. *)
179179

180+
val copy : 'a t -> int -> int -> string
181+
[@@ocaml.alert deprecated "this is just like [to_string] without defaults, were you looking for [sub_copy]?"]
182+
(** [copy cstr off len] is the same as [Cstruct.to_string cstr ~off ~len]. *)
183+
180184
(** {2 Construction from existing t} *)
181185

182186
val append : 'a rd t -> 'b rd t -> rdwr t

ppx/ppx_cstruct.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ let op_expr loc s = function
298298
| Op_set f -> set_expr loc s f
299299
| Op_copy f ->
300300
let len = width_of_field f in
301-
[%expr fun src -> Cstruct.copy src [%e Ast.eint ~loc f.off] [%e Ast.eint ~loc len] ]
301+
[%expr fun src -> Cstruct.to_string src ~off:[%e Ast.eint ~loc f.off] ~len:[%e Ast.eint ~loc len] ]
302302
| Op_blit f ->
303303
let len = width_of_field f in
304304
[%expr fun src srcoff dst ->

0 commit comments

Comments
 (0)