From 235f3a22426b6d9adecd2669300f6ac4bf8e9c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Fri, 10 Feb 2023 08:30:45 +0100 Subject: [PATCH 1/3] Start deprecating Cstruct.copy It does the same as to_string but without defaults, and the name suggests it returns another cstruct but it doesn't. Instead, Cstruct.sub_copy is a version that returns a cstruct copy. --- lib/cstruct.ml | 12 ++++++------ lib/cstruct.mli | 4 +++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/cstruct.ml b/lib/cstruct.ml index fdc6cd4..5c4b871 100644 --- a/lib/cstruct.ml +++ b/lib/cstruct.ml @@ -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 @@ -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 @@ -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 = diff --git a/lib/cstruct.mli b/lib/cstruct.mli index 575d328..c6eb244 100644 --- a/lib/cstruct.mli +++ b/lib/cstruct.mli @@ -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]. *) From bfdb629ec24b101f1571abd3c6926020f2d28346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Fri, 10 Feb 2023 09:36:09 +0100 Subject: [PATCH 2/3] ppx: use to_string instead of copy --- ppx/ppx_cstruct.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ppx/ppx_cstruct.ml b/ppx/ppx_cstruct.ml index 69bf7c0..3525db8 100644 --- a/ppx/ppx_cstruct.ml +++ b/ppx/ppx_cstruct.ml @@ -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 -> From 55a0abe4b86af3bbecf55f236afd3468d26a01a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reynir=20Bj=C3=B6rnsson?= Date: Fri, 10 Feb 2023 11:54:46 +0100 Subject: [PATCH 3/3] Export Cstruct_cap.copy with a deprecation warning --- lib/cstruct_cap.mli | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/cstruct_cap.mli b/lib/cstruct_cap.mli index d62edce..cc73438 100644 --- a/lib/cstruct_cap.mli +++ b/lib/cstruct_cap.mli @@ -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