Source file iobuf.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
[%%import "config.h"]

module Bigstring_in_this_directory = Bigstring
open! Import
module Bigstring = Bigstring_in_this_directory

module Unix = Core_unix

let arch_sixtyfour = Sys.word_size = 64

module T = struct
  type t =
    (* WHEN YOU CHANGE THIS, CHANGE iobuf_fields IN iobuf.h AS WELL!!! *)
    { mutable buf : Bigstring.t sexp_opaque;
      (* The data in [buf] is at indices [lo], [lo+1], ... [hi-1]. *)
      mutable lo_min : int;
      mutable lo     : int;
      mutable hi     : int;
      mutable hi_max : int;
    } [@@deriving fields, sexp_of]
end
open T
type (-'read_write, +'seek) t = T.t [@@deriving sexp_of]
type    seek = Iobuf_intf.   seek [@@deriving sexp_of]
type no_seek = Iobuf_intf.no_seek [@@deriving sexp_of]
module type Bound = Iobuf_intf.Bound with type ('d, 'w) iobuf := ('d, 'w) t

let read_only t = t
let no_seek t = t

let fail t message a sexp_of_a =
  (* Immediately convert the iobuf to sexp.  Otherwise, the iobuf could be modified before
     conversion and printing.  Since we plan to use iobufs for pooled network buffers in
     practice, this could be very confusing when debugging production systems. *)
  failwiths message (a, [%sexp_of: (_, _) t] t)
    (Tuple.T2.sexp_of_t sexp_of_a ident)

module Lo_bound = struct
  let stale t iobuf =
    fail iobuf "Iobuf.Lo_bound.restore got stale snapshot" t [%sexp_of: int]

  type t = int [@@deriving compare, sexp_of] (* lo *)

  let window t = t.lo

  let restore t iobuf =
    if t < iobuf.lo_min || t > iobuf.hi then stale t iobuf;
    iobuf.lo <- t;
  ;;

  let limit t = t.lo_min
end

module Hi_bound = struct
  let stale t iobuf =
    fail iobuf "Iobuf.Hi_bound.restore got stale snapshot" t [%sexp_of: int]

  type t = int [@@deriving compare, sexp_of] (* hi *)

  let window t = t.hi

  let restore t iobuf =
    if t > iobuf.hi_max || t < iobuf.lo then stale t iobuf;
    iobuf.hi <- t;
  ;;

  let limit t = t.hi_max
end

let length t = t.hi - t.lo

let is_empty t = length t = 0

let rewind t = t.lo <- t.lo_min

let reset t =
  t.lo <- t.lo_min;
  t.hi <- t.hi_max
;;

let flip_lo t =
  t.hi <- t.lo;
  t.lo <- t.lo_min;
;;
let bounded_flip_lo_stale t lo_min =
  fail t "Iobuf.bounded_flip_lo got stale snapshot" lo_min [%sexp_of: Lo_bound.t]
;;
let bounded_flip_lo t lo_min =
  if lo_min < t.lo_min || lo_min > t.lo then bounded_flip_lo_stale t lo_min
  else (t.hi <- t.lo; t.lo <- lo_min)
;;

let flip_hi t =
  t.lo <- t.hi;
  t.hi <- t.hi_max;
;;
let bounded_flip_hi_stale t hi_max =
  fail t "Iobuf.bounded_flip_hi got stale snapshot" hi_max [%sexp_of: Hi_bound.t]
;;
let bounded_flip_hi t hi_max =
  if hi_max > t.hi_max || hi_max < t.hi then bounded_flip_hi_stale t hi_max
  else (t.lo <- t.hi; t.hi <- hi_max)
;;

let capacity t = t.hi_max - t.lo_min

let invariant _ _ t =
  try
    Fields.Direct.iter t
      ~buf:(fun _ _ _ -> ())
      ~lo_min:(fun _ _ lo_min ->
        assert (lo_min >= 0);
        assert (lo_min = t.hi_max - capacity t))
      ~hi_max:(fun _ _ hi_max ->
        assert (hi_max >= t.lo);
        assert (hi_max = t.lo_min + capacity t))
      ~lo:(fun _ _ lo ->
        assert (lo >= t.lo_min);
        assert (lo <= t.hi))
      ~hi:(fun _ _ hi ->
        assert (hi >= t.lo);
        assert (hi <= t.hi_max))
  with e -> fail t "Iobuf.invariant failed" e [%sexp_of: exn]
;;


(* We want [check_range] inlined, so we don't want a string constant in there. *)
let bad_range ~pos ~len t =
  fail t "Iobuf got invalid range" (`pos pos, `len len)
    [%sexp_of: [ `pos of int ] * [ `len of int ]];
[@@inline never]
;;

let check_range t ~pos ~len =
  if pos < 0 || len < 0 || len > length t - pos then
    bad_range ~pos ~len t;
[@@inline always]
;;

let of_bigstring ?pos ?len buf =
  let str_len = Bigstring.length buf in
  let pos =
    match pos with
    | None -> 0
    | Some pos ->
      if pos < 0 || pos > str_len then
        failwiths "Iobuf.of_bigstring got invalid pos" (pos, `str_len str_len)
          ([%sexp_of: int * [ `str_len of int ]]);
      pos
  in
  let len =
    match len with
    | None -> str_len - pos
    | Some len ->
      let max_len = str_len - pos in
      if len < 0 || len > max_len then
        failwiths "Iobuf.of_bigstring got invalid len" (len, `max_len max_len)
          ([%sexp_of: int * [ `max_len of int ]]);
      len
  in
  let lo = pos in
  let hi = pos + len in
  { buf; lo_min = lo; lo; hi; hi_max = hi }
;;

let sub_shared ?(pos = 0) ?len t =
  let len =
    match len with
    | None -> length t - pos
    | Some len -> len
  in
  check_range t ~pos ~len;
  let lo = t.lo + pos in
  let hi = lo + len in
  { buf = t.buf;
    lo_min = lo;
    lo;
    hi;
    hi_max = hi;
  }
;;

let set_bounds_and_buffer_sub ~pos ~len ~src ~dst =
  check_range src ~pos ~len;
  let lo = src.lo + pos in
  let hi = lo + len in
  dst.lo_min <- lo;
  dst.lo <- lo;
  dst.hi <- hi;
  dst.hi_max <- hi;
  if not (phys_equal dst.buf src.buf)
  then dst.buf <- src.buf
;;

let set_bounds_and_buffer ~src ~dst =
  dst.lo_min <- src.lo_min;
  dst.lo <- src.lo;
  dst.hi <- src.hi;
  dst.hi_max <- src.hi_max;
  if not (phys_equal dst.buf src.buf)
  then dst.buf <- src.buf
;;

let narrow_lo t = t.lo_min <- t.lo
let narrow_hi t = t.hi_max <- t.hi

let narrow t = narrow_lo t; narrow_hi t

let unsafe_resize t ~len =
  t.hi <- t.lo + len

let resize t ~len =
  if len < 0 then bad_range t ~len ~pos:0;
  let hi = t.lo + len in
  if hi > t.hi_max then bad_range t ~len ~pos:0;
  t.hi <- hi;
[@@inline always]
;;

let protect_window_and_bounds t ~f =
  let lo = t.lo in
  let hi = t.hi in
  let lo_min = t.lo_min in
  let hi_max = t.hi_max in
  let buf = t.buf in  (* also mutable *)
  try
    t.lo_min <- lo;
    t.hi_max <- hi;
    let result = f t in
    t.lo <- lo;
    t.hi <- hi;
    t.lo_min <- lo_min;
    t.hi_max <- hi_max;
    t.buf <- buf;
    result
  with exn ->
    t.lo <- lo;
    t.hi <- hi;
    t.lo_min <- lo_min;
    t.hi_max <- hi_max;
    t.buf <- buf;
    raise exn
;;

let protect_window_and_bounds_1 t x ~f =
  let lo = t.lo in
  let hi = t.hi in
  let lo_min = t.lo_min in
  let hi_max = t.hi_max in
  let buf = t.buf in  (* also mutable *)
  try
    t.lo_min <- lo;
    t.hi_max <- hi;
    let result = f t x in
    t.lo <- lo;
    t.hi <- hi;
    t.lo_min <- lo_min;
    t.hi_max <- hi_max;
    t.buf <- buf;
    result
  with exn ->
    t.lo <- lo;
    t.hi <- hi;
    t.lo_min <- lo_min;
    t.hi_max <- hi_max;
    t.buf <- buf;
    raise exn
;;

let create ~len =
  if len < 0 then
    failwiths "Iobuf.create got negative len" len [%sexp_of: int];
  of_bigstring (Bigstring.create len);
;;

let to_string ?len t =
  let len =
    match len with
    | Some len -> check_range t ~pos:0 ~len; len
    | None -> length t
  in
  Bigstring.to_string t.buf ~pos:t.lo ~len

let of_string s = of_bigstring (Bigstring.of_string s)

let to_bytes t = Bigstring.to_bytes t.buf ~pos:t.lo ~len:(length t)

let of_bytes s = of_bigstring (Bigstring.of_bytes s)

(* We used to do it like {v

let unsafe_with_range t ~pos f =
  f t.buf ~pos:(t.lo + pos);
;;

let with_range t ~pos ~len f =
  check_range t ~pos ~len;
  unsafe_with_range t ~pos f;
;;

let inc_lo t amount = t.lo <- t.lo + amount

(** [unsafe_with_advance] and [unsafe_with_range] forego range checks for code that does
    macro range checks, like we want to do in [Parachute_fix.Std.Protocol].
    Esp. [Consume.Unsafe.int32_le] for unrolled character scanning. *)
let unsafe_with_advance t ~len f =
  let result = unsafe_with_range t ~pos:0 f in
  inc_lo t len;
  result;
;;

let with_advance t ~len f =
  check_range t ~pos:0 ~len;
  unsafe_with_advance t ~len f;
;;

(* pulled out and type-constrained for inlining *)
let ignore_range (_ : Bigstring.t) ~pos:(_ : int) = ()

let advance t len = with_advance t ~len ignore_range

   v} but higher order functions don't get inlined, even in simple uses like advance.
   Therefor, we stick to first order. *)

let [@inline always] unsafe_buf_pos t ~pos = t.lo + pos
let buf_pos_exn t ~pos ~len = check_range t ~pos ~len; unsafe_buf_pos t ~pos

let unsafe_advance t n = t.lo <- t.lo + n
let advance t len = check_range t ~len ~pos:0; unsafe_advance t len [@@inline always]

external bigstring_unsafe_get : Bigstring.t -> pos:int -> char
  = "%caml_ba_unsafe_ref_1"
external bigstring_unsafe_set : Bigstring.t -> pos:int -> char -> unit
  = "%caml_ba_unsafe_set_1"
(* Note that we can get [buf.{pos}] inlined by ensuring that it's monomorphically typed,
   but we can't always get the containing function inlined. *)
(* Similarly, we need the following intermediate functions for the primitives to be
   inlined into.  (Not intuitive, but apparently necessary.) *)
let bigstring_unsafe_get b ~pos   = bigstring_unsafe_get b ~pos
let bigstring_unsafe_set b ~pos c = bigstring_unsafe_set b ~pos c

module Char_elt = struct
  include Char
  let of_bool = function true -> '0' | false -> '1'
end

module T_src = struct
  type t = T.t [@@deriving sexp_of]
  let create = create
  let length = length
  let[@inline] get t pos   = bigstring_unsafe_get t.buf ~pos:(buf_pos_exn t ~len:1 ~pos)
  let[@inline] set t pos c = bigstring_unsafe_set t.buf ~pos:(buf_pos_exn t ~len:1 ~pos) c
end

module Bytes_dst = struct
  include Bytes
  let unsafe_blit ~src ~src_pos ~dst ~dst_pos ~len =
    Bigstring.To_bytes.unsafe_blit
      ~src:src.buf ~src_pos:(unsafe_buf_pos src ~pos:src_pos)
      ~dst ~dst_pos
      ~len
  let create ~len = create len
end

module String_dst = struct
  let sub src ~pos ~len =
    Bigstring.To_string.sub
      src.buf ~pos:(buf_pos_exn src ~pos ~len)
      ~len
  let subo ?(pos = 0) ?len src =
    let len = match len with None -> length src - pos | Some len -> len in
    Bigstring.To_string.subo
      src.buf ~pos:(buf_pos_exn src ~pos ~len)
      ~len
end

module Bigstring_dst = struct
  include Bigstring
  let unsafe_blit ~src ~src_pos ~dst ~dst_pos ~len =
    Bigstring.unsafe_blit
      ~src:src.buf ~src_pos:(unsafe_buf_pos src ~pos:src_pos)
      ~dst ~dst_pos
      ~len
  let create ~len = create len
end

let compact t =
  let len = t.hi - t.lo in
  Bigstring.blit ~src:t.buf ~src_pos:t.lo ~len ~dst:t.buf ~dst_pos:t.lo_min;
  t.lo <- t.lo_min + len;
  t.hi <- t.hi_max;
;;
let bounded_compact_stale t lo_min hi_max =
  fail t "Iobuf.bounded_compact got stale snapshot" (lo_min, hi_max)
    [%sexp_of: Lo_bound.t * Hi_bound.t]
;;
let bounded_compact t lo_min hi_max =
  let len = t.hi - t.lo in
  if hi_max > t.hi_max || hi_max < lo_min + len || lo_min < t.lo_min
  then
    bounded_compact_stale t lo_min hi_max
  else
    (Bigstring.blit ~src:t.buf ~src_pos:t.lo ~len ~dst:t.buf ~dst_pos:lo_min;
     t.lo <- lo_min + len;
     t.hi <- hi_max)

let read_bin_prot reader t ~pos =
  let buf_pos = unsafe_buf_pos t ~pos in
  let pos_ref = ref buf_pos in
  let a = reader.Bin_prot.Type_class.read t.buf ~pos_ref in
  let len = !pos_ref - buf_pos in
  check_range t ~pos ~len;
  (a, len)

module Consume = struct
  type src = (read, seek) t
  module To (Dst : sig
      type t [@@deriving sexp_of]
      val create : len:int -> t
      val length : t -> int
      val get : t -> int -> char
      val set : t -> int -> char -> unit
      val unsafe_blit : (T.t, t) Blit.blit
    end) = struct
    include Test_blit.Make_distinct_and_test (Char_elt) (T_src) (Dst)

    let blit ~src ~dst ~dst_pos ~len =
      blit ~src ~src_pos:0 ~dst ~dst_pos ~len;
      unsafe_advance src len

    let blito ~src ?(src_len = length src) ~dst ?dst_pos () =
      blito ~src ~src_pos:0 ~src_len ~dst ?dst_pos ();
      unsafe_advance src src_len

    let unsafe_blit ~src ~dst ~dst_pos ~len =
      unsafe_blit ~src ~src_pos:0 ~dst ~dst_pos ~len;
      unsafe_advance src len

    let sub src ~len =
      let dst = sub src ~pos:0 ~len in
      unsafe_advance src len;
      dst

    let subo ?len src =
      let len = match len with None -> length src | Some len -> len in
      let dst = subo ~pos:0 ~len src in
      unsafe_advance src len;
      dst
  end
  module To_bytes     = To (Bytes_dst)
  module To_bigstring = To (Bigstring_dst)

  module To_string = struct
    include To_bytes

    let sub src ~len =
      let dst = String_dst.sub src ~len ~pos:0 in
      unsafe_advance src len;
      dst

    let subo ?len src =
      let len = match len with None -> length src | Some len -> len in
      let dst = String_dst.subo ~pos:0 ~len src in
      unsafe_advance src len;
      dst
  end

  type nonrec ('a, 'd, 'w) t = ('d, seek) t -> 'a
    constraint 'd = [> read ]

  let uadv t n x = unsafe_advance t n; x
  [@@inline always]

  let pos t len = buf_pos_exn t ~pos:0 ~len

  let tail_padded_fixed_string ~padding ~len t =
    uadv t len (Bigstring.get_tail_padded_fixed_string t.buf ~pos:(pos t len) ~padding ~len ())
  ;;

  let head_padded_fixed_string ~padding ~len t =
    uadv t len (Bigstring.get_head_padded_fixed_string t.buf ~pos:(pos t len) ~padding ~len ())
  ;;

  let bytes ~str_pos ~len t =
    let dst = Bytes.create (len + str_pos) in
    To_bytes.blit ~src:t ~dst ~len ~dst_pos:str_pos;
    dst
  ;;

  let string ~str_pos ~len t =
    Bytes.unsafe_to_string ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t)

  let bigstring ~str_pos ~len t =
    let dst = Bigstring.create (len + str_pos) in
    To_bigstring.blit ~src:t ~dst ~len ~dst_pos:str_pos;
    dst
  ;;

  let byteso ?(str_pos = 0) ?len t =
    bytes t ~str_pos ~len:(match len with None -> length t | Some len -> len)
  ;;

  let stringo ?(str_pos = 0) ?len t =
    string t ~str_pos ~len:(match len with None -> length t | Some len -> len)
  ;;

  let bigstringo ?(str_pos = 0) ?len t =
    bigstring t ~str_pos ~len:(match len with None -> length t | Some len -> len)
  ;;

  let bin_prot reader t =
    let (a, len) = read_bin_prot reader t ~pos:0 in
    uadv t len a
  ;;

  open Bigstring

  let len = 1
  let [@inline always] char  t = uadv t len (bigstring_unsafe_get t.buf ~pos:(pos t len))
  let [@inline always] uint8 t = uadv t len (unsafe_get_uint8     t.buf ~pos:(pos t len))
  let [@inline always] int8  t = uadv t len (unsafe_get_int8      t.buf ~pos:(pos t len))
  let len = 2
  let [@inline always] int16_be  t = uadv t len (unsafe_get_int16_be  t.buf ~pos:(pos t len))
  let [@inline always] int16_le  t = uadv t len (unsafe_get_int16_le  t.buf ~pos:(pos t len))
  let [@inline always] uint16_be t = uadv t len (unsafe_get_uint16_be t.buf ~pos:(pos t len))
  let [@inline always] uint16_le t = uadv t len (unsafe_get_uint16_le t.buf ~pos:(pos t len))
  let len = 4
  let [@inline always] int32_be  t = uadv t len (unsafe_get_int32_be  t.buf ~pos:(pos t len))
  let [@inline always] int32_le  t = uadv t len (unsafe_get_int32_le  t.buf ~pos:(pos t len))
  let [@inline always] uint32_be t = uadv t len (unsafe_get_uint32_be t.buf ~pos:(pos t len))
  let [@inline always] uint32_le t = uadv t len (unsafe_get_uint32_le t.buf ~pos:(pos t len))
  let len = 8
  let [@inline always] int64_be_exn   t = uadv t len (unsafe_get_int64_be_exn   t.buf ~pos:(pos t len))
  let [@inline always] int64_le_exn   t = uadv t len (unsafe_get_int64_le_exn   t.buf ~pos:(pos t len))
  let [@inline always] uint64_be_exn  t = uadv t len (unsafe_get_uint64_be_exn  t.buf ~pos:(pos t len))
  let [@inline always] uint64_le_exn  t = uadv t len (unsafe_get_uint64_le_exn  t.buf ~pos:(pos t len))
  let [@inline always] int64_t_be     t = uadv t len (unsafe_get_int64_t_be     t.buf ~pos:(pos t len))
  let [@inline always] int64_t_le     t = uadv t len (unsafe_get_int64_t_le     t.buf ~pos:(pos t len))
  let [@inline always] int64_be_trunc t = uadv t len (unsafe_get_int64_be_trunc t.buf ~pos:(pos t len))
  let [@inline always] int64_le_trunc t = uadv t len (unsafe_get_int64_le_trunc t.buf ~pos:(pos t len))
end

let write_bin_prot writer t ~pos a =
  let len = writer.Bin_prot.Type_class.size a in
  let buf_pos = buf_pos_exn t ~pos ~len in
  let stop_pos = writer.Bin_prot.Type_class.write t.buf ~pos:buf_pos a in
  if stop_pos - buf_pos = len then len
  else fail t "Iobuf.write_bin_prot got unexpected number of bytes written \
               (Bin_prot bug: Type_class.write disagrees with .size)"
         (`size_len len, `buf_pos buf_pos, `write_stop_pos stop_pos)
         [%sexp_of: [ `size_len of int ]
                    * [ `buf_pos of int ]
                    * [ `write_stop_pos of int ]]

(* [Itoa] provides a range of functions for integer to ASCII conversion, used by [Poke],
   [Fill] and their [Unsafe] versions.

   The implementation here is done in terms of negative decimals due to the properties of
   [Int.min_value]. Since the result of [Int.(abs min_value)] is [Int.min_value], an
   attempt to utilize a positive decimal loop by writing the sign and calling [Int.abs x]
   fails. The converse, with [- Int.max_value] works for both cases. *)
module Itoa = struct
  (* [num_digits x] returns the number of digits in [x] for non-positive integers
     ([num_digits 0] is defined as 1).

     The below tends to perform better than a binary search or [/= 10 while <> 0], likely
     due to decimal values for our applications skewing towards smaller numbers. *)
  let num_digits x =
    if x > -10 then 1
    else if x > -100 then 2
    else if x > -1000 then 3
    else if x > -10000 then 4
    else if x > -100000 then 5
    else if x > -1000000 then 6
    else if x > -10000000 then 7
    else if x > -100000000 then 8
    else if x > -1000000000 then 9
    else if arch_sixtyfour
    then
      if      x > -1000000000 * 10 then 10
      else if x > -1000000000 * 100 then 11
      else if x > -1000000000 * 1000 then 12
      else if x > -1000000000 * 10000 then 13
      else if x > -1000000000 * 100000 then 14
      else if x > -1000000000 * 1000000 then 15
      else if x > -1000000000 * 10000000 then 16
      else if x > -1000000000 * 100000000 then 17
      else if x > -1000000000 * 1000000000 then 18
      else 19
    else
      10

  let () = assert (String.length (Int.to_string Int.min_value) <= 19 + 1)

  (* Despite the div/mod by a constant optimizations, it's a slight savings to avoid a
     second div/mod. Note also that passing in an [int ref], rather than creating the ref
     locally here, results in allocation on the benchmarks. *)
  let unsafe_poke_negative_decimal_without_sign t ~pos ~len int =
    let int = ref int in
    for pos = pos + len - 1 downto pos do
      let x = !int in
      int := !int / 10;
      bigstring_unsafe_set t.buf ~pos (Char.unsafe_of_int (48 + ((- x) + !int * 10)));
    done

  let unsafe_poke_negative_decimal t ~pos ~len int =
    bigstring_unsafe_set t.buf ~pos '-';
    (* +1 and -1 to account for '-' *)
    unsafe_poke_negative_decimal_without_sign t ~pos:(pos + 1) ~len:(len - 1) int

  let poke_decimal t ~pos int =
    if int < 0 then (
      let len = 1 + num_digits int in
      unsafe_poke_negative_decimal t ~pos:(buf_pos_exn t ~pos ~len) ~len int;
      len)
    else (
      let len = num_digits (- int) in
      unsafe_poke_negative_decimal_without_sign
        t ~pos:(buf_pos_exn t ~pos ~len) ~len (- int);
      len)

  let unsafe_poke_decimal t ~pos int =
    if int < 0 then (
      let len = 1 + num_digits int in
      unsafe_poke_negative_decimal t ~pos:(unsafe_buf_pos t ~pos) ~len int;
      len)
    else (
      let len = num_digits (- int) in
      unsafe_poke_negative_decimal_without_sign
        t ~pos:(unsafe_buf_pos t ~pos) ~len (- int);
      len)
end

module Fill = struct
  type nonrec ('a, 'd, 'w) t = (read_write, seek) t -> 'a -> unit
    constraint 'd = [> read ]

  let pos t len = buf_pos_exn t ~pos:0 ~len
  let uadv = unsafe_advance

  let tail_padded_fixed_string ~padding ~len t src =
    Bigstring.set_tail_padded_fixed_string ~padding ~len t.buf ~pos:(pos t len) src;
    uadv t len
  ;;

  let head_padded_fixed_string ~padding ~len t src =
    Bigstring.set_head_padded_fixed_string ~padding ~len t.buf ~pos:(pos t len) src;
    uadv t len
  ;;

  let bytes ~str_pos ~len t src =
    Bigstring.From_bytes.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(pos t len);
    uadv t len
  ;;

  let string ~str_pos ~len t src =
    Bigstring.From_string.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(pos t len);
    uadv t len
  ;;

  let bigstring ~str_pos ~len t src =
    Bigstring.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(pos t len);
    uadv t len
  ;;

  let byteso ?(str_pos = 0) ?len t src =
    bytes t src ~str_pos
      ~len:(match len with None -> Bytes.length src - str_pos | Some len -> len)
  ;;

  let stringo ?(str_pos = 0) ?len t src =
    string t src ~str_pos
      ~len:(match len with None -> String.length src - str_pos | Some len -> len)
  ;;

  let bigstringo ?(str_pos = 0) ?len t src =
    bigstring t src ~str_pos
      ~len:(match len with None -> Bigstring.length src - str_pos | Some len -> len)
  ;;

  let bin_prot writer t a = write_bin_prot writer t ~pos:0 a |> uadv t

  open Bigstring

  let len = 1
  let [@inline always] char t c =
    bigstring_unsafe_set t.buf c ~pos:(pos t len);
    uadv t len
  let [@inline always] uint8_trunc t i =
    unsafe_set_uint8 t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] int8_trunc t i =
    unsafe_set_int8 t.buf i ~pos:(pos t len);
    uadv t len

  let len = 2
  let [@inline always] int16_be_trunc t i =
    unsafe_set_int16_be t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] int16_le_trunc t i =
    unsafe_set_int16_le t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] uint16_be_trunc t i =
    unsafe_set_uint16_be t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] uint16_le_trunc t i =
    unsafe_set_uint16_le t.buf i ~pos:(pos t len);
    uadv t len

  let len = 4
  let [@inline always] int32_be_trunc t i =
    unsafe_set_int32_be t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] int32_le_trunc t i =
    unsafe_set_int32_le t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] uint32_be_trunc t i =
    unsafe_set_uint32_be t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] uint32_le_trunc t i =
    unsafe_set_uint32_le t.buf i ~pos:(pos t len);
    uadv t len

  let len = 8
  let [@inline always] int64_be t i =
    unsafe_set_int64_be t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] int64_le t i =
    unsafe_set_int64_le t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] uint64_be_trunc t i =
    unsafe_set_uint64_be t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] uint64_le_trunc t i =
    unsafe_set_uint64_le t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] int64_t_be t i =
    unsafe_set_int64_t_be t.buf i ~pos:(pos t len);
    uadv t len
  let [@inline always] int64_t_le t i =
    unsafe_set_int64_t_le t.buf i ~pos:(pos t len);
    uadv t len

  let decimal t i = uadv t (Itoa.poke_decimal t ~pos:0 i)
end

module Peek = struct
  type src = (read, no_seek) t
  module To_bytes     = Test_blit.Make_distinct_and_test (Char_elt) (T_src) (Bytes_dst)
  module To_bigstring = Test_blit.Make_distinct_and_test (Char_elt) (T_src) (Bigstring_dst)

  module To_string = struct
    include To_bytes
    include String_dst
  end

  type nonrec ('a, 'd, 'w) t = ('d, 'w) t -> pos:int -> 'a
    constraint 'd = [> read ]

  let spos = buf_pos_exn (* "safe position" *)

  let tail_padded_fixed_string ~padding ~len t ~pos =
    Bigstring.get_tail_padded_fixed_string t.buf ~padding ~len ~pos:(spos t ~len ~pos) ()
  ;;

  let head_padded_fixed_string ~padding ~len t ~pos =
    Bigstring.get_head_padded_fixed_string t.buf ~padding ~len ~pos:(spos t ~len ~pos) ()
  ;;

  let bytes ~str_pos ~len t ~pos =
    let dst = Bytes.create (len + str_pos) in
    Bigstring.To_bytes.blit ~src:t.buf ~src_pos:(spos t ~len ~pos) ~len
      ~dst ~dst_pos:str_pos;
    dst
  ;;

  let string ~str_pos ~len t ~pos =
    Bytes.unsafe_to_string
      ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t ~pos)

  let bigstring ~str_pos ~len t ~pos =
    let dst = Bigstring.create (len + str_pos) in
    Bigstring.blit ~src:t.buf ~src_pos:(spos t ~len ~pos) ~len ~dst ~dst_pos:str_pos;
    dst
  ;;

  let byteso ?(str_pos = 0) ?len t ~pos =
    bytes t ~pos ~str_pos
      ~len:(match len with None -> length t - pos | Some len -> len)
  ;;

  let stringo ?(str_pos = 0) ?len t ~pos =
    string t ~pos ~str_pos
      ~len:(match len with None -> length t - pos | Some len -> len)
  ;;

  let bigstringo ?(str_pos = 0) ?len t ~pos =
    bigstring t ~pos ~str_pos
      ~len:(match len with None -> length t - pos | Some len -> len)
  ;;

  let bin_prot reader t ~pos = read_bin_prot reader t ~pos |> fst

  let index t ?(pos = 0) ?(len = length t - pos) c =
    let pos = spos t ~len ~pos in
    Option.map (Bigstring.find ~pos ~len c t.buf) ~f:(fun x -> x - t.lo)
  ;;

  open Bigstring

  let [@inline always] char t ~pos = T_src.get t pos
  let len = 1
  let [@inline always] uint8 t ~pos = unsafe_get_uint8 t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] int8  t ~pos = unsafe_get_int8  t.buf ~pos:(spos t ~len ~pos)
  let len = 2
  let [@inline always] int16_be  t ~pos = unsafe_get_int16_be  t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] int16_le  t ~pos = unsafe_get_int16_le  t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] uint16_be t ~pos = unsafe_get_uint16_be t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] uint16_le t ~pos = unsafe_get_uint16_le t.buf ~pos:(spos t ~len ~pos)
  let len = 4
  let [@inline always] int32_be  t ~pos = unsafe_get_int32_be  t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] int32_le  t ~pos = unsafe_get_int32_le  t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] uint32_be t ~pos = unsafe_get_uint32_be t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] uint32_le t ~pos = unsafe_get_uint32_le t.buf ~pos:(spos t ~len ~pos)
  let len = 8
  let [@inline always] int64_be_exn   t ~pos = unsafe_get_int64_be_exn   t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] int64_le_exn   t ~pos = unsafe_get_int64_le_exn   t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] uint64_be_exn  t ~pos = unsafe_get_uint64_be_exn  t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] uint64_le_exn  t ~pos = unsafe_get_uint64_le_exn  t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] int64_t_be     t ~pos = unsafe_get_int64_t_be     t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] int64_t_le     t ~pos = unsafe_get_int64_t_le     t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] int64_be_trunc t ~pos = unsafe_get_int64_be_trunc t.buf ~pos:(spos t ~len ~pos)
  let [@inline always] int64_le_trunc t ~pos = unsafe_get_int64_le_trunc t.buf ~pos:(spos t ~len ~pos)
end

module Poke = struct
  type nonrec ('a, 'd, 'w) t = (read_write, 'w) t -> pos:int -> 'a -> unit
    constraint 'd = [> read ]

  let spos = buf_pos_exn (* "safe position" *)

  let tail_padded_fixed_string ~padding ~len t ~pos src =
    Bigstring.set_tail_padded_fixed_string ~padding ~len t.buf ~pos:(spos t ~len ~pos) src
  ;;

  let head_padded_fixed_string ~padding ~len t ~pos src =
    Bigstring.set_head_padded_fixed_string ~padding ~len t.buf ~pos:(spos t ~len ~pos) src
  ;;

  let bytes ~str_pos ~len t ~pos src =
    Bigstring.From_bytes.blit ~src ~src_pos:str_pos ~len
      ~dst:t.buf ~dst_pos:(spos t ~len ~pos)
  ;;

  let string ~str_pos ~len t ~pos src =
    Bigstring.From_string.blit ~src ~src_pos:str_pos ~len
      ~dst:t.buf ~dst_pos:(spos t ~len ~pos)
  ;;

  let bigstring ~str_pos ~len t ~pos src =
    Bigstring.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(spos t ~len ~pos)
  ;;

  let byteso ?(str_pos = 0) ?len t ~pos src =
    bytes t ~str_pos ~pos src
      ~len:(match len with None -> Bytes.length src - str_pos | Some len -> len)
  ;;

  let stringo ?(str_pos = 0) ?len t ~pos src =
    string t ~str_pos ~pos src
      ~len:(match len with None -> String.length src - str_pos | Some len -> len)
  ;;

  let bigstringo ?(str_pos = 0) ?len t ~pos src =
    bigstring t ~str_pos ~pos src
      ~len:(match len with None -> Bigstring.length src - str_pos | Some len -> len)
  ;;

  let bin_prot writer t ~pos a = write_bin_prot writer t ~pos a |> (ignore : int -> unit)

  open Bigstring

  let len = 1
  let [@inline always] char t ~pos c = T_src.set t pos c
  let [@inline always] uint8_trunc t ~pos i =
    unsafe_set_uint8 t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] int8_trunc t ~pos i =
    unsafe_set_int8 t.buf ~pos:(spos t ~len ~pos) i

  let len = 2
  let [@inline always] int16_be_trunc t ~pos i =
    unsafe_set_int16_be t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] int16_le_trunc t ~pos i =
    unsafe_set_int16_le t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] uint16_be_trunc t ~pos i =
    unsafe_set_uint16_be t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] uint16_le_trunc t ~pos i =
    unsafe_set_uint16_le t.buf ~pos:(spos t ~len ~pos) i

  let len = 4
  let [@inline always] int32_be_trunc t ~pos i =
    unsafe_set_int32_be t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] int32_le_trunc t ~pos i =
    unsafe_set_int32_le t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] uint32_be_trunc t ~pos i =
    unsafe_set_uint32_be t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] uint32_le_trunc t ~pos i =
    unsafe_set_uint32_le t.buf ~pos:(spos t ~len ~pos) i

  let len = 8
  let [@inline always] int64_be t ~pos i =
    unsafe_set_int64_be t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] int64_le t ~pos i =
    unsafe_set_int64_le t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] uint64_be_trunc t ~pos i =
    unsafe_set_uint64_be t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] uint64_le_trunc t ~pos i =
    unsafe_set_uint64_le t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] int64_t_be t ~pos i =
    unsafe_set_int64_t_be t.buf ~pos:(spos t ~len ~pos) i
  let [@inline always] int64_t_le t ~pos i =
    unsafe_set_int64_t_le t.buf ~pos:(spos t ~len ~pos) i

  let decimal = Itoa.poke_decimal
end

module Blit = struct
  type 'rw t_no_seek = ('rw, no_seek) t
  module T_dst = struct
    include T_src
    let unsafe_blit ~src ~src_pos ~dst ~dst_pos ~len =
      Bigstring.unsafe_blit ~len
        ~src:src.buf ~src_pos:(unsafe_buf_pos src ~pos:src_pos)
        ~dst:dst.buf ~dst_pos:(unsafe_buf_pos dst ~pos:dst_pos)
  end
  include Test_blit.Make_and_test (Char_elt) (T_dst)
  (* Workaround the inability of the compiler to inline in the presence of functors. *)
  let unsafe_blit = T_dst.unsafe_blit
end

module Blit_consume = struct
  let unsafe_blit ~src ~dst ~dst_pos ~len =
    Blit.unsafe_blit ~src ~src_pos:0 ~dst ~dst_pos ~len;
    unsafe_advance src len
  let blit ~src ~dst ~dst_pos ~len =
    Blit.blit ~src ~src_pos:0 ~dst ~dst_pos ~len;
    unsafe_advance src len
  let blito ~src ?(src_len = length src) ~dst ?(dst_pos = 0) () =
    blit ~src ~dst ~dst_pos ~len:src_len
  let sub src ~len =
    let dst = Blit.sub src ~pos:0 ~len in
    unsafe_advance src len;
    dst
  let subo ?len src =
    let len = match len with None -> length src | Some len -> len in
    sub src ~len
end

module Blit_fill = struct
  let unsafe_blit ~src ~src_pos ~dst ~len =
    Blit.unsafe_blit ~src ~src_pos ~dst ~dst_pos:0 ~len;
    unsafe_advance dst len
  let blit ~src ~src_pos ~dst ~len =
    Blit.blit ~src ~src_pos ~dst ~dst_pos:0 ~len;
    unsafe_advance dst len
  let blito ~src ?(src_pos = 0) ?(src_len = length src - src_pos) ~dst () =
    blit ~src ~src_pos ~dst ~len:src_len
end

module Blit_consume_and_fill = struct
  let unsafe_blit ~src ~dst ~len =
    if phys_equal src dst then advance src len else begin
      Blit.unsafe_blit ~src ~src_pos:0 ~dst ~dst_pos:0 ~len;
      unsafe_advance src len;
      unsafe_advance dst len
    end
  let blit ~src ~dst ~len =
    if phys_equal src dst then advance src len else begin
      Blit.blit ~src ~src_pos:0 ~dst ~dst_pos:0 ~len;
      unsafe_advance src len;
      unsafe_advance dst len
    end
  let blito ~src ?(src_len = length src) ~dst () =
    blit ~src ~dst ~len:src_len
end

let bin_prot_length_prefix_bytes = 4

let consume_bin_prot t bin_prot_reader =
  let result =
    if length t < bin_prot_length_prefix_bytes then
      error "Iobuf.consume_bin_prot not enough data to read length" t
        ([%sexp_of: (_, _) t])
    else begin
      let mark = t.lo in
      let v_len = Consume.int32_be t in
      if v_len > length t then begin
        t.lo <- mark;
        error "Iobuf.consume_bin_prot not enough data to read value" (v_len, t)
          ([%sexp_of: int * (_, _) t]);
      end else Ok (Consume.bin_prot bin_prot_reader t)
    end
  in
  result;
;;

let fill_bin_prot t writer v =
  let v_len = writer.Bin_prot.Type_class.size v in
  let need = v_len + bin_prot_length_prefix_bytes in
  let result =
    if need > length t then
      error "Iobuf.fill_bin_prot not enough space" (need, t)
        ([%sexp_of: int * (_, _) t])
    else begin
      Fill.int32_be_trunc t v_len;
      Fill.bin_prot writer t v;
      Ok ();
    end
  in
  result;
;;

module Expert = struct
  let buf t    = t.buf
  let hi_max t = t.hi_max
  let hi t     = t.hi
  let lo t     = t.lo
  let lo_min t = t.lo_min

  let to_bigstring_shared ?pos ?len t =
    let pos, len =
      Ordered_collection_common.get_pos_len_exn () ?pos ?len
        ~total_length:(length t)
    in
    Bigstring.sub_shared t.buf ~pos:(t.lo + pos) ~len
  ;;

  let to_iovec_shared ?pos ?len t =
    let pos, len =
      Ordered_collection_common.get_pos_len_exn () ?pos ?len
        ~total_length:(length t)
    in
    Unix.IOVec.of_bigstring t.buf ~pos:(t.lo + pos) ~len
  ;;

  let reinitialize_of_bigstring t ~pos ~len buf =
    let str_len = Bigstring.length buf in
    if pos < 0 || pos > str_len then
      raise_s [%message "Expert.reinitialize_of_bigstring got invalid pos"
                          (pos     : int)
                          (str_len : int)];
    let max_len = str_len - pos in
    if len < 0 || len > max_len then
      raise_s [%message "Expert.reinitialize_of_bigstring got invalid len"
                          (len     : int)
                          (max_len : int)];
    let lo = pos in
    let hi = pos + len in
    (* avoid [caml_modify], if possible *)
    if not (phys_equal t.buf buf)
    then t.buf <- buf;
    t.lo_min   <- lo;
    t.lo       <- lo;
    t.hi       <- hi;
    t.hi_max   <- hi;
  ;;

  let _remember_to_update_reinitialize_of_bigstring
    :  (_, _) t
      -> buf:Bigstring.t
    -> lo_min:int
    -> lo:int
    -> hi:int
    -> hi_max:int
    -> unit
    = Fields.Direct.set_all_mutable_fields

  let set_bounds_and_buffer ~src ~dst = set_bounds_and_buffer ~src ~dst
  let set_bounds_and_buffer_sub ~pos ~len ~src ~dst =
    (set_bounds_and_buffer_sub [@inlined]) ~pos ~len ~src ~dst
end

type ok_or_eof = Ok | Eof [@@deriving compare, sexp_of]

module File_descr = Iobuf_intf.Unix.File_descr

let input t ch =
  match Bigstring.input ch t.buf ~pos:t.lo ~len:(length t) with
  | n                                            -> unsafe_advance t n; Ok
  | exception Bigstring.IOError (n, End_of_file) -> unsafe_advance t n; Eof
;;

let read t fd =
  match Bigstring.read fd t.buf ~pos:t.lo ~len:(length t) with
  | n                                            -> unsafe_advance t n; Ok
  | exception Bigstring.IOError (n, End_of_file) -> unsafe_advance t n; Eof
;;

let read_assume_fd_is_nonblocking t fd =
  let nread =
    Bigstring.read_assume_fd_is_nonblocking fd t.buf ~pos:t.lo ~len:(length t)
  in
  if Syscall_result.Int.is_ok nread then
    unsafe_advance t (Syscall_result.Int.ok_exn nread);
  Syscall_result.ignore_ok_value nread
;;

let pread_assume_fd_is_nonblocking t fd ~offset =
  let nread =
    Bigstring.pread_assume_fd_is_nonblocking fd ~offset t.buf ~pos:t.lo ~len:(length t)
  in
  unsafe_advance t nread
;;

let recvfrom_assume_fd_is_nonblocking t fd =
  let nread, sockaddr =
    Bigstring.recvfrom_assume_fd_is_nonblocking fd t.buf ~pos:t.lo ~len:(length t)
  in
  unsafe_advance t nread;
  sockaddr
;;

[%%ifdef JSC_RECVMMSG]

(* Allocate and pre-populate the [struct mmsghdr]s and associated [struct iovec]s. Reusing
   this context reduces the cost of calls to [recvmmsg] considerably if the iobuf array is
   large. *)
module Recvmmsg_context = struct
  type ctx

  external unsafe_ctx : ([> write], seek) t array -> ctx = "iobuf_recvmmsg_ctx"

  let ctx ts =
    if Array.for_all ts ~f:(fun t -> length t = capacity t) then
      unsafe_ctx ts
    else
      failwiths "Recvmmsg_context.create: all buffers must be reset" ts
        [%sexp_of: (_, _) t array]
  ;;

  (* we retain a reference to the underlying bigstrings, in the event that callers
     mistakenly use set_bounds_and_buffer. Since we've cached the underlying memory
     referenced by the bigstring, we want to prevent it from being garbage collected and
     released. *)
  type nonrec t = {
    iobufs : (read_write, seek) t array;
    bstrs  : Bigstring.t array;
    ctx    : ctx;
  }

  let create iobufs = {
    iobufs;
    bstrs = Array.map iobufs ~f:(fun iobuf -> iobuf.buf);
    ctx = ctx iobufs;
  }
end

external unsafe_recvmmsg_assume_fd_is_nonblocking
  : File_descr.t
    -> (read_write, seek) t array
    -> Recvmmsg_context.ctx
    -> Unix.Syscall_result.Int.t
  = "iobuf_recvmmsg_assume_fd_is_nonblocking_stub" [@@noalloc]

let recvmmsg_assume_fd_is_nonblocking fd { Recvmmsg_context. iobufs; ctx; _ } =
  unsafe_recvmmsg_assume_fd_is_nonblocking fd iobufs ctx
;;

let recvmmsg_assume_fd_is_nonblocking =
  (* We link with [--wrap recvmmsg].  If we have compiled on a machine with recvmmsg
     (e.g., CentOS 6) but then run on a machine without (e.g., CentOS 5), our wrapped
     [recvmmsg] always returns -1 and sets errno to ENOSYS. *)
  match
    Unix.Syscall_result.Int.to_result
      (let fd = File_descr.of_int (-1) in
       recvmmsg_assume_fd_is_nonblocking fd (Recvmmsg_context.create [||]))
  with
  | Error ENOSYS -> Or_error.unimplemented "Iobuf.recvmmsg_assume_fd_is_nonblocking"
  | _            -> Ok                     recvmmsg_assume_fd_is_nonblocking
;;

[%%else]
(* not JSC_RECVMMSG *)

module Recvmmsg_context = struct
  type t = unit
  let create = ignore
end

let recvmmsg_assume_fd_is_nonblocking =
  Or_error.unimplemented "Iobuf.recvmmsg_assume_fd_is_nonblocking"
;;

[%%endif]
(* JSC_RECVMMSG *)

let unsafe_sent t result =
  if Syscall_result.Int.is_ok result
  then (unsafe_advance t (Syscall_result.Int.ok_exn result);
        Syscall_result.unit)
  else Syscall_result.Int.reinterpret_error_exn result

(* Don't use [Or_error.map].  The natural usage results in a partially applied function,
   which is slower to call. *)
let send_nonblocking_no_sigpipe () =
  match Bigstring.send_nonblocking_no_sigpipe with
  | Error _ as e -> e
  | Ok send ->
    Ok (fun t fd -> unsafe_sent t (send fd t.buf ~pos:t.lo ~len:(length t)))
;;

let sendto_nonblocking_no_sigpipe () =
  match Bigstring.sendto_nonblocking_no_sigpipe with
  | Error _ as e -> e
  | Ok sendto ->
    Ok (fun t fd addr -> unsafe_sent t (sendto fd t.buf ~pos:t.lo ~len:(length t) addr))
;;

let output t ch =
  let nwritten =
    Bigstring.output ch t.buf ~pos:t.lo ~len:(length t)
  in
  unsafe_advance t nwritten
;;

let write t fd =
  let nwritten =
    Bigstring.write fd t.buf ~pos:t.lo ~len:(length t)
  in
  unsafe_advance t nwritten
;;

let write_assume_fd_is_nonblocking t fd =
  (* This is safe because of the invariant of [t] that the window is within the buffer
     (unless the user has violated the invariant with an unsafe operation). *)
  let nwritten =
    Bigstring.unsafe_write_assume_fd_is_nonblocking fd t.buf ~pos:t.lo ~len:(length t)
  in
  unsafe_advance t nwritten
;;

let pwrite_assume_fd_is_nonblocking t fd ~offset =
  let nwritten =
    Bigstring.pwrite_assume_fd_is_nonblocking fd ~offset t.buf ~pos:t.lo ~len:(length t)
  in
  unsafe_advance t nwritten
;;

module Unsafe = struct
  module Consume = struct
    (* copy of Consume with pos replaced by an unsafe version *)

    type src = Consume.src
    module To_bytes = struct
      include Consume.To_bytes
      let blit = unsafe_blit
    end
    module To_bigstring = struct
      include Consume.To_bigstring
      let blit = unsafe_blit
    end

    module To_string = Consume.To_string

    type ('a, 'd, 'w) t = ('a, 'd, 'w) Consume.t

    let uadv t n x = unsafe_advance t n; x [@@inline always]
    let upos t = unsafe_buf_pos t ~pos:0

    let tail_padded_fixed_string ~padding ~len t =
      uadv t len
        (Bigstring.get_tail_padded_fixed_string t.buf ~pos:(upos t) ~padding ~len ())
    ;;

    let head_padded_fixed_string ~padding ~len t =
      uadv t len
        (Bigstring.get_head_padded_fixed_string t.buf ~pos:(upos t) ~padding ~len ())
    ;;

    let bytes      = Consume.bytes
    let string     = Consume.string
    let bigstring  = Consume.bigstring
    let byteso     = Consume.byteso
    let stringo    = Consume.stringo
    let bigstringo = Consume.bigstringo

    let bin_prot = Consume.bin_prot

    open Bigstring

    let len = 1
    let [@inline always] char  t = uadv t len (bigstring_unsafe_get t.buf ~pos:(upos t))
    let [@inline always] uint8 t = uadv t len (unsafe_get_uint8     t.buf ~pos:(upos t))
    let [@inline always] int8  t = uadv t len (unsafe_get_int8      t.buf ~pos:(upos t))
    let len = 2
    let [@inline always] int16_be  t = uadv t len (unsafe_get_int16_be  t.buf ~pos:(upos t))
    let [@inline always] int16_le  t = uadv t len (unsafe_get_int16_le  t.buf ~pos:(upos t))
    let [@inline always] uint16_be t = uadv t len (unsafe_get_uint16_be t.buf ~pos:(upos t))
    let [@inline always] uint16_le t = uadv t len (unsafe_get_uint16_le t.buf ~pos:(upos t))
    let len = 4
    let [@inline always] int32_be  t = uadv t len (unsafe_get_int32_be  t.buf ~pos:(upos t))
    let [@inline always] int32_le  t = uadv t len (unsafe_get_int32_le  t.buf ~pos:(upos t))
    let [@inline always] uint32_be t = uadv t len (unsafe_get_uint32_be t.buf ~pos:(upos t))
    let [@inline always] uint32_le t = uadv t len (unsafe_get_uint32_le t.buf ~pos:(upos t))
    let len = 8
    let [@inline always] int64_be_exn   t = uadv t len (unsafe_get_int64_be_exn   t.buf ~pos:(upos t))
    let [@inline always] int64_le_exn   t = uadv t len (unsafe_get_int64_le_exn   t.buf ~pos:(upos t))
    let [@inline always] uint64_be_exn  t = uadv t len (unsafe_get_uint64_be_exn  t.buf ~pos:(upos t))
    let [@inline always] uint64_le_exn  t = uadv t len (unsafe_get_uint64_le_exn  t.buf ~pos:(upos t))
    let [@inline always] int64_t_be     t = uadv t len (unsafe_get_int64_t_be     t.buf ~pos:(upos t))
    let [@inline always] int64_t_le     t = uadv t len (unsafe_get_int64_t_le     t.buf ~pos:(upos t))
    let [@inline always] int64_be_trunc t = uadv t len (unsafe_get_int64_be_trunc t.buf ~pos:(upos t))
    let [@inline always] int64_le_trunc t = uadv t len (unsafe_get_int64_le_trunc t.buf ~pos:(upos t))
  end

  module Fill = struct
    type ('a, 'd, 'w) t = ('a, 'd, 'w) Fill.t

    (* copy with unsafe pos *)

    let upos t _len = unsafe_buf_pos t ~pos:0
    let uadv t n = unsafe_advance t n

    let tail_padded_fixed_string ~padding ~len t src =
      Bigstring.set_tail_padded_fixed_string ~padding ~len t.buf ~pos:(upos t len) src;
      uadv t len
    ;;

    let head_padded_fixed_string ~padding ~len t src =
      Bigstring.set_head_padded_fixed_string ~padding ~len t.buf ~pos:(upos t len) src;
      uadv t len
    ;;

    let bytes ~str_pos ~len t src =
      Bigstring.From_bytes.blit ~src ~src_pos:str_pos ~len
        ~dst:t.buf ~dst_pos:(upos t len);
      uadv t len
    ;;

    let string ~str_pos ~len t src =
      Bigstring.From_string.blit ~src ~src_pos:str_pos ~len
        ~dst:t.buf ~dst_pos:(upos t len);
      uadv t len
    ;;

    let bigstring ~str_pos ~len t src =
      Bigstring.blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(upos t len);
      uadv t len
    ;;

    let byteso ?(str_pos = 0) ?len t src =
      bytes t src ~str_pos
        ~len:(match len with None -> Bytes.length src - str_pos | Some len -> len)
    ;;

    let stringo ?(str_pos = 0) ?len t src =
      string t src ~str_pos
        ~len:(match len with None -> String.length src - str_pos | Some len -> len)
    ;;

    let bigstringo ?(str_pos = 0) ?len t src =
      bigstring t src ~str_pos
        ~len:(match len with None -> Bigstring.length src - str_pos | Some len -> len)
    ;;

    let bin_prot = Fill.bin_prot

    open Bigstring

    let len = 1
    let [@inline always] char t c = bigstring_unsafe_set t.buf c ~pos:(upos t len); uadv t len
    let len = 2
    let [@inline always] int16_be_trunc  t i = unsafe_set_int16_be  t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] int16_le_trunc  t i = unsafe_set_int16_le  t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] uint16_be_trunc t i = unsafe_set_uint16_be t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] uint16_le_trunc t i = unsafe_set_uint16_le t.buf i ~pos:(upos t len); uadv t len
    let len = 4
    let [@inline always] int32_be_trunc  t i = unsafe_set_int32_be  t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] int32_le_trunc  t i = unsafe_set_int32_le  t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] uint32_be_trunc t i = unsafe_set_uint32_be t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] uint32_le_trunc t i = unsafe_set_uint32_le t.buf i ~pos:(upos t len); uadv t len
    let len = 8
    let [@inline always] int64_be        t i = unsafe_set_int64_be   t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] int64_le        t i = unsafe_set_int64_le   t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] uint64_be_trunc t i = unsafe_set_uint64_be  t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] uint64_le_trunc t i = unsafe_set_uint64_le  t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] int64_t_be      t i = unsafe_set_int64_t_be t.buf i ~pos:(upos t len); uadv t len
    let [@inline always] int64_t_le      t i = unsafe_set_int64_t_le t.buf i ~pos:(upos t len); uadv t len

    (* Bigstring int8 accessors are slow C calls.  Use the fast char primitive. *)
    let [@inline always] uint8_trunc t i = char t (Char.unsafe_of_int i)
    let [@inline always] int8_trunc  t i = char t (Char.unsafe_of_int i)

    let decimal t i = uadv t (Itoa.unsafe_poke_decimal t ~pos:0 i)
  end

  module Peek = struct
    type src = Peek.src
    module To_bytes     = struct include Peek.To_bytes     let blit = unsafe_blit end
    module To_bigstring = struct include Peek.To_bigstring let blit = unsafe_blit end

    module To_string = Peek.To_string

    type ('a, 'd, 'w) t = ('a, 'd, 'w) Peek.t

    let upos = unsafe_buf_pos

    let tail_padded_fixed_string ~padding ~len t ~pos =
      Bigstring.get_tail_padded_fixed_string t.buf ~padding ~len ~pos:(upos t ~pos) ()
    ;;

    let head_padded_fixed_string ~padding ~len t ~pos =
      Bigstring.get_head_padded_fixed_string t.buf ~padding ~len ~pos:(upos t ~pos) ()
    ;;

    let bytes ~str_pos ~len t ~pos =
      let dst = Bytes.create (len + str_pos) in
      Bigstring.To_bytes.unsafe_blit ~src:t.buf ~src_pos:(upos t ~pos)
        ~len ~dst ~dst_pos:str_pos;
      dst
    ;;

    let string ~str_pos ~len t ~pos =
      Bytes.unsafe_to_string
        ~no_mutation_while_string_reachable:(bytes ~str_pos ~len t ~pos)
    ;;

    let bigstring ~str_pos ~len t ~pos =
      let dst = Bigstring.create (len + str_pos) in
      Bigstring.unsafe_blit ~src:t.buf ~src_pos:(upos t ~pos) ~len ~dst ~dst_pos:str_pos;
      dst
    ;;

    let byteso ?(str_pos = 0) ?len t ~pos =
      bytes t ~pos ~str_pos
        ~len:(match len with None -> length t - pos | Some len -> len)
    ;;

    let stringo ?(str_pos = 0) ?len t ~pos =
      string t ~pos ~str_pos
        ~len:(match len with None -> length t - pos | Some len -> len)
    ;;

    let bigstringo ?(str_pos = 0) ?len t ~pos =
      bigstring t ~pos ~str_pos
        ~len:(match len with None -> length t - pos | Some len -> len)
    ;;

    let bin_prot = Peek.bin_prot

    let index = Peek.index

    open Bigstring

    let [@inline always] char           t ~pos = bigstring_unsafe_get      t.buf ~pos:(upos t ~pos)
    let [@inline always] uint8          t ~pos = unsafe_get_uint8          t.buf ~pos:(upos t ~pos)
    let [@inline always] int8           t ~pos = unsafe_get_int8           t.buf ~pos:(upos t ~pos)
    let [@inline always] int16_be       t ~pos = unsafe_get_int16_be       t.buf ~pos:(upos t ~pos)
    let [@inline always] int16_le       t ~pos = unsafe_get_int16_le       t.buf ~pos:(upos t ~pos)
    let [@inline always] uint16_be      t ~pos = unsafe_get_uint16_be      t.buf ~pos:(upos t ~pos)
    let [@inline always] uint16_le      t ~pos = unsafe_get_uint16_le      t.buf ~pos:(upos t ~pos)
    let [@inline always] int32_be       t ~pos = unsafe_get_int32_be       t.buf ~pos:(upos t ~pos)
    let [@inline always] int32_le       t ~pos = unsafe_get_int32_le       t.buf ~pos:(upos t ~pos)
    let [@inline always] uint32_be      t ~pos = unsafe_get_uint32_be      t.buf ~pos:(upos t ~pos)
    let [@inline always] uint32_le      t ~pos = unsafe_get_uint32_le      t.buf ~pos:(upos t ~pos)
    let [@inline always] int64_be_exn   t ~pos = unsafe_get_int64_be_exn   t.buf ~pos:(upos t ~pos)
    let [@inline always] int64_le_exn   t ~pos = unsafe_get_int64_le_exn   t.buf ~pos:(upos t ~pos)
    let [@inline always] uint64_be_exn  t ~pos = unsafe_get_uint64_be_exn  t.buf ~pos:(upos t ~pos)
    let [@inline always] uint64_le_exn  t ~pos = unsafe_get_uint64_le_exn  t.buf ~pos:(upos t ~pos)
    let [@inline always] int64_t_be     t ~pos = unsafe_get_int64_t_be     t.buf ~pos:(upos t ~pos)
    let [@inline always] int64_t_le     t ~pos = unsafe_get_int64_t_le     t.buf ~pos:(upos t ~pos)
    let [@inline always] int64_be_trunc t ~pos = unsafe_get_int64_be_trunc t.buf ~pos:(upos t ~pos)
    let [@inline always] int64_le_trunc t ~pos = unsafe_get_int64_le_trunc t.buf ~pos:(upos t ~pos)
  end

  module Poke = struct
    type ('a, 'd, 'w) t = ('a, 'd, 'w) Poke.t

    let upos = unsafe_buf_pos

    let tail_padded_fixed_string ~padding ~len t ~pos src =
      Bigstring.set_tail_padded_fixed_string ~padding ~len t.buf ~pos:(upos t ~pos) src
    ;;

    let head_padded_fixed_string ~padding ~len t ~pos src =
      Bigstring.set_head_padded_fixed_string ~padding ~len t.buf ~pos:(upos t ~pos) src
    ;;

    let bytes ~str_pos ~len t ~pos src =
      Bigstring.From_bytes.unsafe_blit ~src ~src_pos:str_pos ~len
        ~dst:t.buf ~dst_pos:(upos t ~pos)
    ;;

    let string ~str_pos ~len t ~pos src =
      Bigstring.From_string.unsafe_blit ~src ~src_pos:str_pos ~len
        ~dst:t.buf ~dst_pos:(upos t ~pos)
    ;;

    let bigstring ~str_pos ~len t ~pos src =
      Bigstring.unsafe_blit ~src ~src_pos:str_pos ~len ~dst:t.buf ~dst_pos:(upos t ~pos)
    ;;

    let byteso ?(str_pos = 0) ?len t ~pos src =
      bytes t ~str_pos ~pos src
        ~len:(match len with None -> Bytes.length src - str_pos | Some len -> len)
    ;;

    let stringo ?(str_pos = 0) ?len t ~pos src =
      string t ~str_pos ~pos src
        ~len:(match len with None -> String.length src - str_pos | Some len -> len)
    ;;

    let bigstringo ?(str_pos = 0) ?len t ~pos src =
      bigstring t ~str_pos ~pos src
        ~len:(match len with None -> Bigstring.length src - str_pos | Some len -> len)
    ;;

    let bin_prot = Poke.bin_prot

    open Bigstring

    let [@inline always] char            t ~pos c = bigstring_unsafe_set  t.buf ~pos:(upos t ~pos) c
    let [@inline always] uint8_trunc     t ~pos i = unsafe_set_uint8      t.buf ~pos:(upos t ~pos) i
    let [@inline always] int8_trunc      t ~pos i = unsafe_set_int8       t.buf ~pos:(upos t ~pos) i
    let [@inline always] int16_be_trunc  t ~pos i = unsafe_set_int16_be   t.buf ~pos:(upos t ~pos) i
    let [@inline always] int16_le_trunc  t ~pos i = unsafe_set_int16_le   t.buf ~pos:(upos t ~pos) i
    let [@inline always] uint16_be_trunc t ~pos i = unsafe_set_uint16_be  t.buf ~pos:(upos t ~pos) i
    let [@inline always] uint16_le_trunc t ~pos i = unsafe_set_uint16_le  t.buf ~pos:(upos t ~pos) i
    let [@inline always] int32_be_trunc  t ~pos i = unsafe_set_int32_be   t.buf ~pos:(upos t ~pos) i
    let [@inline always] int32_le_trunc  t ~pos i = unsafe_set_int32_le   t.buf ~pos:(upos t ~pos) i
    let [@inline always] uint32_be_trunc t ~pos i = unsafe_set_uint32_be  t.buf ~pos:(upos t ~pos) i
    let [@inline always] uint32_le_trunc t ~pos i = unsafe_set_uint32_le  t.buf ~pos:(upos t ~pos) i
    let [@inline always] int64_be        t ~pos i = unsafe_set_int64_be   t.buf ~pos:(upos t ~pos) i
    let [@inline always] int64_le        t ~pos i = unsafe_set_int64_le   t.buf ~pos:(upos t ~pos) i
    let [@inline always] uint64_be_trunc t ~pos i = unsafe_set_uint64_be  t.buf ~pos:(upos t ~pos) i
    let [@inline always] uint64_le_trunc t ~pos i = unsafe_set_uint64_le  t.buf ~pos:(upos t ~pos) i
    let [@inline always] int64_t_be      t ~pos i = unsafe_set_int64_t_be t.buf ~pos:(upos t ~pos) i
    let [@inline always] int64_t_le      t ~pos i = unsafe_set_int64_t_le t.buf ~pos:(upos t ~pos) i

    let decimal = Itoa.unsafe_poke_decimal
  end
end

module For_hexdump = struct

  module T2 = struct
    type nonrec ('rw, 'seek) t = ('rw, 'seek) t
  end

  module Window_indexable = struct
    include T2
    let length t = length t
    let get t pos = Peek.char t ~pos
  end

  module Limits_indexable = struct
    include T2
    let length t = t.hi_max - t.lo_min
    let get t pos = Bigstring.get t.buf (t.lo_min + pos)
  end

  module Buffer_indexable = struct
    include T2
    let length t = Bigstring.length t.buf
    let get t pos = Bigstring.get t.buf pos
  end

  module Window = Hexdump.Of_indexable2 (Window_indexable)
  module Limits = Hexdump.Of_indexable2 (Limits_indexable)
  module Buffer = Hexdump.Of_indexable2 (Buffer_indexable)

  module type Relative_indexable = sig
    val name : string
    val lo : (_, _) t -> int
    val hi : (_, _) t -> int
  end

  module type Compound_indexable = sig
    include Hexdump.S2 with type ('rw, 'seek) t := ('rw, 'seek) t
    val parts : (module Relative_indexable) list
  end

  module Make_compound_hexdump (Compound : Compound_indexable) = struct
    module Hexdump = struct
      include T2

      let relative_sequence ?max_lines t (module Relative : Relative_indexable) =
        let lo = Relative.lo t in
        let hi = Relative.hi t in
        Compound.Hexdump.to_sequence ?max_lines ~pos:lo ~len:(hi - lo) t

      let to_sequence ?max_lines t =
        List.concat_map Compound.parts ~f:(fun (module Relative) ->
          [ Sequence.singleton (String.capitalize Relative.name)
          ; relative_sequence ?max_lines t (module Relative)
            |> Sequence.map ~f:(fun line -> "  " ^ line)
          ])
        |> Sequence.of_list
        |> Sequence.concat

      let to_string_hum ?max_lines t =
        to_sequence ?max_lines t
        |> Sequence.to_list
        |> String.concat ~sep:"\n"

      let sexp_of_t _ _ t =
        List.map Compound.parts ~f:(fun (module Relative) ->
          Relative.name, Sequence.to_list (relative_sequence t (module Relative)))
        |> [%sexp_of: (string * string list) list]
    end
  end

  module Window_within_limits = struct
    let name = "window"
    let lo t = t.lo - t.lo_min
    let hi t = t.hi - t.lo_min
  end

  module Limits_within_limits = struct
    let name = "limits"
    let lo _ = 0
    let hi t = t.hi_max - t.lo_min
  end

  module Window_within_buffer = struct
    let name = "window"
    let lo t = t.lo
    let hi t = t.hi
  end

  module Limits_within_buffer = struct
    let name = "limits"
    let lo t = t.lo_min
    let hi t = t.hi_max
  end

  module Buffer_within_buffer = struct
    let name = "buffer"
    let lo _ = 0
    let hi t = Bigstring.length t.buf
  end

  module Window_and_limits = Make_compound_hexdump (struct
      include Limits
      let parts =
        [ (module Window_within_limits : Relative_indexable)
        ; (module Limits_within_limits : Relative_indexable)
        ]
    end)

  module Window_and_limits_and_buffer = Make_compound_hexdump (struct
      include Buffer
      let parts =
        [ (module Window_within_buffer : Relative_indexable)
        ; (module Limits_within_buffer : Relative_indexable)
        ; (module Buffer_within_buffer : Relative_indexable)
        ]
    end)
end

module Window = For_hexdump.Window
module Limits = For_hexdump.Limits
module Debug  = For_hexdump.Window_and_limits_and_buffer

include For_hexdump.Window_and_limits

let to_string_hum = Hexdump.to_string_hum