pack-io 2.1.0

Compact binary wire format with schema evolution and zero-copy deserialization for Rust. The serialization substrate under network-protocol and Hive DB.
Documentation
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
<h1 align="center">
    <img width="99" alt="Rust logo" src="https://raw.githubusercontent.com/jamesgober/rust-collection/72baabd71f00e14aa9184efcb16fa3deddda3a0a/assets/rust-logo.svg">
    <br><b>pack-io</b><br>
    <sub><sup>API REFERENCE</sup></sub>
</h1>
<div align="center">
    <sup>
        <a href="../README.md" title="Project Home"><b>HOME</b></a>
        <span>&nbsp;│&nbsp;</span>
        <a href="../CHANGELOG.md" title="Changelog"><b>CHANGELOG</b></a>
        <span>&nbsp;│&nbsp;</span>
        <span>API</span>
        <span>&nbsp;│&nbsp;</span>
        <a href="./WIRE_FORMAT.md" title="Wire Format Spec"><b>WIRE FORMAT</b></a>
        <span>&nbsp;│&nbsp;</span>
        <a href="./release/" title="Release Notes"><b>RELEASES</b></a>
    </sup>
</div>
<br>

> Reference for every public item in `pack-io`, with runnable examples.
>
> **Status: stable (v2.1.0).** The public surface enumerated below is the
> `2.x` contract: items are only ever added in `2.x`, never removed or
> changed. The wire format ([`docs/WIRE_FORMAT.md`]./WIRE_FORMAT.md,
> spec version 1.3) is unchanged since 1.0 — every `2.x` decoder reads
> every encoding a `1.x` or `2.x` encoder produced for valid data.
> Upgrading from 1.x: [`docs/MIGRATION.md`]./MIGRATION.md. The performance baseline
> ([`docs/PERFORMANCE_BASELINE.md`]./PERFORMANCE_BASELINE.md) is the
> contractual reference; a change exceeding 5 % regression on any row
> blocks the merge. Source-breaking and wire-format-breaking changes wait
> for the next major version.

---

## Table of Contents

- [Installation]#installation
- [Frozen public surface]#frozen-public-surface
- [Overview]#overview
- [Decoding untrusted input]#decoding-untrusted-input
- [Tier 1 — the lazy path]#tier-1--the-lazy-path
  - [`encode`]#encode
  - [`decode` / `decode_with_config`]#decode--decode_with_config
- [Tier 2a — in-memory codec]#tier-2a--in-memory-codec
  - [`Encoder`]#encoder
  - [`Decoder`]#decoder
  - [`Config`]#config
- [Tier 2b — streaming codec]#tier-2b--streaming-codec
  - [`IoEncoder<W>`]#ioencoder
  - [`IoDecoder<R>`]#iodecoder
  - [`encode_into`]#encode_into
  - [`decode_from` / `decode_from_with_config`]#decode_from--decode_from_with_config
- [Tier 3 — the trait surface]#tier-3--the-trait-surface
  - [`Encode` / `Decode` (behaviour traits)]#encode--decode-behaviour-traits
  - [`Serialize` / `Deserialize` (value traits)]#serialize--deserialize-value-traits
  - [`#[derive(Serialize, Deserialize)]`]#derive-serialize-deserialize
  - [Derive attributes]#derive-attributes
- [Zero-copy decode]#zero-copy-decode
  - [`DeserializeView<'a>`]#deserializeview
  - [`decode_view` / `decode_view_with_config`]#decode_view--decode_view_with_config
  - [`Decoder::read_length_prefixed_borrowed` / `read_borrowed`]#decoder-borrowed-reads
  - [`#[derive(DeserializeView)]`]#derive-deserializeview
- [Schema evolution]#schema-evolution
  - [`#[pack_io(version = N)]`]#pack_io-version
  - [`#[pack_io(since = N)]`]#pack_io-since
  - [`#[pack_io(retired = N)]`]#pack_io-retired
  - [`#[pack_io(removed = N)]`]#pack_io-removed
  - [`peek_version`]#peek_version
- [Canonical decoding]#canonical-decoding
- [serde adapter (feature `serde`)]#serde-adapter-feature-serde
- [bytes integration (feature `bytes`)]#bytes-integration-feature-bytes
- [Errors]#errors
  - [`SerialError`]#serialerror
  - [`Result<T>`]#resultt
- [Supported types]#supported-types
- [Wire format]#wire-format
- [Feature flags]#feature-flags
- [Other public items]#other-public-items
- [Cross-platform notes]#cross-platform-notes
- [Compatibility & semver]#compatibility--semver

---

## Installation

```toml
[dependencies]
pack-io = "2"

# With the derive macros (including the schema attributes):
pack-io = { version = "2", features = ["derive"] }
```

`no_std` build (derives, including versioned ones, work here too):

```toml
[dependencies]
pack-io = { version = "2", default-features = false, features = ["derive"] }
```

MSRV is **Rust 1.85** (2024 edition). The CI matrix runs every supported
platform on both stable and MSRV; downstream crates may rely on the declared
`rust-version` in [`Cargo.toml`](../Cargo.toml) for resolver-aware version
selection.

---

## Frozen public surface

The exhaustive list below is the `2.x` contract. Anything not on this
list is an internal detail and may change at any time without a major
version bump. Items are only added within `2.x`. "Since" gives the
release that introduced each item.

### Types

| Path                              | Kind                | Since |
|-----------------------------------|---------------------|-------|
| `pack_io::Encoder`                | concrete struct     | v0.2.0 |
| `pack_io::Decoder<'a>`            | concrete struct     | v0.2.0 |
| `pack_io::Config`                 | `#[non_exhaustive]` struct | v0.2.0 |
| `pack_io::SerialError`            | `#[non_exhaustive]` enum   | v0.2.0 |
| `pack_io::Result<T>`              | type alias          | v0.2.0 |
| `pack_io::IoEncoder<W: Write>`    | concrete struct     | v0.3.0 |
| `pack_io::IoDecoder<R: Read>`     | concrete struct     | v0.3.0 |
| `pack_io::serde::Compat<T>` *(feature `serde`)* | wrapper struct | v2.0.0 |

### Traits

| Path                              | Methods | Since |
|-----------------------------------|---------|-------|
| `pack_io::Serialize`              | `serialize`, `serialize_slice` (default) | v0.6.0 |
|                                   | `encoded_len`, `encoded_len_slice` (default) | v2.0.0 |
| `pack_io::Deserialize`            | `deserialize`, `deserialize_many` (default) | v0.6.0 |
|                                   | `deserialize_array` (default) | v2.0.0 |
| `pack_io::DeserializeView<'a>`    | `deserialize_view` | v0.4.0 |
|                                   | `deserialize_view_many`, `deserialize_view_array` (default) | v2.0.0 |
| `pack_io::Encode`                 | `write_byte`, `write_bytes`, `reserve`, `write_varint_u64`, `write_varint_u128` | v0.3.0 |
| `pack_io::Decode`                 | `read_byte`, `read_into`, `max_alloc`, `read_varint_u64`, `read_varint_u128`, `read_length_prefixed` | v0.3.0 |
|                                   | `remaining_hint`, `config`, `depth`, `enter_nested`, `leave_nested`, `consumed`, `bytes_read`, `unbacked`, `charge_unbacked`, `read_byte_cow` (default) | v2.0.0 |
|                                   | `reset_unbacked` (default) | v2.1.0 |

Every method added in 2.0 has a default implementation, so existing
implementations of these traits keep compiling unchanged.

### Free functions

| Path                              | Since |
|-----------------------------------|-------|
| `pack_io::encode`                 | v0.2.0 |
| `pack_io::decode`                 | v0.2.0 |
| `pack_io::decode_view`            | v0.4.0 |
| `pack_io::encode_into`            | v0.3.0 |
| `pack_io::decode_from`            | v0.3.0 |
| `pack_io::peek_version`           | v0.5.0 |
| `pack_io::decode_with_config`     | v2.0.0 |
| `pack_io::decode_view_with_config`| v2.0.0 |
| `pack_io::decode_from_with_config`| v2.0.0 |
| `pack_io::varint_len` (`const fn`)| v2.0.0 |
| `pack_io::serde::{to_vec, to_encoder, from_slice, from_slice_with_config, from_decoder, from_decode}` *(feature `serde`)* | v2.0.0 |
| `pack_io::serde::compat::{serialize, deserialize, deserialize_view}` *(feature `serde`)* | v2.0.0 |
| `pack_io::serde::sorted_set::{serialize, deserialize}` *(feature `serde`)* | v2.0.0 |

### Constants

| Path                              | Since |
|-----------------------------------|-------|
| `pack_io::VERSION`                | v0.1.0 |
| `pack_io::Config::DEFAULT_MAX_ALLOC` | v2.0.0 |
| `pack_io::Config::DEFAULT_MAX_DEPTH` | v2.0.0 |

### Inherent methods on `Encoder`

| Method                            | Since |
|-----------------------------------|-------|
| `Encoder::new`                    | v0.2.0 |
| `Encoder::with_capacity`          | v0.6.0 |
| `Encoder::into_buffer`            | v0.2.0 |
| `Encoder::as_bytes`               | v0.2.0 |
| `Encoder::into_inner`             | v0.2.0 |
| `Encoder::take`                   | v0.2.0 |
| `Encoder::write`                  | v0.2.0 |

### Inherent methods on `Decoder<'a>`

| Method                                       | Since |
|----------------------------------------------|-------|
| `Decoder::new`                               | v0.2.0 |
| `Decoder::with_config`                       | v0.2.0 |
| `Decoder::position`                          | v0.2.0 |
| `Decoder::remaining`                         | v0.2.0 |
| `Decoder::is_empty`                          | v0.2.0 |
| `Decoder::read`                              | v0.2.0 |
| `Decoder::read_length_prefixed_borrowed`     | v0.4.0 |
| `Decoder::read_borrowed`                     | v2.0.0 |
| `Decoder::config`                            | v2.0.0 |

### Inherent methods on `IoEncoder<W>` / `IoDecoder<R>`

| Method                                       | Since |
|----------------------------------------------|-------|
| `IoEncoder::new`, `writer`, `writer_mut`, `into_inner`, `write` | v0.3.0 |
| `IoDecoder::new`, `with_config`, `reader`, `into_inner`, `read` | v0.3.0 |
| `IoDecoder::config`                          | v2.0.0 |

### `Config`

| Item                              | Since |
|-----------------------------------|-------|
| field `max_alloc`, `Config::new` (`const fn`), `Config::with_max_alloc` (`const fn`) | v0.2.0 |
| field `max_depth`, `Config::with_max_depth` (`const fn`) | v2.0.0 |
| field `canonical`, `Config::with_canonical` (`const fn`) | v2.0.0 |
| field `max_total_alloc`, `Config::with_max_total_alloc`, `Config::total_alloc_limit`, `Config::network` (all `const fn`), `Config::NETWORK_MAX_ALLOC` | v2.1.0 |

### `SerialError` methods

| Method                            | Since |
|-----------------------------------|-------|
| `SerialError::custom`             | v2.0.0 |

### Re-exported derive macros (feature `derive`)

| Path                              | Since |
|-----------------------------------|-------|
| `#[derive(pack_io::Serialize)]`   | v0.4.0 |
| `#[derive(pack_io::Deserialize)]` | v0.4.0 |
| `#[derive(pack_io::DeserializeView)]` | v0.4.0 (lifetime-less types: v2.0.0) |

### Derive attributes (feature `derive`)

| Attribute                                  | Where | Since |
|--------------------------------------------|-------|-------|
| `#[pack_io(version = N)]`                  | struct | v0.5.0 |
| `#[pack_io(since = N)]`                    | field of a versioned struct | v0.5.0 |
| `#[pack_io(retired = N)]`                  | field of a versioned struct | v2.0.0 |
| `#[pack_io(removed = N)]`                  | field of a versioned struct | v2.0.0 (the v0.5.0 `deprecated` encoding; `deprecated` itself is a compile error since v2.0.0) |
| `#[pack_io(crate = "path")]`               | struct / enum | v2.0.0 |
| `#[pack_io(skip)]`                         | field | v2.0.0 |
| `#[pack_io(default)]` / `#[pack_io(default = "path")]` | field | v2.0.0 |
| `#[pack_io(with = "module")]`              | field | v2.0.0 |
| `#[pack_io(index = N)]`                    | enum variant | v2.0.0 |

### `SerialError` variants

| Variant                                          | Since |
|--------------------------------------------------|-------|
| `UnexpectedEof { needed, remaining }`            | v0.2.0 |
| `InvalidLength { declared, remaining }`          | v0.2.0 |
| `VarintOverflow`                                 | v0.2.0 |
| `IntegerOutOfRange`                              | v0.2.0 |
| `InvalidBool { byte }`                           | v0.2.0 |
| `InvalidUtf8`                                    | v0.2.0 |
| `InvalidTag { kind, tag }`                       | v0.2.0 |
| `TrailingBytes { remaining }`                    | v0.2.0 |
| `UnknownVariant { kind, index }`                 | v0.4.0 |
| `Io { kind, message }` *(feature `std`)*         | v0.3.0 |
| `DepthLimitExceeded { max_depth }`               | v2.0.0 |
| `NonCanonical { reason }`                        | v2.0.0 |
| `InvalidVersion { version }`                     | v2.0.0 |
| `InvalidValue { kind }`                          | v2.0.0 |
| `InvalidConfig { reason }`                       | v2.0.0 |
| `Custom { message }`                             | v2.0.0 |

`SerialError` is `#[non_exhaustive]`, so callers must include a wildcard
`match` arm. New variants may be added in backwards-compatible MINOR
releases.

### Feature flags

| Feature  | Default | Since |
|----------|---------|-------|
| `std`    | yes     | v0.1.0 |
| `derive` | no      | v0.4.0 |
| `schema` | no      | v0.5.0 (alias for `derive`) |
| `serde`  | no      | v0.1.0 (adapter implemented in v2.0.0) |
| `bytes`  | no      | v2.0.0 |

All feature flags are **additive**. Enabling a feature never removes or
changes existing behaviour; disabling a feature never breaks code that
did not opt into it.

### Out of the public surface (intentional)

- The `pack-io-derive` crate is an implementation detail. Depending on
  it directly is unsupported — the version pin is exact (`=X.Y.Z`)
  precisely to prevent users from getting a different revision than
  the parent crate expects. The two crates are always released together.
- `pack_io::__private` exists only for derive-generated code. It is
  hidden from the docs and may change in any release.
- The `varint` module is `pub(crate)`. The wire-format spec at
  [`docs/WIRE_FORMAT.md`]./WIRE_FORMAT.md defines the LEB128 layout
  normatively; `pack_io::varint_len` is the one public helper.
- Test-only and benchmark-only items in `tests/` and `benches/` are
  not part of the public surface.

---

## Overview

`pack-io` exposes a compact binary codec in three layers:

- **Tier 1** — the [`encode`]#encode / [`decode`]#decode--decode_with_config
  free functions. One line each direction, no setup, no type parameters
  the caller has to name beyond the target type.
- **Tier 2** — concrete encoder / decoder pairs:
  - **2a, in-memory**: [`Encoder`]#encoder + [`Decoder`]#decoder,
    backed by `Vec<u8>` / `&[u8]`. Best when the whole payload already
    lives in memory.
  - **2b, streaming**: [`IoEncoder<W>`]#ioencoder +
    [`IoDecoder<R>`]#iodecoder, wrapping any `std::io::Write` / `Read`.
    Plus [`encode_into`]#encode_into /
    [`decode_from`]#decode_from--decode_from_with_config convenience
    helpers. Gated on the default `std` feature.
- **Tier 3** — the [`Serialize`]#serialize--deserialize-value-traits and
  [`Deserialize`]#serialize--deserialize-value-traits traits implemented
  on your own types, by hand or with the derive macros. Both are generic
  over the [`Encode`]#encode--decode-behaviour-traits and
  [`Decode`]#encode--decode-behaviour-traits behaviour traits, so a
  single impl works through every concrete encoder / decoder the crate
  ships.

Properties that hold across every release:

- The encode path never allocates beyond the output buffer, except for
  maps and sets, which encode their entries into one scratch buffer to
  sort them.
- Encoding is **deterministic** — the same value always produces the same
  bytes, regardless of platform, insertion order, hash randomisation, or
  build flags.
- Decoding is **safe under untrusted input** — see the next section.

The normative byte-level spec lives in
[`docs/WIRE_FORMAT.md`](./WIRE_FORMAT.md).

---

## Decoding untrusted input

Every decoder in the crate enforces these limits on any byte sequence:

| Guarantee | Mechanism |
|---|---|
| No panic, no read past the input | every read is bounds-checked and returns `Result` |
| No single value larger than `max_alloc` | length prefixes and element counts are checked against [`Config::max_alloc`]#config (default 1 GiB) |
| No length-prefixed allocation the input cannot back | in-memory decoders compare declared lengths with the remaining input before allocating; streaming decoders grow buffers in bounded steps as bytes arrive; collection pre-allocation is capped by the remaining input and 64 KiB (4 KiB on a stream) |
| The memory one decoded message builds is bounded | every collection slot (`size_of::<T>()` per element, charged before the elements decode), every owned `String` / `Vec<u8>` / `Bytes`, and every `Box<T>` / `Rc<T>` / `Arc<T>` is charged against one running total, capped by [`Config::max_total_alloc`]#config (default: the same as `max_alloc`), versioned bodies included |
| Element loops are bounded | each collection element that consumed no input (`()`, `Box<()>`, `Rc<()>`, a struct whose fields are all `skip`) is charged at least 64 bytes against the same total, so a few bytes of input cannot run a long loop |
| No stack overflow on nested input | each derived type counts one level against [`Config::max_depth`]#config (default 128) |
| Versioned bodies obey the caller's limits | the body is decoded under the same `Config` and nesting depth as the enclosing value |
| Optional: one byte string per value | [`Config::canonical`]#canonical-decoding rejects every non-canonical encoding |

Memory is not simply proportional to input length: `None` of an
`Option<[u8; 4096]>` is one byte on the wire and 4097 in a `Vec`. That is
why the budget charges in-memory sizes, not input bytes. What a decode
holds is at most a small constant times `total_alloc_limit()`: a growing
`Vec` can briefly hold twice its length, and hash and tree collections add
their own per-entry overhead on top of the slot size.

The total covers one value. Each `Decoder::read` / `IoDecoder::read` and
each free function starts a fresh budget, so a decoder reading message
after message from a socket gives every message the same limit. Code that
calls `T::deserialize` directly on a long-lived decoder calls
`Decode::reset_unbacked` between messages.

The 1 GiB default suits files and trusted data. For bytes from a peer,
start from `Config::network()` (16 MiB per value and per message) and
tighten it to what your protocol needs:

```rust
use pack_io::{Config, decode_with_config};

let cfg = Config::network()
    .with_max_alloc(64 * 1024)
    .with_max_total_alloc(1 << 20)
    .with_max_depth(32);
let bytes = pack_io::encode(&vec![1u32, 2, 3]).unwrap();
let v: Vec<u32> = decode_with_config(&bytes, cfg).unwrap();
assert_eq!(v, [1, 2, 3]);
```

---

## Tier 1 — the lazy path

### `encode`

```rust,ignore
pub fn encode<T: Serialize + ?Sized>(value: &T) -> Result<Vec<u8>>;
```

Encode `value` into a freshly allocated `Vec<u8>`. The buffer starts with
a 512-byte capacity so that typical messages encode with one allocation;
for a value you will store for a long time, size the buffer exactly with
[`Serialize::encoded_len`](#serialize--deserialize-value-traits) instead
(see the example there).

**Example:**

```rust
let bytes = pack_io::encode(&42_u64).unwrap();
let back: u64 = pack_io::decode(&bytes).unwrap();
assert_eq!(back, 42);
```

**Example — heterogeneous tuple:**

```rust
let v = (1_u64, true, String::from("hello"));
let bytes = pack_io::encode(&v).unwrap();
let back: (u64, bool, String) = pack_io::decode(&bytes).unwrap();
assert_eq!(back, v);
```

### `decode` / `decode_with_config`

```rust,ignore
pub fn decode<T: Deserialize>(bytes: &[u8]) -> Result<T>;
pub fn decode_with_config<T: Deserialize>(bytes: &[u8], config: Config) -> Result<T>;
```

Decode a value of type `T` from `bytes`, requiring the input to be fully
consumed. Trailing input is rejected with
[`SerialError::TrailingBytes`](#serialerror). `decode` uses
[`Config::default()`](#config); `decode_with_config` validates and uses
the given config.

**Example — strict decode rejects trailing bytes:**

```rust
let mut bytes = pack_io::encode(&7_u8).unwrap();
bytes.push(0xff);
let err = pack_io::decode::<u8>(&bytes).unwrap_err();
assert!(matches!(err, pack_io::SerialError::TrailingBytes { remaining: 1 }));
```

**Example — a tight cap for one call:**

```rust
use pack_io::{Config, SerialError, decode_with_config, encode};

let bytes = encode(&"x".repeat(100)).unwrap();
let cfg = Config::new().with_max_alloc(16);
let err = decode_with_config::<String>(&bytes, cfg).unwrap_err();
assert!(matches!(err, SerialError::InvalidLength { declared: 100, .. }));
```

---

## Tier 2a — in-memory codec

### `Encoder`

In-memory encoder. Writes into an owned `Vec<u8>`; the buffer can be
swapped in / out so a single allocation serves many encodes.

| Constructor                         | Purpose                                          |
|-------------------------------------|--------------------------------------------------|
| `Encoder::new()`                    | Empty buffer.                                    |
| `Encoder::with_capacity(n)`         | Pre-sized buffer (pair with `encoded_len`).      |
| `Encoder::into_buffer(buf)`         | Append into the caller's `Vec<u8>` (no alloc).   |
| `Encoder::default()`                | Same as `new()`.                                 |

| Method                  | Returns       | Purpose                                  |
|-------------------------|---------------|------------------------------------------|
| `write(&value)`         | `Result<()>`  | Append `value`'s wire-format bytes.      |
| `as_bytes()`            | `&[u8]`       | Borrow the bytes written so far.         |
| `into_inner()`          | `Vec<u8>`     | Consume the encoder, return the buf.     |
| `take()`                | `Vec<u8>`     | Swap in an empty buffer, return the old. |

**Example — write three values into one buffer:**

```rust
use pack_io::Encoder;

let mut enc = Encoder::new();
enc.write(&7_u64).unwrap();
enc.write(&true).unwrap();
enc.write(&"hello").unwrap();
let bytes = enc.into_inner();
assert!(bytes.len() > 0);
```

### `Decoder`

Cursored decoder. Borrows from an input slice and advances a position
pointer as values are read. Bounds-checked on every operation.

| Constructor                              | Purpose                                              |
|------------------------------------------|------------------------------------------------------|
| `Decoder::new(bytes)`                    | Default config (1 GiB `max_alloc`, depth 128, lenient). |
| `Decoder::with_config(bytes, cfg)`       | Validates `cfg` (see [`Config`]#config).           |

| Method         | Returns          | Purpose                                  |
|----------------|------------------|------------------------------------------|
| `read::<T>()`  | `Result<T>`      | Decode one value of `T`, advance cursor. |
| `read_borrowed(n)` | `Result<&'a [u8]>` | Borrow the next `n` bytes (no prefix). |
| `position()`   | `usize`          | Bytes consumed so far.                   |
| `remaining()`  | `usize`          | Bytes left in the input.                 |
| `is_empty()`   | `bool`           | `remaining() == 0`.                      |
| `config()`     | `Config`         | The limits this decoder enforces.        |

**Example — read several values from one buffer:**

```rust
use pack_io::{Encoder, Decoder};

let mut enc = Encoder::new();
enc.write(&7_u64).unwrap();
enc.write(&true).unwrap();
let bytes = enc.into_inner();

let mut dec = Decoder::new(&bytes);
let n: u64 = dec.read().unwrap();
let b: bool = dec.read().unwrap();
assert!(dec.is_empty());
assert_eq!((n, b), (7, true));
```

### `Config`

```rust,ignore
#[non_exhaustive]
pub struct Config {
    pub max_alloc: usize,               // default 1 GiB (Config::DEFAULT_MAX_ALLOC)
    pub max_total_alloc: Option<usize>, // default None: same as max_alloc
    pub max_depth: usize,               // default 128   (Config::DEFAULT_MAX_DEPTH)
    pub canonical: bool,                // default false
}
```

`#[non_exhaustive]` so MINOR releases can add knobs without breaking
downstream code. Build it with the `const fn` builders:

| Constructor / builder                | Purpose                                            |
|--------------------------------------|----------------------------------------------------|
| `Config::new()` / `Config::default()`| The defaults above.                                |
| `Config::network()`                  | Limits for peer input: 16 MiB per value and per message (`Config::NETWORK_MAX_ALLOC`). |
| `with_max_alloc(n)`                  | Cap on any single length-prefixed value.           |
| `with_max_total_alloc(n)`            | Cap on the memory one decoded value builds in total. |
| `total_alloc_limit()`                | The total cap in force: `max_total_alloc`, or `max_alloc` when unset. |
| `with_max_depth(n)`                  | Cap on nesting of derived types.                   |
| `with_canonical(bool)`               | Strict canonical decoding (see [Canonical decoding]#canonical-decoding). |

Validation happens once, when a decoder is built: `max_alloc == 0`,
`max_total_alloc == Some(0)` and
`max_depth == 0` are rejected with `InvalidConfig` (1.x reported a zero
`max_alloc` as `InvalidLength { declared: 0, .. }`).

`max_depth` counts derived values: each `#[derive(Deserialize)]` /
`#[derive(DeserializeView)]` struct or enum is one level while its fields
decode. Built-in containers do not count (they cannot recurse without a
user type). The default of 128 is the `serde_json` default; measured on
Windows x86_64 (Rust 1.95, debug build), a five-field recursive struct
uses about 3 KiB of stack per level, so 128 levels fit in a 1 MiB thread
stack with room to spare. Raise it only for data you trust to be that
deep, and lower it for very large structs decoded on small stacks.

**Example — limits for untrusted input:**

```rust
let cfg = pack_io::Config::new()
    .with_max_alloc(16 * 1024)
    .with_max_depth(16);
assert_eq!((cfg.max_alloc, cfg.max_depth), (16 * 1024, 16));
```

---

## Tier 2b — streaming codec

The streaming pair (`std` feature, default-on) wraps any `std::io::Write`
or `Read`. Bytes are written through to the underlying sink as they are
produced — useful for sockets, files, and pipes where buffering the entire
payload in memory is undesirable. Both flavours surface I/O failure
through [`SerialError::Io`](#serialerror). Neither buffers internally:
wrap raw sockets and files in `BufWriter` / `BufReader`.

### `IoEncoder`

```rust,ignore
pub struct IoEncoder<W: Write> { /* … */ }
```

| Constructor                    | Purpose                                          |
|--------------------------------|--------------------------------------------------|
| `IoEncoder::new(writer)`       | Wrap any `std::io::Write`.                       |

| Method               | Returns       | Purpose                                  |
|----------------------|---------------|------------------------------------------|
| `write(&value)`      | `Result<()>`  | Encode straight into the writer.         |
| `writer()`           | `&W`          | Borrow the underlying writer.            |
| `writer_mut()`       | `&mut W`      | Borrow the writer mutably.               |
| `into_inner()`       | `W`           | Consume the encoder, return the writer.  |

**Example:**

```rust
use pack_io::IoEncoder;

let mut sink: Vec<u8> = Vec::new();
let mut enc = IoEncoder::new(&mut sink);
enc.write(&42_u64).unwrap();
enc.write(&"hello").unwrap();
assert!(!sink.is_empty());
```

### `IoDecoder`

```rust,ignore
pub struct IoDecoder<R: Read> { /* … */ }
```

| Constructor                              | Purpose                                              |
|------------------------------------------|------------------------------------------------------|
| `IoDecoder::new(reader)`                 | Default `Config`.                                    |
| `IoDecoder::with_config(reader, cfg)`    | Validates `cfg`.                                     |

| Method               | Returns       | Purpose                                  |
|----------------------|---------------|------------------------------------------|
| `read::<T>()`        | `Result<T>`   | Decode one value of `T` from the reader. |
| `reader()`           | `&R`          | Borrow the underlying reader.            |
| `into_inner()`       | `R`           | Consume the decoder, return the reader.  |
| `config()`           | `Config`      | The limits this decoder enforces.        |

A streaming decoder cannot know how much input is left, so it never
sizes a buffer from a declared length: it reads in chunks (64 KiB, then
doubling) as the bytes arrive. A peer that declares a 1 GiB string and
then stalls costs one chunk, not 1 GiB. Canonical mode needs the raw
input to compare map keys, which a stream does not keep; read the frame
into memory and use [`decode_with_config`](#decode--decode_with_config)
(or [`decode_from_with_config`](#decode_from--decode_from_with_config)).

**Example — round-trip through a `Cursor<Vec<u8>>`:**

```rust
use pack_io::{IoEncoder, IoDecoder};
use std::io::Cursor;

let mut buf: Vec<u8> = Vec::new();
{
    let mut enc = IoEncoder::new(&mut buf);
    enc.write(&1_u64).unwrap();
    enc.write(&2_u64).unwrap();
}
let mut dec = IoDecoder::new(Cursor::new(buf));
let a: u64 = dec.read().unwrap();
let b: u64 = dec.read().unwrap();
assert_eq!((a, b), (1, 2));
```

### `encode_into`

```rust,ignore
pub fn encode_into<T: Serialize + ?Sized, W: Write>(
    value: &T, writer: &mut W,
) -> Result<()>;
```

Single-shot convenience wrapper around [`IoEncoder::write`](#ioencoder).

**Example:**

```rust
use pack_io::encode_into;

let mut buf: Vec<u8> = Vec::new();
encode_into(&(7_u64, "hello"), &mut buf).unwrap();
assert!(!buf.is_empty());
```

### `decode_from` / `decode_from_with_config`

```rust,ignore
pub fn decode_from<T: Deserialize, R: Read>(reader: &mut R) -> Result<T>;
pub fn decode_from_with_config<T: Deserialize, R: Read>(reader: &mut R, config: Config) -> Result<T>;
```

Read the rest of `reader` into memory — **at most `max_alloc` bytes** —
and decode it as a single value of type `T`. A longer stream is cut off
and rejected with [`SerialError::InvalidLength`](#serialerror) (1.0 read
without limit). Trailing bytes after the value are rejected with
[`SerialError::TrailingBytes`](#serialerror). Because the frame ends up in
memory, every `Config` knob applies, canonical mode included.

**Example:**

```rust
use pack_io::{encode, decode_from};
use std::io::Cursor;

let bytes = encode(&42_u64).unwrap();
let n: u64 = decode_from(&mut Cursor::new(bytes)).unwrap();
assert_eq!(n, 42);
```

---

## Tier 3 — the trait surface

### `Encode` / `Decode` (behaviour traits)

```rust,ignore
pub trait Encode {
    fn write_byte(&mut self, byte: u8) -> Result<()>;
    fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>;
    fn reserve(&mut self, _additional: usize) { /* default: no-op */ }
    fn write_varint_u64(&mut self, value: u64) -> Result<()> { /* default */ }
    fn write_varint_u128(&mut self, value: u128) -> Result<()> { /* default */ }
}

pub trait Decode {
    fn read_byte(&mut self) -> Result<u8>;
    fn read_into(&mut self, out: &mut [u8]) -> Result<()>;
    fn max_alloc(&self) -> usize;
    // Provided; every one has a correct, bounded default:
    fn remaining_hint(&self) -> Option<usize>;
    fn config(&self) -> Config;
    fn depth(&self) -> usize;
    fn enter_nested(&mut self) -> Result<()>;
    fn leave_nested(&mut self);
    fn consumed(&self) -> Option<&[u8]>;
    fn bytes_read(&self) -> Option<u64>;
    fn unbacked(&self) -> usize;
    fn charge_unbacked(&mut self, bytes: usize) -> Result<()>;
    fn read_byte_cow(&mut self, len: usize) -> Result<Cow<'_, [u8]>>;
    fn read_varint_u64(&mut self) -> Result<u64>;
    fn read_varint_u128(&mut self) -> Result<u128>;
    fn read_length_prefixed(&mut self) -> Result<Vec<u8>>;
}
```

The behavioural seam — implemented by every concrete encoder / decoder in
the crate (and, with the `bytes` feature, `Encode` by `BytesMut`). User
code rarely implements these directly; they exist so that
[`Serialize`](#serialize--deserialize-value-traits) and
[`Deserialize`](#serialize--deserialize-value-traits) impls can be written
generically and work through both the in-memory and streaming codec
flavours unchanged.

A custom `Decode` needs only the three required methods. It should also
override `remaining_hint` when it knows how much input is left (so
collection pre-allocation is bounded by it), the depth methods to
enforce `max_depth`, `consumed` if it keeps its input in memory (so
canonical mode can check map key order), and `unbacked` /
`charge_unbacked` / `reset_unbacked` plus `bytes_read` so that the memory
one value builds is bounded by `total_alloc_limit()` and elements that
occupy no input bytes are detected (`bytes_read` already works through
`consumed`; without a running total, `charge_unbacked` refuses only a
single charge above the limit, and without `bytes_read` no element can be
detected as free).

Hand-written `Deserialize` impls for **recursive** types should call
`enter_nested` / `leave_nested` around their body (derived impls do):

```rust
use pack_io::{Decode, Deserialize, Result};

struct Tree(Vec<Tree>);

impl Deserialize for Tree {
    fn deserialize<D: Decode + ?Sized>(dec: &mut D) -> Result<Self> {
        dec.enter_nested()?;
        let kids = Vec::<Tree>::deserialize(dec);
        dec.leave_nested(); // on the error path too
        Ok(Tree(kids?))
    }
}

let hostile = vec![1u8; 10_000]; // 10,000 levels of `Tree([Tree([...])])`
assert!(matches!(
    pack_io::decode::<Tree>(&hostile),
    Err(pack_io::SerialError::DepthLimitExceeded { .. })
));
```

### `Serialize` / `Deserialize` (value traits)

```rust,ignore
pub trait Serialize {
    fn serialize<E: Encode + ?Sized>(&self, encoder: &mut E) -> Result<()>;
    fn serialize_slice<E: Encode + ?Sized>(slice: &[Self], encoder: &mut E) -> Result<()> where Self: Sized;
    fn encoded_len(&self) -> Result<usize>;
    fn encoded_len_slice(slice: &[Self]) -> Result<usize> where Self: Sized;
}

pub trait Deserialize: Sized {
    fn deserialize<D: Decode + ?Sized>(decoder: &mut D) -> Result<Self>;
    fn deserialize_many<D: Decode + ?Sized>(decoder: &mut D, count: usize) -> Result<Vec<Self>>;
    fn deserialize_array<D: Decode + ?Sized, const N: usize>(decoder: &mut D) -> Result<[Self; N]>;
}
```

**Contract:**

- `serialize` MUST be deterministic: equal values produce equal bytes.
- `serialize` appends to the encoder's buffer / sink; it does not clear
  what's already there.
- `encoded_len` MUST return exactly the number of bytes `serialize`
  writes. The default computes it by running `serialize` against a
  counting sink; the built-in impls and the derive use direct arithmetic.
  Versioned structs write their body length from it, so an override that
  miscounts corrupts the frame.
- `deserialize` consumes exactly the bytes a corresponding `serialize`
  would have produced.
- On malformed input, `deserialize` MUST return an error and MUST NOT
  panic, allocate more than the input can back, or read past the
  underlying source.

**Example — implement both traits for a custom struct:**

```rust
use pack_io::{Decode, Decoder, Deserialize, Encode, Encoder, Result, Serialize};

#[derive(Debug, PartialEq)]
struct Point { x: i32, y: i32 }

impl Serialize for Point {
    fn serialize<E: Encode + ?Sized>(&self, enc: &mut E) -> Result<()> {
        self.x.serialize(enc)?;
        self.y.serialize(enc)
    }
}

impl Deserialize for Point {
    fn deserialize<D: Decode + ?Sized>(dec: &mut D) -> Result<Self> {
        Ok(Point {
            x: i32::deserialize(dec)?,
            y: i32::deserialize(dec)?,
        })
    }
}

let p = Point { x: 3, y: -7 };
let bytes = pack_io::encode(&p).unwrap();
let back: Point = pack_io::decode(&bytes).unwrap();
assert_eq!(back, p);
assert_eq!(p.encoded_len().unwrap(), bytes.len());
```

**Example — exactly-sized output for long-lived storage:**

```rust
use pack_io::{Encoder, Serialize};

let value = (7_u64, String::from("stored key"));
let mut enc = Encoder::with_capacity(value.encoded_len().unwrap());
value.serialize(&mut enc).unwrap();
let bytes = enc.into_inner();
assert_eq!(bytes.len(), bytes.capacity());
```

**Example — the same `Point` impl works through `IoEncoder<File>`:**

```rust,no_run
# use pack_io::{Decode, Encode, Result, Serialize, Deserialize};
# #[derive(PartialEq, Debug)] struct Point { x: i32, y: i32 }
# impl Serialize for Point {
#     fn serialize<E: Encode + ?Sized>(&self, e: &mut E) -> Result<()> {
#         self.x.serialize(e)?;
#         self.y.serialize(e)
#     }
# }
# impl Deserialize for Point {
#     fn deserialize<D: Decode + ?Sized>(d: &mut D) -> Result<Self> {
#         Ok(Point { x: i32::deserialize(d)?, y: i32::deserialize(d)? })
#     }
# }
use pack_io::IoEncoder;
use std::fs::File;

let file = File::create("point.pack").unwrap();
let mut enc = IoEncoder::new(file);
enc.write(&Point { x: 3, y: -7 }).unwrap();
```

### `#[derive(Serialize, Deserialize)]`

```rust,ignore
use pack_io::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Account { id: u64, handle: String, active: bool }

#[derive(Serialize, Deserialize)]
enum Event {
    Heartbeat,
    Login { user: u64, ip: String },
    Error(u32, String),
}
```

The derive macros (feature `derive`, default off) write `Serialize` and
`Deserialize` impls for any struct (named, tuple, unit) and any non-empty
enum (any variant shape), generic over type parameters. The generated
code uses only `::core` and the `pack_io` crate path, so it compiles in
`#![no_std]` crates.

Field order in the source code is the encoded byte order. Enums are
encoded as `varint(variant_index) ++ fields`, where `variant_index` is
the variant's source-declaration position starting at `0` unless an
explicit [`index`](#derive-attributes) says otherwise. **Append new
variants to the end** of an enum declaration (or give variants explicit
indices) to preserve wire-format compatibility.

Unknown variant indices on decode surface as
[`SerialError::UnknownVariant`](#serialerror) carrying the real index.
Every derived `Deserialize` counts one level against `max_depth`.

The macros are re-exported at `pack_io::{Serialize, Deserialize,
DeserializeView}` — the underlying [`pack-io-derive`](https://crates.io/crates/pack-io-derive)
proc-macro crate is an implementation detail.

### Derive attributes

Every attribute lives under `#[pack_io(...)]`. Anything unknown,
duplicated, or used in the wrong place is a compile error pointing at
the attribute (1.0 silently ignored attributes on enum fields and
variants).

| Attribute | Where | Effect |
|---|---|---|
| `version = N` | struct | versioned frame, see [Schema evolution]#schema-evolution; `N >= 1` |
| `since = N` | field of a versioned struct | field added at version `N`; `N` must not exceed the struct's `version` |
| `retired = N` | field of a versioned struct | from version `N` the slot stays on the wire carrying the default value, and readers ignore it; safe with every reader; `N > since` |
| `removed = N` | field of a versioned struct | the field's bytes leave the wire at version `N` (1.x `deprecated`); roll readers out first; `N > since` |
| `skip` | any field | never encoded; decoded as its default |
| `default` / `default = "path"` | any field with `skip`, `since`, `retired` or `removed` | value used when the field is absent or retired: `Default::default()` or `path()` |
| `with = "module"` | any field | encode with `module::serialize(&field, encoder)`, decode with `module::deserialize(decoder)`, and in views `module::deserialize_view(decoder)` |
| `crate = "path"` | struct / enum | path to `pack_io` in generated code (default `::pack_io`) — for crates that re-export pack-io |
| `index = N` | enum variant | explicit wire tag; later variants without one continue from `N + 1`; duplicates are rejected |

```rust
use pack_io::{Decode, Deserialize, Encode, Result, Serialize};

/// Store a `u32` as four little-endian bytes instead of a varint.
mod fixed_u32 {
    use pack_io::{Decode, Encode, Result};
    pub fn serialize<E: Encode + ?Sized>(v: &u32, e: &mut E) -> Result<()> {
        e.write_bytes(&v.to_le_bytes())
    }
    pub fn deserialize<D: Decode + ?Sized>(d: &mut D) -> Result<u32> {
        let mut b = [0u8; 4];
        d.read_into(&mut b)?;
        Ok(u32::from_le_bytes(b))
    }
}

fn unknown() -> String { "unknown".into() }

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Header {
    #[pack_io(with = "fixed_u32")]
    magic: u32,
    #[pack_io(skip, default = "unknown")]
    source: String, // local only, never on the wire
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
enum Op {
    #[pack_io(index = 10)]
    Get,
    Put, // 11
}

let h = Header { magic: 0xCAFE_BABE, source: "socket".into() };
let bytes = pack_io::encode(&(h, Op::Put)).unwrap();
assert_eq!(bytes, [0xBE, 0xBA, 0xFE, 0xCA, 11]);
let (back, op): (Header, Op) = pack_io::decode(&bytes).unwrap();
assert_eq!((back.source.as_str(), op), ("unknown", Op::Put));
```

`Box<T>` encodes like `T`, so recursive types derive directly, and the
depth limit still applies:

```rust
use pack_io::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct List {
    value: u8,
    next: Option<Box<List>>,
}

let list = List { value: 1, next: Some(Box::new(List { value: 2, next: None })) };
assert_eq!(pack_io::encode(&list).unwrap(), [1, 1, 2, 0]);
```

---

## Zero-copy decode

The owning [`Deserialize`](#serialize--deserialize-value-traits) surface
allocates `String`s and `Vec<u8>`s during decode. The zero-copy
[`DeserializeView`](#deserializeview) surface returns `&'a str` / `&'a [u8]`
that borrow directly from the input slice — no per-field allocation. Both
surfaces use the **same on-wire format** and enforce the same `Config`
limits; choose the surface that matches the lifetime relationship the
caller has with the source bytes.

On a representative borrow-heavy record, the view decode runs about
**2.3×** faster than the owning decode on the measurement machine (25 ns
against 57 ns; see
[`docs/PERFORMANCE_BASELINE.md`](./PERFORMANCE_BASELINE.md)). Reproduce with
`cargo bench --bench codec_bench --features derive`.

### `DeserializeView`

```rust,ignore
pub trait DeserializeView<'a>: Sized {
    fn deserialize_view(decoder: &mut Decoder<'a>) -> Result<Self>;
    fn deserialize_view_many(decoder: &mut Decoder<'a>, count: usize) -> Result<Vec<Self>>;
    fn deserialize_view_array<const N: usize>(decoder: &mut Decoder<'a>) -> Result<[Self; N]>;
}
```

The borrowed counterpart to [`Deserialize`](#serialize--deserialize-value-traits).
`'a` is the lifetime of the input buffer; the borrow checker guarantees
the decoded value cannot outlive its source.

Built-in implementors:

- `&'a str`, `&'a [u8]`, `Cow<'a, str>`, `Cow<'a, [u8]>` (as
  `Cow::Borrowed`) — the zero-copy types.
- Every primitive (`u8` … `u128`, `i8` … `i128`, `usize`, `isize`,
  `bool`, `char`, `f32`, `f64`, `()`), `NonZero*`, `Duration`,
  `PhantomData<T>`, and owned text (`String`, `Box<str>`, `Rc<str>`,
  `Arc<str>`) — these reduce to `Deserialize`.
- `Option<T>`, `Result<T, E>`, tuples (arity 1–12), fixed arrays `[T; N]`.
- `Vec<T>`, `VecDeque<T>`, `BTreeMap<K, V>`, `BTreeSet<T>`,
  `HashMap<K, V>` *(std)*, `HashSet<T>` *(std)* — container allocated,
  elements may still borrow.

### `decode_view` / `decode_view_with_config`

```rust,ignore
pub fn decode_view<'a, T: DeserializeView<'a>>(bytes: &'a [u8]) -> Result<T>;
pub fn decode_view_with_config<'a, T: DeserializeView<'a>>(bytes: &'a [u8], config: Config) -> Result<T>;
```

Tier-1 zero-copy entry points — the counterparts of
[`decode` / `decode_with_config`](#decode--decode_with_config). Strict:
trailing bytes are rejected with [`SerialError::TrailingBytes`](#serialerror).

**Example:**

```rust
let bytes = pack_io::encode(&"hello").unwrap();
let view: &str = pack_io::decode_view(&bytes).unwrap();
assert_eq!(view, "hello"); // borrowed from `bytes`
```

<a id="decoder-borrowed-reads"></a>
### `Decoder::read_length_prefixed_borrowed` / `read_borrowed`

```rust,ignore
impl<'a> Decoder<'a> {
    pub fn read_length_prefixed_borrowed(&mut self) -> Result<&'a [u8]>;
    pub fn read_borrowed(&mut self, len: usize) -> Result<&'a [u8]>;
}
```

`read_length_prefixed_borrowed` reads a varint length prefix, validates
it against [`Config::max_alloc`](#config) and the remaining input, then
returns a borrowed slice over the next `length` bytes; it powers the
`&'a str` / `&'a [u8]` view impls. `read_borrowed` borrows a fixed number
of bytes with no prefix, for fields whose length the caller already
knows. Neither exists on [`IoDecoder<R>`](#iodecoder) — a stream has no
buffer to borrow from.

### `#[derive(DeserializeView)]`

```rust
use pack_io::{DeserializeView, Serialize, decode_view, encode};

#[derive(Serialize, DeserializeView)]
struct Header { id: u64 }                 // no lifetime: nests in views

#[derive(Serialize, DeserializeView)]
#[pack_io(version = 2)]
struct ViewMsg<'a> {
    header: Header,
    text: &'a str,
    #[pack_io(since = 2)]
    payload: &'a [u8],
}

let bytes = encode(&ViewMsg { header: Header { id: 1 }, text: "hi", payload: b"x" }).unwrap();
let view: ViewMsg<'_> = decode_view(&bytes).unwrap();
assert_eq!((view.header.id, view.text, view.payload), (1, "hi", &b"x"[..]));
```

The derive (feature `derive`) writes a [`DeserializeView`](#deserializeview)
impl for any struct or enum with **at most one** lifetime parameter:

- One lifetime `'a`: `impl DeserializeView<'a>`; borrowed fields point
  into the input.
- No lifetime: an impl for every lifetime, so owned helper types
  (headers, ids, enums of scalars) can be nested inside borrowing views
  (1.0 rejected these).

Every derive attribute works on views too, including
`#[pack_io(version = N)]`: the view decoder reads the same versioned
frame as the owning one and borrows the body instead of copying it (1.0
ignored the attribute here and mis-parsed versioned payloads). `with`
modules must provide `deserialize_view(&mut Decoder<'a>)`.

---

## Schema evolution

A struct tagged with `#[pack_io(version = N)]` encodes as a length-framed
body (`varint(version) ++ varint(body_len) ++ body`) so that old and new
revisions interoperate as long as fields are **appended**. Old decoders
skip trailing body bytes they don't recognise; new decoders
default-construct fields that older payloads didn't include.

The attributes need only the `derive` feature (`schema` is an alias kept
for compatibility). Full normative spec:
[`docs/WIRE_FORMAT.md §3.8`](./WIRE_FORMAT.md#38-versioned-structs).

### `#[pack_io(version)]`

Type-level attribute marking the struct as versioned. `N` is a positive
`u32`; `0` is rejected at compile time, and a payload carrying version
`0` is rejected at decode time with
[`SerialError::InvalidVersion`](#serialerror) (1.0 accepted it, against
the spec).

```rust,ignore
#[derive(pack_io::Serialize, pack_io::Deserialize)]
#[pack_io(version = 2)]
struct Message {
    id: u64,
    text: String,
    #[pack_io(since = 2)]
    timestamp: Option<u64>,
}
```

The body length is validated against `max_alloc` and the remaining input
before anything is read, and the body is decoded under the caller's
`Config` and nesting depth (1.0 decoded it with a fresh default config).
In-memory decoding borrows the body; nothing is copied.

### `#[pack_io(since)]`

Field-level attribute marking the field as **added** at version `N`
(default `1`). `N` must not exceed the struct's own `version` — 1.0
accepted that and silently never encoded the field; it is now a compile
error. Decoders reading payloads from versions before `N` fill the field
with `Default::default()` or the `#[pack_io(default = "path")]` function.

<a id="pack_io-retired"></a>
### `#[pack_io(retired = N)]`

Field-level attribute that takes a field out of use **without changing
the shape of the body**. From version `N` the encoder writes the field's
default (`Default::default()` or the `default = "path"` function) in its
slot, and decoders reading a payload at version `>= N` read the slot and
discard it. Payloads before `N` keep their real value.

Because the slot is still there, every reader stays aligned — including
readers built before the retirement, with 1.x or 2.x — and simply sees
the default value. This is the 2.0 fix for audit finding PACK-005: in
1.x the only option dropped the bytes, and an older reader then decoded
the *next* field's bytes into the dropped one without any error. A
retired slot usually costs one byte (an empty string, a zero, `None`).

```rust
use pack_io::{Deserialize, Serialize, decode, encode};

#[derive(Serialize, Deserialize)]
#[pack_io(version = 2)]
struct V2 { id: u64, token: String }       // a reader that predates the change

#[derive(Serialize, Deserialize)]
#[pack_io(version = 3)]
struct V3 {
    id: u64,
    #[pack_io(retired = 3)]
    token: String,
    #[pack_io(since = 3)]
    bio: String,
}

let bytes = encode(&V3 { id: 1, token: "local only".into(), bio: "hi".into() }).unwrap();
let old: V2 = decode(&bytes).unwrap();
assert_eq!(old.token, "");                  // never another field's bytes
```

<a id="pack_io-removed"></a>
### `#[pack_io(removed = N)]`

Field-level attribute that **drops the field's bytes** from version `N`
on. Encoders at version `>= N` omit it; decoders reading payloads at
version `>= N` default it. This is exactly the 1.x
`#[pack_io(deprecated = N)]` encoding, kept so that payloads written by
1.x stay readable; 1.x code that used `deprecated = N` should write
`removed = N` (2.0 rejects `deprecated` with an error that explains the
choice). `N` must be greater than `since`.

Removal takes bytes out of the *middle* of the body, so it is only safe
once every reader knows about it. A reader built before the removal
cannot tell the field is gone and decodes the following field's bytes
into it. Roll it out **reader-first**:

1. Ship readers at version `N - 1` that already declare
   `#[pack_io(removed = N)]` on the field. They still write the
   field, and they read version-`N` payloads correctly.
2. Once every reader runs that code, bump writers to `version = N`.

Prefer `retired` unless the bytes saved matter and you control every
reader.

### `peek_version`

```rust,ignore
pub fn peek_version(bytes: &[u8]) -> Result<u32>;
```

Read only the leading `varint(version)` of a versioned payload without
consuming the buffer or decoding the body. Useful when a single
transport carries multiple revisions and the dispatcher needs to pick a
target type at runtime.

```rust
# #[cfg(feature = "derive")] {
use pack_io::{encode, peek_version, Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
#[pack_io(version = 2)]
struct Msg { id: u64 }

let bytes = encode(&Msg { id: 7 }).unwrap();
assert_eq!(peek_version(&bytes).unwrap(), 2);
# }
```

On a non-versioned payload, `peek_version` returns whatever the first
varint of the payload happens to be — call it only on payloads you
know are versioned. The value is not validated: a leading `0` peeks as
`Ok(0)`, while every decoder rejects version `0` with `InvalidVersion`.

---

## Canonical decoding

The encoder produces exactly one byte string per value. By default the
decoder is lenient — as the wire spec allows — and also accepts a few
other byte strings for the same value. Where the bytes are hashed,
signed, deduplicated or used as a content address, turn on
`Config::with_canonical(true)`: the decoder then accepts only the
encoder's own output and rejects everything else with
[`SerialError::NonCanonical { reason }`](#serialerror):

| `reason` | Rejected input |
|---|---|
| `"varint"` | a multi-byte varint whose last byte is `0x00` (e.g. `[0x80, 0x00]` for `0`) |
| `"map key order"` | map entries not in strictly increasing encoded-key order (covers duplicates) |
| `"set element order"` | set elements not strictly increasing (covers duplicates) |
| `"versioned struct version"` | a versioned struct at any version other than the reader's own |
| `"versioned body"` | unread bytes inside a versioned body |
| `"retired field"` | a retired field's slot holding anything but the encoding of its default |

A canonical reader therefore reads versioned structs only at its own
schema version: an older frame is a second encoding of the defaulted
value, and a newer frame carries fields the reader cannot check (so any
bytes could be appended). Decode other versions leniently, and re-encode
them if you need their canonical form.

The guarantee covers every type built from pack-io's own impls and
derives. A hand-written `Deserialize` impl or a `with` helper is part of
it only if it, too, accepts one encoding per value, and serde set fields
need [`sorted_set`](#serde-adapter-feature-serde).

```rust
use std::collections::BTreeMap;
use pack_io::{Config, SerialError, decode, decode_with_config};

// {2: 20, 1: 10, 2: 99}: unsorted, with a duplicate key.
let bytes = [3, 2, 20, 1, 10, 2, 99];
let lenient: BTreeMap<u32, u32> = decode(&bytes).unwrap();
assert_eq!(lenient.len(), 2);

let strict = Config::new().with_canonical(true);
assert_eq!(
    decode_with_config::<BTreeMap<u32, u32>>(&bytes, strict),
    Err(SerialError::NonCanonical { reason: "map key order" }),
);
```

Canonical mode is supported by the in-memory [`Decoder`](#decoder) and
everything built on it (`decode_with_config`, `decode_view_with_config`,
`decode_from_with_config`, the serde adapter). It costs nothing on
inputs without maps; map and set decoding compares each key with the
previous one in place (no copies). `IoDecoder::with_config` rejects it
with `InvalidConfig`: checking key order needs the raw input, and
recording it on the streaming hot path measured 30–60 % slower for
*lenient* streaming decodes, so 2.0 keeps the 1.x design — buffer the
frame (`decode_from_with_config` does, within `max_alloc`).

**Why it is not the default (decided for 2.0).** Correctness first:
lenient decoding already returns the right value for every valid
encoding, the wire spec explicitly permits lenient readers, and encoding
is deterministic either way — canonical mode adds *injectivity*, which
matters only where the encoded bytes themselves are trusted as an
identity. Canonical mode also needs the raw input, so a canonical default would
make `IoDecoder::new` unusable. Then cost: measured on the baseline
machine, enforcing it adds about 25 % to decoding maps with small keys
(about 4 % with string keys), far over the 5 % regression budget, for
every user. So it stays opt-in; turn it on at the boundary where you
hash, sign, or deduplicate bytes. Escape hatch in both directions:
`Config::with_canonical(true)` per decoder, or per call through the
`*_with_config` functions.

---

## serde adapter (feature `serde`)

`pack_io::serde` puts any `serde::Serialize` / `serde::Deserialize` type
on the pack-io wire. A serde-derived type encodes to the same bytes as
the equivalent `#[derive(pack_io::Serialize)]` type: structs and tuples
are their fields concatenated, enums are `varint(index) ++ fields`,
sequences and strings are length-prefixed, maps are written in canonical
key order. **Sets are the exception**: serde has no set type, so a
`BTreeSet` / `HashSet` arrives as a sequence in iteration order (`Ord`
order, or a per-process hash order for `HashSet`). Mark set fields
`#[serde(with = "pack_io::serde::sorted_set")]` to write them in the
native encoded order (deterministic and byte-identical to the native
derive) and to have canonical mode check their order on decode.

| Item | Use |
|---|---|
| `to_vec(&v)` | encode into a fresh `Vec<u8>` |
| `to_encoder(&v, &mut enc)` | encode into any `Encode` sink |
| `from_slice(&bytes)` / `from_slice_with_config(&bytes, cfg)` | decode one value from a whole buffer; `&str` / `&[u8]` fields borrow |
| `from_decoder(&mut decoder)` | decode the next value from a `Decoder` (borrowing) |
| `from_decode(&mut any_decoder)` | decode the next value from any `Decode`, e.g. `IoDecoder` (owning) |
| `compat::{serialize, deserialize, deserialize_view}` | `#[pack_io(with = "pack_io::serde::compat")]` for one serde-typed field |
| `sorted_set::{serialize, deserialize}` | `#[serde(with = "pack_io::serde::sorted_set")]` for a set field: native set bytes, canonical order checks |
| `Compat<T>` | wrapper that gives a serde type pack-io's traits |

```rust
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
enum Shape {
    Circle { radius: u32 },
    Rect(u32, u32),
}

let bytes = pack_io::serde::to_vec(&Shape::Rect(3, 4)).unwrap();
assert_eq!(bytes, [1, 3, 4]);
let back: Shape = pack_io::serde::from_slice(&bytes).unwrap();
assert_eq!(back, Shape::Rect(3, 4));
```

Limits: pack-io is not self-describing, so `deserialize_any` (untagged
and internally tagged enums, `#[serde(flatten)]`) and
`deserialize_ignored_any` return `SerialError::Custom`. Sets without
`sorted_set` are plain sequences: their bytes follow iteration order and
canonical mode cannot check them. Decoding enforces the same
`max_alloc`, total budget (sequence and map slots, owned strings and byte
buffers, elements that occupy no input), `max_depth` and canonical-mode
rules as the native path.

---

## bytes integration (feature `bytes`)

- `bytes::Bytes` and `bytes::BytesMut` implement `Serialize` /
  `Deserialize` with the same wire shape as `Vec<u8>`.
- `BytesMut` implements `Encode`, so a value encodes straight into a
  tokio codec's output buffer.
- Zero-copy from a `Bytes` frame: decode a view and turn each borrowed
  `&[u8]` into a reference-counted slice with `Bytes::slice_ref`.

```rust,ignore
use bytes::{Bytes, BytesMut};
use pack_io::{Serialize, decode_view};

let mut buf = BytesMut::new();
(7_u32, b"payload".as_slice()).serialize(&mut buf).unwrap();
let frame: Bytes = buf.freeze();

let (id, body): (u32, &[u8]) = decode_view(&frame).unwrap();
let body: Bytes = frame.slice_ref(body); // no copy
```

---

## Errors

### `SerialError`

```rust,ignore
#[non_exhaustive]
pub enum SerialError {
    UnexpectedEof      { needed: usize, remaining: usize },
    InvalidLength      { declared: u64, remaining: usize },
    VarintOverflow,
    IntegerOutOfRange,
    InvalidBool        { byte: u8 },
    InvalidUtf8,
    InvalidTag         { kind: &'static str, tag: u8 },
    UnknownVariant     { kind: &'static str, index: u64 },
    TrailingBytes      { remaining: usize },
    #[cfg(feature = "std")]
    Io                 { kind: std::io::ErrorKind, message: String },
    DepthLimitExceeded { max_depth: usize },
    NonCanonical       { reason: &'static str },
    InvalidVersion     { version: u64 },
    InvalidValue       { kind: &'static str },
    InvalidConfig      { reason: &'static str },
    Custom             { message: Cow<'static, str> },
}
```

`#[non_exhaustive]` so MINOR releases can add variants without breaking
downstream `match` arms.

| Variant | When | Caller action |
|---------|------|---------------|
| `UnexpectedEof` | Decoder needed more bytes than were available. | Treat as truncated transport. |
| `InvalidLength` | A length prefix exceeded the buffer or `max_alloc`; `decode_from` read more than `max_alloc`. | Reject the message. |
| `VarintOverflow` | A LEB128 varint exceeded its target width. | Reject. |
| `IntegerOutOfRange` | A decoded integer did not fit in the requested narrower target. | Reject; producer / consumer width mismatch. |
| `InvalidBool` | A boolean byte was neither `0x00` nor `0x01`. | Reject. |
| `InvalidUtf8` | A length-prefixed byte run was not valid UTF-8. | Reject. |
| `InvalidTag` | An `Option` / `Result` tag was outside `0x00` / `0x01`. | Reject. |
| `UnknownVariant` | An enum tag matched no variant of the target type; `index` is the tag read. | Reject; producer / consumer are at incompatible enum revisions. |
| `TrailingBytes` | A whole-buffer decode left bytes unread. | Either producer wrong, or use [`Decoder`]#decoder for multi-value streams. |
| `Io` *(std-only)* | Underlying `Read` / `Write` failed. | Inspect `kind` and `message`; surface to transport. |
| `DepthLimitExceeded` | Nesting exceeded `max_depth`. | Treat as hostile, or raise the limit for trusted deep data. |
| `NonCanonical` | Canonical mode saw a non-canonical encoding. | Reject; the bytes are not the canonical form of their value. |
| `InvalidVersion` | A versioned payload carried the reserved version `0`. | Reject as corrupt. |
| `InvalidValue` | The value violates its type's invariant (`char`, `NonZero*`, `Duration`). | Reject. |
| `InvalidConfig` | A `Config` knob was rejected when building a decoder. | Fix the configuration. |
| `Custom` | Raised by user code, a `with` module, or a serde type. | Depends on the source. |

`SerialError` implements `Debug`, `Clone`, `PartialEq`, `Eq`, `Display`,
and (under the default `std` feature) `std::error::Error`; with the
`serde` feature it also implements `serde::ser::Error` and
`serde::de::Error`. Error messages never echo the offending bytes back to
the caller — safe to log without input sanitisation.
`SerialError::custom(message)` builds a `Custom` from a `&'static str`
or a `String`.

### `Result<T>`

```rust,ignore
pub type Result<T> = core::result::Result<T, SerialError>;
```

Convenience alias used throughout the codec.

---

## Supported types

| Group | Types |
|-------|-------|
| Unsigned integers | `u8`, `u16`, `u32`, `u64`, `u128`, `usize`, `NonZeroU*` |
| Signed integers   | `i8`, `i16`, `i32`, `i64`, `i128`, `isize`, `NonZeroI*` |
| Floats            | `f32`, `f64` |
| Bool / char / unit| `bool`, `char`, `()`, `PhantomData<T>` |
| Strings           | `String`, `&str` (encode / view), `Box<str>`, `Rc<str>`, `Arc<str>`, `Cow<'_, str>` |
| Sequences         | `Vec<T>`, `&[T]` (encode), `Box<[T]>`, `Rc<[T]>`, `Arc<[T]>`, `VecDeque<T>`, `[T; N]` |
| Tuples            | arity 1 through 12 |
| Sums              | `Option<T>`, `Result<T, E>` |
| Maps              | `BTreeMap<K, V>`, `HashMap<K, V, S>` *(std)* |
| Sets              | `BTreeSet<T>`, `HashSet<T, S>` *(std)* |
| Time              | `core::time::Duration` |
| Pointers          | `&T` (encode), `Box<T>`, `Rc<T>`, `Arc<T>`, `Cow<'_, T>` — transparent |
| Byte buffers      | `bytes::Bytes`, `bytes::BytesMut` *(feature `bytes`)* |
| serde types       | any, through [`pack_io::serde`]#serde-adapter-feature-serde *(feature `serde`)* |

User-defined types opt in via `#[derive(pack_io::Serialize,
pack_io::Deserialize)]` under the `derive` feature.

`Box<T>` is implemented for every `T` (new in 2.0). A downstream crate
that wrote `impl pack_io::Serialize for Box<TheirType>` against 1.x must
delete that impl; the blanket impl encodes the same bytes (see
[`docs/MIGRATION.md`](./MIGRATION.md)).

---

## Wire format

The full normative byte-level spec lives in
[`docs/WIRE_FORMAT.md`](./WIRE_FORMAT.md). Highlights:

- LEB128 varint for multi-byte unsigned integers, ZigZag-then-LEB128 for
  signed. Same shape as `protobuf`, `postcard`, and `bincode`'s varint mode.
- 1 byte fixed for `u8` / `i8` (no varint overhead for standalone bytes).
- IEEE 754 bit pattern, little-endian for `f32` / `f64`.
- `String` / sequences / collections: varint length prefix + content.
- `Option` / `Result` / `bool`: strict 1-byte tag (`0x00` / `0x01`).
- **Maps and sets encode in canonical key-sorted order** — sorted
  lexicographically by the encoded key bytes. A `HashMap` and a
  `BTreeMap` over the same logical data encode identically.

The wire format has not changed since 1.0. 2.0 adds encodings for new
types (spec 1.3) and clarifies decoder obligations; every byte string a
1.x encoder produced for valid data is produced identically by 2.0 and
decodes to the same value (checked against committed 1.0.1 golden
vectors, plus 2.0 vectors for the new encodings).

---

## Feature flags

| Feature  | Default | Description |
|----------|---------|-------------|
| `std`    | yes     | Standard library. Off → `no_std` (`core` + `alloc`). Enables [`std::error::Error`] on [`SerialError`]#serialerror, `HashMap` / `HashSet` integration, and the [`io`]#tier-2b--streaming-codec module. |
| `derive` | no      | `#[derive(Serialize, Deserialize, DeserializeView)]` and every `#[pack_io(...)]` attribute. Pulls in the companion `pack-io-derive` crate. |
| `schema` | no      | Alias for `derive`, kept for compatibility. |
| `serde`  | no      | The [`pack_io::serde`]#serde-adapter-feature-serde adapter. Adds the `serde` dependency (`alloc` features; `std` follows pack-io's `std`). |
| `bytes`  | no      | [`bytes`]#bytes-integration-feature-bytes integration. Adds the `bytes` dependency (no default features). |

All feature flags are **additive**. Enabling a feature never removes or
changes existing behaviour; disabling a feature never breaks code that did
not opt into it.

---

## Other public items

### `VERSION`

```rust,ignore
pub const VERSION: &str;
```

The semantic version of the crate, exposed at compile time. Mirrors
`Cargo.toml` exactly.

**Example:**

```rust
// VERSION mirrors Cargo.toml exactly, with no parsing.
assert_eq!(pack_io::VERSION, env!("CARGO_PKG_VERSION"));
```

### `varint_len`

```rust,ignore
pub const fn varint_len(value: u64) -> usize;
```

Bytes the LEB128 encoding of `value` occupies (1–10). Use it in
hand-written `encoded_len` overrides.

```rust
assert_eq!(pack_io::varint_len(127), 1);
assert_eq!(pack_io::varint_len(128), 2);
```

---

## Cross-platform notes

- Tier-1 supported targets: Linux (`x86_64`, `aarch64`), macOS (`x86_64`,
  Apple Silicon), Windows (`x86_64`). All three run the full CI matrix on
  every commit, on both stable and MSRV. CI also builds for a bare-metal
  `no_std` target (`thumbv7em-none-eabihf`).
- Encoding is byte-deterministic across all of them.
- `usize` and `isize` encode through `u64` and `i64` respectively, so a
  value encoded on a 64-bit machine that exceeds `usize::MAX` on a 32-bit
  consumer surfaces as [`SerialError::IntegerOutOfRange`]#serialerror —
  no silent truncation.
- `no_std` builds rely on `core` + `alloc` only. The Tier-2b streaming
  codec ([`IoEncoder`]#ioencoder / [`IoDecoder`]#iodecoder,
  [`encode_into`]#encode_into /
  [`decode_from`]#decode_from--decode_from_with_config) requires
  `std` and is gated on the `std` feature. `Arc` impls require a target
  with pointer-width atomics.
- `HashMap` / `HashSet` impls require `std` and are gated on the same
  feature.

---

## Compatibility & semver

- **The public surface listed in [§ Frozen public surface]#frozen-public-surface
  is the `2.x` contract.** Source-breaking changes wait for the next
  major version.
- **The wire format ([`docs/WIRE_FORMAT.md`]./WIRE_FORMAT.md, spec
  version 1.3) has been stable since 1.0.** Any `2.x` decoder reads any
  `1.x` or `2.x` encoding of valid data. A wire-format-breaking change
  requires a new major version.
- **The performance baseline ([`docs/PERFORMANCE_BASELINE.md`]./PERFORMANCE_BASELINE.md)
  is the contractual reference.** A change exceeding 5 % regression on
  any row blocks the merge.
- `2.x` minor releases ship bug fixes, hardening, performance
  improvements, and strictly *additive* API (new `SerialError` variants
  under the existing `#[non_exhaustive]` enum, new trait methods with
  default implementations, new built-in impls for `core` / `alloc` types).
- 2.0 was a major release because of behaviour changes, not wire
  changes: decoders reject input past their limits or against the spec
  that 1.x accepted, the derive rejects attributes 1.x ignored, and
  `deprecated` became `retired` / `removed`. Each is listed with its
  migration in [`docs/MIGRATION.md`]./MIGRATION.md.
- Deprecated items remain available for at least one MAJOR after the
  `#[deprecated]` attribute is added.