| 1 |
open Common |
| 2 |
|
| 3 |
module T = struct |
| 4 |
type 'a t = 'a [@@deriving compare, sexp_of] |
| 5 |
|
| 6 |
let bin_shape_t t = |
| 7 |
Shape.(basetype (Uuid.of_string "85a2557e-490a-11e6-98ac-4b8953d525fe") [ t ]) |
| 8 |
;; |
| 9 |
|
| 10 |
let bin_size_t bin_size_a a = Utils.size_header_length + bin_size_a a |
| 11 |
|
| 12 |
let bin_write_t bin_write_a buf ~pos a = |
| 13 |
let start_a = pos + Utils.size_header_length in |
| 14 |
let end_a = bin_write_a buf ~pos:start_a a in |
| 15 |
let size = end_a - start_a in |
| 16 |
let written = Utils.bin_write_size_header buf ~pos size in |
| 17 |
assert (written = start_a); |
| 18 |
end_a |
| 19 |
;; |
| 20 |
|
| 21 |
let bin_read_t bin_read_a buf ~pos_ref = |
| 22 |
let expected_size = Utils.bin_read_size_header buf ~pos_ref in |
| 23 |
let start_a = !pos_ref in |
| 24 |
let a = bin_read_a buf ~pos_ref in |
| 25 |
let end_a = !pos_ref in |
| 26 |
if end_a - start_a <> expected_size |
| 27 |
then |
| 28 |
failwith |
| 29 |
(Printf.sprintf |
| 30 |
"Bin_prot.Blob.bin_read_t: size (%d) <> expected (%d)" |
| 31 |
(end_a - start_a) |
| 32 |
expected_size); |
| 33 |
a |
| 34 |
;; |
| 35 |
|
| 36 |
let __bin_read_t__ _ _ ~pos_ref = raise_variant_wrong_type "Bin_prot.Blob.t" !pos_ref |
| 37 |
end |
| 38 |
|
| 39 |
type 'a id = 'a |
| 40 |
|
| 41 |
include T |
| 42 |
|
| 43 |
include Utils.Make_binable1_without_uuid [@alert "-legacy"] (struct |
| 44 |
module Binable = T |
| 45 |
|
| 46 |
type 'a t = 'a T.t |
| 47 |
|
| 48 |
let of_binable t = t |
| 49 |
let to_binable t = t |
| 50 |
end) |
| 51 |
|
| 52 |
module Opaque = struct |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
let bin_shape_t = |
| 58 |
Shape.(basetype (Uuid.of_string "85a1f76e-490a-11e6-86a9-5bef585f2602") []) |
| 59 |
;; |
| 60 |
|
| 61 |
module Bigstring = struct |
| 62 |
|
| 63 |
|
| 64 |
module T = struct |
| 65 |
type t = buf |
| 66 |
|
| 67 |
let bin_shape_t = bin_shape_t |
| 68 |
let bin_size_t t = Utils.size_header_length + buf_len t |
| 69 |
|
| 70 |
let bin_write_t buf ~pos t = |
| 71 |
let size = buf_len t in |
| 72 |
let pos = Utils.bin_write_size_header buf ~pos size in |
| 73 |
blit_buf ~src:t ~src_pos:0 ~dst:buf ~dst_pos:pos size; |
| 74 |
pos + size |
| 75 |
;; |
| 76 |
|
| 77 |
let bin_read_t buf ~pos_ref = |
| 78 |
let size = Utils.bin_read_size_header buf ~pos_ref in |
| 79 |
let t = create_buf size in |
| 80 |
blit_buf ~src:buf ~src_pos:!pos_ref ~dst:t ~dst_pos:0 size; |
| 81 |
pos_ref := !pos_ref + size; |
| 82 |
t |
| 83 |
;; |
| 84 |
|
| 85 |
let __bin_read_t__ _ ~pos_ref = |
| 86 |
raise_variant_wrong_type "Bin_prot.Blob.Opaque.t" !pos_ref |
| 87 |
;; |
| 88 |
end |
| 89 |
|
| 90 |
include T |
| 91 |
include Utils.Of_minimal (T) |
| 92 |
|
| 93 |
let to_opaque blob bin_writer = Utils.bin_dump bin_writer blob |
| 94 |
let of_opaque_exn (t : t) bin_reader = bin_reader.Type_class.read t ~pos_ref:(ref 0) |
| 95 |
|
| 96 |
|
| 97 |
let compare = (Stdlib.compare : buf -> buf -> int) |
| 98 |
|
| 99 |
let sexp_of_t t = |
| 100 |
Ppx_sexp_conv_lib.Sexp.Atom (of_opaque_exn t Type_class.bin_reader_string) |
| 101 |
;; |
| 102 |
end |
| 103 |
|
| 104 |
module String = struct |
| 105 |
module T = struct |
| 106 |
type t = string |
| 107 |
|
| 108 |
let bin_shape_t = bin_shape_t |
| 109 |
let bin_size_t t = Utils.size_header_length + String.length t |
| 110 |
|
| 111 |
let bin_write_t buf ~pos t = |
| 112 |
let size = String.length t in |
| 113 |
let pos = Utils.bin_write_size_header buf ~pos size in |
| 114 |
Common.blit_string_buf t ~src_pos:0 buf ~dst_pos:pos ~len:size; |
| 115 |
pos + size |
| 116 |
;; |
| 117 |
|
| 118 |
let string_of_bigstring buf ~pos ~len = |
| 119 |
let str = Bytes.create len in |
| 120 |
blit_buf_bytes ~src_pos:pos buf ~dst_pos:0 str ~len; |
| 121 |
Bytes.unsafe_to_string str |
| 122 |
;; |
| 123 |
|
| 124 |
let bin_read_t buf ~pos_ref = |
| 125 |
let len = Utils.bin_read_size_header buf ~pos_ref in |
| 126 |
let t = string_of_bigstring buf ~pos:!pos_ref ~len in |
| 127 |
pos_ref := !pos_ref + len; |
| 128 |
t |
| 129 |
;; |
| 130 |
|
| 131 |
let __bin_read_t__ _ ~pos_ref = |
| 132 |
raise_variant_wrong_type "Bin_prot.Blob.Opaque.t" !pos_ref |
| 133 |
;; |
| 134 |
end |
| 135 |
|
| 136 |
include T |
| 137 |
include Utils.Of_minimal (T) |
| 138 |
|
| 139 |
let length t = String.length t |
| 140 |
|
| 141 |
let to_opaque ~buf v bin_writer_v : t = |
| 142 |
let pos = 0 in |
| 143 |
let len = bin_writer_v.Type_class.write buf ~pos v in |
| 144 |
string_of_bigstring buf ~pos ~len |
| 145 |
;; |
| 146 |
|
| 147 |
let of_opaque_exn ~buf (t : t) bin_reader_v = |
| 148 |
let len = String.length t in |
| 149 |
Common.blit_string_buf t buf ~len; |
| 150 |
let pos_ref = ref 0 in |
| 151 |
let res = bin_reader_v.Type_class.read buf ~pos_ref in |
| 152 |
if !pos_ref <> len |
| 153 |
then ( |
| 154 |
let error = |
| 155 |
Printf.sprintf |
| 156 |
"Opaque blob has %d bytes but [of_opaque_exn] read %d" |
| 157 |
len |
| 158 |
!pos_ref |
| 159 |
in |
| 160 |
failwith error) |
| 161 |
else res |
| 162 |
;; |
| 163 |
|
| 164 |
|
| 165 |
let compare = (Stdlib.compare : string -> string -> int) |
| 166 |
let sexp_of_t = Ppx_sexp_conv_lib.Conv.sexp_of_string |
| 167 |
end |
| 168 |
end |
| 169 |
|
| 170 |
module Ignored = struct |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
type t = int |
| 175 |
|
| 176 |
let bin_size_t size = Utils.size_header_length + size |
| 177 |
|
| 178 |
let bin_read_t buf ~pos_ref = |
| 179 |
let size = Utils.bin_read_size_header buf ~pos_ref in |
| 180 |
pos_ref := !pos_ref + size; |
| 181 |
size |
| 182 |
;; |
| 183 |
|
| 184 |
let __bin_read_t__ _ ~pos_ref = |
| 185 |
raise_variant_wrong_type "Bin_prot.Blob.Ignored.t" !pos_ref |
| 186 |
;; |
| 187 |
|
| 188 |
let bin_reader_t = { Type_class.read = bin_read_t; vtag_read = __bin_read_t__ } |
| 189 |
end |