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
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
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
//! The codec primitives: the [`Encode`] / [`Decode`] behaviour traits, the
//! concrete in-memory [`Encoder`] / [`Decoder`] types, the [`Config`] struct,
//! and the Tier-1 [`encode`] / [`decode`] free functions.
//!
//! ## Layering
//!
//! - **Tier 1** — the [`encode`] / [`decode`] free functions. One line each
//!   direction, no setup, no type parameters beyond the target type.
//! - **Tier 2** — concrete encoder / decoder types. The in-memory pair
//!   ([`Encoder`] + [`Decoder`]) lives in this module; the streaming pair
//!   ([`crate::IoEncoder`] + [`crate::IoDecoder`]) lives in
//!   [`crate::io`] and is `std`-gated. All four implement the [`Encode`] /
//!   [`Decode`] behaviour traits, so [`Serialize`] / [`Deserialize`] impls
//!   work through any of them.
//! - **Tier 3** — implementing the [`Serialize`] / [`Deserialize`] traits
//!   directly on your own types. Generic over `E: Encode` / `D: Decode`, so
//!   one impl works for both in-memory and streaming codecs.
//!
//! ## Safety contract for decoders
//!
//! Every method on [`Decode`] is total: it either returns the requested
//! value (advancing the read cursor) or returns a [`SerialError`]. It never
//! panics and never reads past the input. Allocation is bounded three ways:
//!
//! - no single length-prefixed value may exceed [`Config::max_alloc`];
//! - the memory one decode builds (every collection slot at
//!   `size_of::<T>()`, every owned string or byte buffer, every boxed
//!   value) is charged against one budget for the whole value,
//!   [`Config::max_total_alloc`], which defaults to `max_alloc`;
//! - no allocation is sized from a declared length before the bytes backing
//!   it are known to exist (in-memory decoders check the remaining input;
//!   streaming decoders grow their buffers only as data actually arrives).
//!
//! The memory a decode holds is therefore at most a small constant times
//! the total budget (a growing `Vec` can briefly hold twice its length, and
//! hash and tree collections add their own per-entry overhead), whatever
//! the input says. Nesting is bounded by [`Config::max_depth`].

use alloc::borrow::Cow;
use alloc::vec::Vec;

use crate::error::{Result, SerialError};
use crate::traits::{Deserialize, Serialize};
use crate::varint;

/// Configuration for a decode session.
///
/// At construction time the codec validates the configuration; an invalid
/// config (`max_alloc == 0`, `max_depth == 0`) is rejected before any bytes
/// are read. Validation happens once, in [`Decoder::with_config`] /
/// `IoDecoder::with_config` (feature `std`) / the `*_with_config` free
/// functions,
/// not on every operation.
///
/// `Config` is `#[non_exhaustive]` so the project can add knobs in a MINOR
/// release without breaking downstream code. Build instances with
/// [`Config::new`] and the `with_*` builders, or via [`Default`].
///
/// | Knob | Default | Purpose |
/// |---|---|---|
/// | [`max_alloc`](Config::max_alloc) | 1 GiB | cap on any single length-prefixed value |
/// | [`max_total_alloc`](Config::max_total_alloc) | `None` (same as `max_alloc`) | cap on the memory one decoded value builds in total |
/// | [`max_depth`](Config::max_depth) | 128 | cap on nested derived types (stack-overflow guard) |
/// | [`canonical`](Config::canonical) | `false` | reject every non-canonical encoding |
///
/// The 1 GiB default suits files and other trusted, possibly large data.
/// For bytes that come from a peer, start from [`Config::network`]
/// (16 MiB per value and per message) and tighten it further if the
/// protocol's messages are smaller.
///
/// # Examples
///
/// ```
/// use pack_io::{Config, Decoder};
///
/// // Refuse to allocate more than 16 KiB for any single length-prefixed
/// // value (a `String`, a `Vec<u8>`, a collection element count, …),
/// // refuse nesting deeper than 32 derived types, and only accept the
/// // exact bytes the encoder would have produced.
/// let cfg = Config::new()
///     .with_max_alloc(16 * 1024)
///     .with_max_depth(32)
///     .with_canonical(true);
/// let dec = Decoder::with_config(&[], cfg).expect("valid config");
/// assert_eq!(dec.config(), cfg);
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Config {
    /// Maximum number of bytes the decoder may allocate for any single
    /// length-prefixed value (a `String`, a `Vec<u8>`, a collection element
    /// count, a versioned struct body, …).
    ///
    /// The default is 1 GiB, which is enough that well-formed inputs are
    /// never rejected on size, while still refusing the obvious
    /// hostile-length-prefix DoS before allocating. It is a limit per
    /// value; the memory a whole decode builds is capped by
    /// [`max_total_alloc`](Config::max_total_alloc), which follows this
    /// value unless set separately. 1 GiB is a file-format default, not a
    /// network one: for peer input use [`Config::network`] or a tighter
    /// limit.
    pub max_alloc: usize,

    /// Maximum memory, in bytes, that one decoded value may build in
    /// total. `None` (the default) means "the same as
    /// [`max_alloc`](Config::max_alloc)".
    ///
    /// Every collection charges `size_of::<T>()` per element slot, every
    /// owned `String` / `Vec<u8>` / `Bytes` charges its length, and every
    /// `Box<T>` / `Rc<T>` / `Arc<T>` charges the value it holds, against
    /// one running total. A collection element that consumes no input at
    /// all (`()`, `Box<()>`, a struct whose fields are all `skip`) is
    /// charged at least 64 bytes, so a few bytes of input cannot drive an
    /// element loop for long either. The decode fails with
    /// [`SerialError::InvalidLength`] (`declared` set to the total) once
    /// the total would pass the limit.
    ///
    /// The total covers one value: each [`Decoder::read`] and
    /// `IoDecoder::read` (feature `std`) starts a new one, and every free
    /// function (`decode`, `decode_with_config`, …) decodes exactly one
    /// value. Borrowed views (`&str`, `&[u8]` through
    /// [`crate::DeserializeView`]) take no heap and are not charged.
    pub max_total_alloc: Option<usize>,

    /// Maximum nesting depth of derived types during one decode.
    ///
    /// Every `#[derive(Deserialize)]` / `#[derive(DeserializeView)]` value
    /// (struct or enum) counts as one level while its fields are being
    /// decoded. The limit exists so that a few kilobytes of hostile input
    /// for a recursive type cannot exhaust the thread's stack, which aborts
    /// the process rather than returning an error. Exceeding it returns
    /// [`SerialError::DepthLimitExceeded`].
    ///
    /// The default of 128 matches `serde_json` and leaves ample headroom on
    /// a 1 MiB stack in debug builds. Built-in container types (`Vec`,
    /// `Option`, maps, tuples, …) do not count: they cannot recurse without
    /// passing through a user type.
    pub max_depth: usize,

    /// Strict canonical decoding. Off by default.
    ///
    /// When `true`, the decoder accepts only the exact bytes the pack-io
    /// encoder would produce for the decoded value, and rejects everything
    /// else with [`SerialError::NonCanonical`]:
    ///
    /// - varints with redundant zero high bytes (`[0x80, 0x00]` for `0`);
    /// - map and set entries that are not in strictly increasing
    ///   encoded-key order (this also rejects duplicates);
    /// - a versioned struct written at any version other than the decoding
    ///   type's own: an older frame is a second encoding of the defaulted
    ///   value, and a newer one carries fields the reader cannot check;
    /// - unread bytes inside a versioned struct body;
    /// - a retired field's slot holding anything but its default.
    ///
    /// Turn it on wherever the encoded bytes are hashed, signed, or used as
    /// a content address. Decode payloads from older or newer schema
    /// 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; serde
    /// types follow the adapter's rules (sets need
    /// `pack_io::serde::sorted_set`).
    ///
    /// **Why it is off by default** (re-evaluated for 2.0): lenient decoding
    /// is correct — every valid encoding decodes to its value, the wire
    /// specification permits lenient readers (`docs/WIRE_FORMAT.md` §2.1,
    /// §4.3), and the encoder is deterministic either way. What canonical
    /// mode adds is injectivity (one accepted byte string per value), which
    /// only matters where the bytes themselves are trusted as an identity,
    /// and it gives up reading other schema versions of versioned structs.
    /// It also needs the raw input, so a canonical default would make every
    /// streaming decoder fail to construct. And it is not free: about 25 %
    /// on small-key map decoding on the v2.0 baseline machine, well beyond
    /// the 5 % regression budget for everyone who does not need it.
    ///
    /// Canonical mode is supported by the in-memory [`Decoder`] (and every
    /// free function built on it). `IoDecoder::with_config` (feature `std`) rejects
    /// it with [`SerialError::InvalidConfig`]; read the frame into memory
    /// first (`decode_from_with_config` does that within the
    /// configured `max_alloc`).
    pub canonical: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self::new()
    }
}

impl Config {
    /// Default value of [`Config::max_alloc`]: 1 GiB.
    pub const DEFAULT_MAX_ALLOC: usize = 1 << 30;

    /// Default value of [`Config::max_depth`]: 128 nested derived types.
    pub const DEFAULT_MAX_DEPTH: usize = 128;

    /// Per-value and per-message limit of [`Config::network`]: 16 MiB.
    pub const NETWORK_MAX_ALLOC: usize = 16 << 20;

    /// Default configuration: `max_alloc = 1 GiB`, a total budget equal to
    /// `max_alloc`, `max_depth = 128`, lenient (non-canonical) decoding.
    ///
    /// 1 GiB is large enough to be irrelevant for well-formed inputs and
    /// small enough to refuse the obvious `length = u64::MAX` attack before
    /// allocating a single byte. For input from a peer, use
    /// [`Config::network`].
    ///
    /// # Examples
    ///
    /// ```
    /// let cfg = pack_io::Config::new();
    /// assert_eq!(cfg.max_alloc, 1 << 30);
    /// assert_eq!(cfg.max_total_alloc, None);
    /// assert_eq!(cfg.total_alloc_limit(), 1 << 30);
    /// assert_eq!(cfg.max_depth, 128);
    /// assert!(!cfg.canonical);
    /// ```
    #[must_use]
    pub const fn new() -> Self {
        Self {
            max_alloc: Self::DEFAULT_MAX_ALLOC,
            max_total_alloc: None,
            max_depth: Self::DEFAULT_MAX_DEPTH,
            canonical: false,
        }
    }

    /// Limits for bytes received from a peer: 16 MiB for any single value
    /// and 16 MiB for everything one decoded message builds, depth 128,
    /// lenient decoding.
    ///
    /// Use it (or something tighter) for every decode of network input;
    /// the 1 GiB default of [`Config::new`] is meant for files and other
    /// trusted data. A protocol whose messages are larger than 16 MiB
    /// raises both limits explicitly.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Config, SerialError, decode_with_config, encode};
    ///
    /// let cfg = Config::network();
    /// assert_eq!(cfg.max_alloc, 16 << 20);
    /// assert_eq!(cfg.total_alloc_limit(), 16 << 20);
    ///
    /// // Ten 2 MiB strings: each fits the per-value limit, together they do
    /// // not fit the message budget.
    /// let big = vec!["x".repeat(2 << 20); 10];
    /// let err = decode_with_config::<Vec<String>>(&encode(&big)?, cfg).unwrap_err();
    /// assert!(matches!(err, SerialError::InvalidLength { .. }));
    /// # Ok::<(), pack_io::SerialError>(())
    /// ```
    #[must_use]
    pub const fn network() -> Self {
        Self {
            max_alloc: Self::NETWORK_MAX_ALLOC,
            max_total_alloc: Some(Self::NETWORK_MAX_ALLOC),
            max_depth: Self::DEFAULT_MAX_DEPTH,
            canonical: false,
        }
    }

    /// The total budget this config enforces: [`Config::max_total_alloc`]
    /// when set, otherwise [`Config::max_alloc`].
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Config;
    ///
    /// assert_eq!(Config::new().with_max_alloc(4096).total_alloc_limit(), 4096);
    /// let split = Config::new().with_max_alloc(4096).with_max_total_alloc(1 << 20);
    /// assert_eq!(split.total_alloc_limit(), 1 << 20);
    /// ```
    #[inline]
    #[must_use]
    pub const fn total_alloc_limit(&self) -> usize {
        match self.max_total_alloc {
            Some(total) => total,
            None => self.max_alloc,
        }
    }

    /// Replace `max_alloc` and return the updated config.
    ///
    /// # Examples
    ///
    /// ```
    /// let cfg = pack_io::Config::new().with_max_alloc(4096);
    /// assert_eq!(cfg.max_alloc, 4096);
    /// ```
    #[must_use]
    pub const fn with_max_alloc(mut self, max_alloc: usize) -> Self {
        self.max_alloc = max_alloc;
        self
    }

    /// Set the total budget for one decoded value
    /// ([`Config::max_total_alloc`]) and return the updated config.
    ///
    /// `0` is rejected at decoder construction with
    /// [`SerialError::InvalidConfig`].
    ///
    /// # Examples
    ///
    /// ```
    /// // Strings of up to 64 KiB each, but no more than 1 MiB per message.
    /// let cfg = pack_io::Config::new()
    ///     .with_max_alloc(64 * 1024)
    ///     .with_max_total_alloc(1 << 20);
    /// assert_eq!(cfg.max_total_alloc, Some(1 << 20));
    /// ```
    #[must_use]
    pub const fn with_max_total_alloc(mut self, max_total_alloc: usize) -> Self {
        self.max_total_alloc = Some(max_total_alloc);
        self
    }

    /// Replace `max_depth` and return the updated config.
    ///
    /// `0` is rejected at decoder construction with
    /// [`SerialError::InvalidConfig`].
    ///
    /// # Examples
    ///
    /// ```
    /// let cfg = pack_io::Config::new().with_max_depth(16);
    /// assert_eq!(cfg.max_depth, 16);
    /// ```
    #[must_use]
    pub const fn with_max_depth(mut self, max_depth: usize) -> Self {
        self.max_depth = max_depth;
        self
    }

    /// Turn strict canonical decoding on or off and return the updated
    /// config. See [`Config::canonical`] for exactly what is rejected.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Config, SerialError, decode_with_config};
    ///
    /// // `[0x80, 0x00]` is an overlong (two-byte) encoding of 0.
    /// let overlong = [0x80, 0x00];
    /// assert_eq!(pack_io::decode::<u64>(&overlong).unwrap(), 0);
    ///
    /// let strict = Config::new().with_canonical(true);
    /// let err = decode_with_config::<u64>(&overlong, strict).unwrap_err();
    /// assert!(matches!(err, SerialError::NonCanonical { reason: "varint" }));
    /// ```
    #[must_use]
    pub const fn with_canonical(mut self, canonical: bool) -> Self {
        self.canonical = canonical;
        self
    }

    /// Validate the configuration: `max_alloc`, `max_total_alloc` (when
    /// set) and `max_depth` must be at least 1, otherwise
    /// [`SerialError::InvalidConfig`].
    pub(crate) fn validate(self) -> Result<Self> {
        if self.max_alloc == 0 {
            return Err(SerialError::InvalidConfig {
                reason: "max_alloc must be at least 1",
            });
        }
        if self.max_total_alloc == Some(0) {
            return Err(SerialError::InvalidConfig {
                reason: "max_total_alloc must be at least 1",
            });
        }
        if self.max_depth == 0 {
            return Err(SerialError::InvalidConfig {
                reason: "max_depth must be at least 1",
            });
        }
        Ok(self)
    }
}

/// Upper bound on the bytes a decoder pre-allocates for a collection before
/// any element has been read. Collections larger than this grow while they
/// decode, so honest payloads pay at most a few reallocations while a
/// hostile element count cannot reserve more than this up front.
pub(crate) const PREALLOC_BYTES: usize = 64 * 1024;

/// Pre-allocation bound for a collection read from a decoder that does not
/// know how much input remains (a stream). Smaller than [`PREALLOC_BYTES`]
/// because nothing else bounds it: a stalled stream that opens one hostile
/// collection per nesting level would otherwise pin `max_depth` × 64 KiB.
pub(crate) const STREAM_PREALLOC_BYTES: usize = 4 * 1024;

/// Initial capacity for a collection of `count` elements of `T`.
///
/// Bounded three ways: by the declared count, by the number of input bytes
/// that remain (when the decoder knows it; every element that needs storage
/// almost always needs at least one byte of input), and by
/// [`PREALLOC_BYTES`] worth of `T` ([`STREAM_PREALLOC_BYTES`] when the
/// remaining input is unknown).
#[inline]
pub(crate) fn initial_capacity<T, D: Decode + ?Sized>(count: usize, decoder: &D) -> usize {
    let per_element = core::mem::size_of::<T>().max(1);
    match decoder.remaining_hint() {
        Some(remaining) => count.min(PREALLOC_BYTES / per_element).min(remaining),
        None => count.min(STREAM_PREALLOC_BYTES / per_element),
    }
}

/// Cost charged for each collection element that consumed no input bytes,
/// whatever its in-memory size.
///
/// Such elements (`()`, `Box<()>`, a struct whose fields are all `skip`)
/// are not bounded by the input, so the budget has to bound them. Charging
/// at least this much each keeps both their memory and the time spent in
/// the element loop proportional to [`Config::total_alloc_limit`]: with the
/// 1 GiB default a few bytes of input can ask for at most 16 Mi such
/// elements, and with [`Config::network`] for 256 Ki.
pub(crate) const FREE_ELEMENT_COST: usize = 64;

/// Charge the in-memory slots of a collection of `count` elements of `T`
/// (`count * size_of::<T>()`) against the decode's total budget, before any
/// element is decoded.
///
/// This is what bounds memory for elements whose encoding is far smaller
/// than their in-memory size: `Option<[u8; 4096]>` encodes `None` in one
/// byte but occupies 4097 bytes in a `Vec`.
///
/// # Errors
///
/// [`SerialError::InvalidLength`] when the slots alone exceed the
/// remaining budget.
#[inline]
pub(crate) fn charge_elements<T, D: Decode + ?Sized>(decoder: &mut D, count: usize) -> Result<()> {
    let size = core::mem::size_of::<T>();
    if size == 0 || count == 0 {
        return Ok(());
    }
    decoder.charge_unbacked(count.saturating_mul(size))
}

/// Charge the heap block of one `Box<T>` / `Rc<T>` / `Arc<T>` holding a `T`
/// (`size_of::<T>()` plus `extra` bytes of pointer bookkeeping).
///
/// # Errors
///
/// [`SerialError::InvalidLength`] when the block exceeds the remaining
/// budget.
#[inline]
pub(crate) fn charge_boxed<T, D: Decode + ?Sized>(decoder: &mut D, extra: usize) -> Result<()> {
    let size = core::mem::size_of::<T>();
    if size == 0 && extra == 0 {
        return Ok(());
    }
    decoder.charge_unbacked(size.saturating_add(extra))
}

/// Decode `count` collection elements by calling `each` once per element,
/// charging [`FREE_ELEMENT_COST`] for every element that took no input
/// bytes.
///
/// The caller has already charged the element slots with
/// [`charge_elements`]. Most elements consume at least one input byte, so
/// their number is bounded by the input itself. An element type that
/// encodes to nothing (a zero-sized type, `Box<()>`, `Rc<()>`, a struct
/// whose fields are all `skip`, a `with` helper that reads nothing) is not:
/// a five-byte count could otherwise demand billions of loop iterations.
/// Every element beyond the number of input bytes the loop consumed is
/// therefore charged through [`Decode::charge_unbacked`], which refuses the
/// decode once the running total exceeds [`Config::total_alloc_limit`].
///
/// The check runs between chunks, not per element: the first chunk is
/// [`PREALLOC_BYTES`] worth of `T` (so a typical collection is one chunk
/// and one comparison), and later chunks double, never past what the
/// remaining budget and input could cover. The inner loop is exactly the
/// plain element loop.
#[inline]
pub(crate) fn decode_elements_counted<T, D, F>(
    decoder: &mut D,
    count: usize,
    mut each: F,
) -> Result<()>
where
    D: Decode + ?Sized,
    F: FnMut(&mut D) -> Result<()>,
{
    let Some(start) = decoder.bytes_read() else {
        // The decoder does not count its input, so element sizes cannot be
        // observed. Documented on `Decode::bytes_read`.
        for _ in 0..count {
            each(decoder)?;
        }
        return Ok(());
    };
    let per_element = core::mem::size_of::<T>().max(1);
    let mut done = 0usize;
    let mut charged = 0usize;
    let mut stop = count.min((PREALLOC_BYTES / per_element).max(1));
    loop {
        for _ in done..stop {
            each(decoder)?;
        }
        done = stop;
        // Fast path: at least one input byte per element so far, so no
        // element can have been free.
        let read = decoder.bytes_read().unwrap_or(start).wrapping_sub(start);
        if done as u64 > read {
            charged = charge_free_elements(decoder, done, read, charged)?;
        }
        if done == count {
            return Ok(());
        }
        stop = next_element_stop(decoder, done, count)?;
    }
}

/// How many more free elements the remaining budget can pay for.
#[inline]
fn free_budget<D: Decode + ?Sized>(decoder: &D) -> usize {
    decoder
        .config()
        .total_alloc_limit()
        .saturating_sub(decoder.unbacked())
        / FREE_ELEMENT_COST
}

/// Charge the elements among the first `done` that the `read` input bytes
/// cannot account for, beyond the `charged` already paid for.
#[cold]
#[inline(never)]
fn charge_free_elements<D: Decode + ?Sized>(
    decoder: &mut D,
    done: usize,
    read: u64,
    charged: usize,
) -> Result<usize> {
    // `read < done`, so the difference fits in `usize`.
    let free = usize::try_from(done as u64 - read).unwrap_or(usize::MAX);
    if free > charged {
        decoder.charge_unbacked((free - charged).saturating_mul(FREE_ELEMENT_COST))?;
        return Ok(free);
    }
    Ok(charged)
}

/// Where the next chunk of a counted element loop ends: double the elements
/// decoded so far, but never more than the remaining budget for free
/// elements plus the remaining input could pay for.
///
/// Every element still to come is either free (charged against the budget)
/// or consumes at least one input byte, so when the decoder knows its
/// remaining input and the elements left exceed both together, the decode
/// cannot succeed: it is refused here, before the collection grows again.
#[cold]
#[inline(never)]
fn next_element_stop<D: Decode + ?Sized>(decoder: &D, done: usize, count: usize) -> Result<usize> {
    let free_budget = free_budget(decoder);
    let left = count - done;
    let room = match decoder.remaining_hint() {
        Some(input) => {
            let room = free_budget.saturating_add(input);
            if left > room {
                let total = decoder
                    .unbacked()
                    .saturating_add(left.saturating_mul(FREE_ELEMENT_COST));
                return Err(unbacked_limit_error(total, Some(input)));
            }
            room
        }
        None => free_budget,
    };
    let chunk = done.min(room).max(1);
    Ok(done.saturating_add(chunk).min(count))
}

/// Per-element form of [`charge_elements`] plus [`decode_elements_counted`],
/// for loops the crate does not drive itself (the serde adapter's sequence
/// and map access): call [`FreeElements::after_element`] once after each
/// element.
#[cfg(feature = "serde")]
#[derive(Debug)]
pub(crate) struct FreeElements {
    start: Option<u64>,
    done: usize,
    charged: usize,
}

#[cfg(feature = "serde")]
impl FreeElements {
    /// Start counting at the decoder's current position.
    #[inline]
    pub(crate) fn new<D: Decode + ?Sized>(decoder: &D) -> Self {
        Self {
            start: decoder.bytes_read(),
            done: 0,
            charged: 0,
        }
    }

    /// Record one more decoded element of `per_element` bytes in memory:
    /// charge its slot, and charge [`FREE_ELEMENT_COST`] if the input
    /// consumed so far cannot account for it.
    #[inline]
    pub(crate) fn after_element<D: Decode + ?Sized>(
        &mut self,
        decoder: &mut D,
        per_element: usize,
    ) -> Result<()> {
        self.done += 1;
        if per_element != 0 {
            decoder.charge_unbacked(per_element)?;
        }
        let Some(start) = self.start else {
            return Ok(());
        };
        let read = decoder.bytes_read().unwrap_or(start).wrapping_sub(start);
        if self.done as u64 > read {
            self.charged = charge_free_elements(decoder, self.done, read, self.charged)?;
        }
        Ok(())
    }
}

/// The error for a decode whose memory would exceed
/// [`Config::total_alloc_limit`].
#[cold]
pub(crate) fn unbacked_limit_error(total: usize, remaining: Option<usize>) -> SerialError {
    SerialError::InvalidLength {
        declared: total as u64,
        remaining: remaining.unwrap_or(0),
    }
}

/// Validate a declared element count against the decoder's `max_alloc`
/// and convert it to `usize`.
#[inline]
pub(crate) fn guard_element_count<D: Decode + ?Sized>(declared: u64, decoder: &D) -> Result<usize> {
    let max = decoder.max_alloc() as u64;
    if declared > max {
        return Err(SerialError::InvalidLength {
            declared,
            remaining: decoder.remaining_hint().unwrap_or(0),
        });
    }
    usize::try_from(declared).map_err(|_| SerialError::IntegerOutOfRange)
}

/// [`guard_element_count`] for a length that becomes an owned byte buffer
/// (`String`, `Vec<u8>` read as bytes, `Bytes`): validate it, then charge
/// its bytes against the total budget.
#[inline]
pub(crate) fn guard_owned_bytes<D: Decode + ?Sized>(
    declared: u64,
    decoder: &mut D,
) -> Result<usize> {
    let len = guard_element_count(declared, decoder)?;
    if len != 0 {
        decoder.charge_unbacked(len)?;
    }
    Ok(len)
}

// ---------------------------------------------------------------------------
// Encode / Decode behaviour traits
// ---------------------------------------------------------------------------

/// Sink that a [`Serialize`] implementation writes its wire-format bytes
/// into.
///
/// Implemented by every concrete encoder in the crate ([`Encoder`] for the
/// in-memory case, `IoEncoder` (feature `std`) for `std::io::Write` streams). User
/// code rarely implements `Encode` directly — `Serialize` impls are written
/// generically over `E: Encode` so a single impl works for every encoder
/// flavour.
///
/// # Examples
///
/// ```
/// use pack_io::{Encode, Encoder, Result};
///
/// // A helper that writes a length-prefixed list of `u32`s into any encoder.
/// fn write_u32_list<E: Encode>(enc: &mut E, items: &[u32]) -> Result<()> {
///     enc.write_varint_u64(items.len() as u64)?;
///     for item in items {
///         enc.write_varint_u64(u64::from(*item))?;
///     }
///     Ok(())
/// }
///
/// let mut enc = Encoder::new();
/// write_u32_list(&mut enc, &[1, 2, 3]).unwrap();
/// ```
pub trait Encode {
    /// Append a single byte.
    ///
    /// # Errors
    ///
    /// Returns the encoder's underlying error variant (I/O failure for
    /// streaming encoders; never errors for the in-memory [`Encoder`]).
    fn write_byte(&mut self, byte: u8) -> Result<()>;

    /// Append a slice of bytes.
    ///
    /// # Errors
    ///
    /// Same as [`Encode::write_byte`].
    fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>;

    /// Hint that the caller is about to write `additional` more bytes.
    ///
    /// In-memory encoders MAY pre-allocate the requested capacity to avoid
    /// intermediate `Vec` growth. Streaming encoders typically ignore the
    /// hint. The default implementation is a no-op.
    #[inline]
    fn reserve(&mut self, additional: usize) {
        let _ = additional;
    }

    /// The output buffer, for an encoder that appends to one in memory.
    ///
    /// Internal: lets the map and set impls encode their entries in place
    /// instead of through a scratch buffer. Only [`Encoder`] returns
    /// `Some`; the default (`None`) is right for every other encoder,
    /// including wrappers, which must keep seeing every byte written.
    #[doc(hidden)]
    #[inline]
    fn in_memory_buffer(&mut self) -> Option<&mut Vec<u8>> {
        None
    }

    /// Append a `u64` as an unsigned LEB128 varint (1–10 bytes).
    ///
    /// # Errors
    ///
    /// Same as [`Encode::write_bytes`].
    #[inline]
    fn write_varint_u64(&mut self, value: u64) -> Result<()> {
        // Fast path for the overwhelmingly common case: value fits in a
        // single byte. Skips the stack buffer + write_bytes round-trip.
        if value < 0x80 {
            return self.write_byte(value as u8);
        }
        let mut buf = [0u8; varint::MAX_VARINT_LEN_U64];
        let n = varint::write_u64(value, &mut buf);
        self.write_bytes(&buf[..n])
    }

    /// Append a `u128` as an unsigned LEB128 varint (1–19 bytes).
    ///
    /// # Errors
    ///
    /// Same as [`Encode::write_bytes`].
    #[inline]
    fn write_varint_u128(&mut self, value: u128) -> Result<()> {
        let mut buf = [0u8; varint::MAX_VARINT_LEN_U128];
        let n = varint::write_u128(value, &mut buf);
        self.write_bytes(&buf[..n])
    }
}

/// Source that a [`Deserialize`] implementation reads its wire-format bytes
/// from.
///
/// Implemented by every concrete decoder in the crate ([`Decoder`] for the
/// in-memory case, `IoDecoder` (feature `std`) for `std::io::Read` streams). User
/// code rarely implements `Decode` directly — `Deserialize` impls are
/// written generically over `D: Decode`.
///
/// All methods are **total**: on any byte sequence they either succeed
/// (advancing the cursor) or return a [`SerialError`]. They never panic and
/// never read past the input. The provided methods never allocate more
/// than [`Decode::max_alloc`] for one value, and never size an allocation
/// from a declared length beyond what [`Decode::remaining_hint`] says the
/// input can supply.
///
/// # Implementing `Decode`
///
/// Only [`read_byte`](Decode::read_byte), [`read_into`](Decode::read_into)
/// and [`max_alloc`](Decode::max_alloc) are required. The other methods
/// have correct, bounded defaults. A custom decoder should additionally
/// override:
///
/// - [`remaining_hint`](Decode::remaining_hint) when it knows how much
///   input is left, so collection pre-allocation is bounded by it;
/// - [`config`](Decode::config), [`depth`](Decode::depth),
///   [`enter_nested`](Decode::enter_nested) and
///   [`leave_nested`](Decode::leave_nested) to enforce
///   [`Config::max_depth`] (the defaults do not track depth);
/// - [`consumed`](Decode::consumed) if it keeps the input in memory, so
///   canonical mode can check map key order;
/// - [`unbacked`](Decode::unbacked),
///   [`charge_unbacked`](Decode::charge_unbacked) and
///   [`reset_unbacked`](Decode::reset_unbacked) to keep the running total
///   that [`Config::total_alloc_limit`] caps across one decoded value, and
///   [`bytes_read`](Decode::bytes_read) so that elements occupying no input
///   bytes are detected (`bytes_read` already works when `consumed` is
///   overridden).
pub trait Decode {
    /// Read the next byte, advancing the cursor.
    ///
    /// # Errors
    ///
    /// Returns [`SerialError::UnexpectedEof`] if the input is exhausted.
    /// Streaming decoders MAY return an I/O-flavoured error variant.
    fn read_byte(&mut self) -> Result<u8>;

    /// Fill `out` with exactly `out.len()` bytes, advancing the cursor.
    ///
    /// # Errors
    ///
    /// Returns [`SerialError::UnexpectedEof`] on short read.
    fn read_into(&mut self, out: &mut [u8]) -> Result<()>;

    /// Maximum number of bytes the decoder will allocate for a single
    /// length-prefixed value. Mirrors [`Config::max_alloc`].
    fn max_alloc(&self) -> usize;

    /// An upper bound on the number of bytes that can still be read, when
    /// the decoder knows it.
    ///
    /// In-memory decoders return `Some(remaining)`. Streaming decoders
    /// return `None` (the default): the source length is unknown. Decode
    /// impls use the hint to refuse, before allocating, any declared length
    /// the input cannot possibly back.
    #[inline]
    fn remaining_hint(&self) -> Option<usize> {
        None
    }

    /// The configuration this decoder enforces.
    ///
    /// The default reports [`Config::default`] with this decoder's
    /// [`max_alloc`](Decode::max_alloc). Decoders built by this crate
    /// return the exact config they were constructed with.
    #[inline]
    fn config(&self) -> Config {
        Config::new().with_max_alloc(self.max_alloc())
    }

    /// Current nesting depth: the number of derived values whose fields are
    /// being decoded right now. Always `0` for decoders that do not track
    /// depth (the default).
    #[inline]
    fn depth(&self) -> usize {
        0
    }

    /// Record entry into one more level of nesting.
    ///
    /// Derived `Deserialize` / `DeserializeView` impls call this before
    /// decoding their fields and [`leave_nested`](Decode::leave_nested)
    /// afterwards. Hand-written impls for **recursive** types should do the
    /// same. The default implementation does not track depth.
    ///
    /// # Errors
    ///
    /// [`SerialError::DepthLimitExceeded`] when the decoder is already at
    /// [`Config::max_depth`].
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Decode, Deserialize, Result};
    ///
    /// // A hand-written recursive type: a linked list of bytes.
    /// struct List(Option<(u8, Vec<List>)>);
    ///
    /// impl Deserialize for List {
    ///     fn deserialize<D: Decode + ?Sized>(dec: &mut D) -> Result<Self> {
    ///         dec.enter_nested()?;
    ///         let inner = Option::<(u8, Vec<List>)>::deserialize(dec);
    ///         dec.leave_nested();
    ///         Ok(List(inner?))
    ///     }
    /// }
    ///
    /// // 10,000 levels of `Some((0, [ ... ]))` from 30 KB of input: refused
    /// // with an error instead of overflowing the stack.
    /// let mut hostile = Vec::new();
    /// for _ in 0..10_000 { hostile.extend_from_slice(&[1, 0, 1]); }
    /// hostile.push(0);
    /// let err = pack_io::decode::<List>(&hostile).err().unwrap();
    /// assert!(matches!(err, pack_io::SerialError::DepthLimitExceeded { .. }));
    /// ```
    #[inline]
    fn enter_nested(&mut self) -> Result<()> {
        Ok(())
    }

    /// Record exit from one level of nesting. Pairs with
    /// [`enter_nested`](Decode::enter_nested); call it on the error path
    /// too, so the decoder stays usable after a failed read.
    #[inline]
    fn leave_nested(&mut self) {}

    /// The bytes this decoder has consumed so far, when it keeps its input
    /// in memory.
    ///
    /// The in-memory [`Decoder`] returns `Some(&input[..position])`;
    /// streaming decoders return `None` (the default). Canonical mode uses
    /// it to compare the raw encoded keys of map and set entries.
    #[inline]
    fn consumed(&self) -> Option<&[u8]> {
        None
    }

    /// Number of input bytes this decoder has consumed so far, when it
    /// counts them.
    ///
    /// Collection decoders compare it before and after their elements to
    /// find elements that took no input bytes (zero-sized types, `Box<()>`,
    /// `Rc<()>`, structs whose fields are all `skip`, ...) and charge them
    /// with [`charge_unbacked`](Decode::charge_unbacked). The default
    /// derives it from [`consumed`](Decode::consumed). A decoder that
    /// returns `None` here opts out of that part of the accounting, so a
    /// hostile element count for such a type is then bounded only by
    /// [`max_alloc`](Decode::max_alloc) elements per collection; every
    /// decoder this crate ships counts.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Decode, Decoder};
    ///
    /// let mut dec = Decoder::new(&[1, 2, 3]);
    /// let _ = dec.read_byte().unwrap();
    /// assert_eq!(dec.bytes_read(), Some(1));
    /// ```
    #[inline]
    fn bytes_read(&self) -> Option<u64> {
        self.consumed().map(|bytes| bytes.len() as u64)
    }

    /// Memory, in bytes, charged so far for the value being decoded with
    /// [`charge_unbacked`](Decode::charge_unbacked). Always `0` for decoders
    /// that keep no running total (the default).
    #[inline]
    fn unbacked(&self) -> usize {
        0
    }

    /// Charge `bytes` of memory the value being decoded needs against
    /// [`Config::total_alloc_limit`].
    ///
    /// The built-in impls call this for every collection slot, every owned
    /// string or byte buffer, every boxed value, and for collection
    /// elements that consumed no input. A hand-written `Deserialize` that
    /// allocates from a count it read should do the same. The decoders
    /// this crate ships keep a running total and refuse the decode once it
    /// would pass the limit, so the memory one value builds is bounded as a
    /// whole, not per collection. The default keeps no total and refuses
    /// only a single charge above the limit.
    ///
    /// The name predates the 2.1 total budget, when only elements that
    /// consumed no input were charged.
    ///
    /// # Errors
    ///
    /// [`SerialError::InvalidLength`] (with `declared` set to the memory
    /// total in bytes) when the limit would be exceeded.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Config, Decode, Decoder, SerialError};
    ///
    /// let mut dec = Decoder::with_config(&[], Config::new().with_max_alloc(100)).unwrap();
    /// dec.charge_unbacked(60).unwrap();
    /// assert_eq!(dec.unbacked(), 60);
    /// assert!(matches!(dec.charge_unbacked(41), Err(SerialError::InvalidLength { .. })));
    /// ```
    #[inline]
    fn charge_unbacked(&mut self, bytes: usize) -> Result<()> {
        if bytes > self.config().total_alloc_limit() {
            return Err(unbacked_limit_error(bytes, self.remaining_hint()));
        }
        Ok(())
    }

    /// Clear the running total of [`charge_unbacked`](Decode::charge_unbacked),
    /// starting the budget of a new top-level value.
    ///
    /// [`Decoder::read`] and `IoDecoder::read` (feature `std`) call it
    /// before each top-level value, so a long-lived decoder reading one
    /// message after another gives each message its own budget. Call it
    /// yourself between values when you decode with `T::deserialize`
    /// directly on one decoder. The default does nothing.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Config, Decode, Decoder};
    ///
    /// let mut dec = Decoder::with_config(&[], Config::new().with_max_alloc(100)).unwrap();
    /// dec.charge_unbacked(60).unwrap();
    /// dec.reset_unbacked();
    /// assert_eq!(dec.unbacked(), 0);
    /// ```
    #[inline]
    fn reset_unbacked(&mut self) {}

    /// Read exactly `len` bytes.
    ///
    /// In-memory decoders return a borrow of their input
    /// ([`Cow::Borrowed`], no copy). The default implementation reads into
    /// a fresh buffer that grows in bounded steps as the bytes actually
    /// arrive, so a large declared `len` over a short or stalled stream
    /// costs at most a small first chunk, not `len` bytes. When
    /// [`remaining_hint`](Decode::remaining_hint) is known, a `len` beyond
    /// it is refused before anything is allocated.
    ///
    /// This method does not check `len` against
    /// [`max_alloc`](Decode::max_alloc); callers that took `len` from the
    /// wire must do that first (see
    /// [`read_length_prefixed`](Decode::read_length_prefixed)).
    ///
    /// # Errors
    ///
    /// [`SerialError::UnexpectedEof`] if the input ends first.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Decode, Decoder};
    ///
    /// let mut dec = Decoder::new(&[1, 2, 3, 4]);
    /// assert_eq!(&*dec.read_byte_cow(3).unwrap(), &[1, 2, 3]);
    /// assert!(dec.read_byte_cow(2).is_err()); // only one byte left
    /// ```
    fn read_byte_cow(&mut self, len: usize) -> Result<Cow<'_, [u8]>> {
        read_bytes_incrementally(self, len).map(Cow::Owned)
    }

    /// Read a LEB128 varint as a `u64`.
    ///
    /// # Errors
    ///
    /// - [`SerialError::VarintOverflow`] for an encoding wider than 10
    ///   bytes or one whose value overflows `u64`.
    /// - [`SerialError::UnexpectedEof`] for a truncated encoding.
    /// - [`SerialError::NonCanonical`] for a multi-byte encoding with a
    ///   redundant zero final byte, when [`Config::canonical`] is on.
    #[inline]
    fn read_varint_u64(&mut self) -> Result<u64> {
        // Fast path for single-byte varints (values 0..=127, the
        // overwhelmingly common case for length prefixes and small ints).
        // It is the 1.0 decoder byte for byte: no configuration is read.
        let first = self.read_byte()?;
        if first < 0x80 {
            return Ok(u64::from(first));
        }
        let mut result: u64 = u64::from(first & 0x7f);
        let mut shift: u32 = 7;
        for consumed in 2..=varint::MAX_VARINT_LEN_U64 {
            let byte = self.read_byte()?;
            // The 10th byte may only set bit 0 — anything else overflows u64.
            if consumed == varint::MAX_VARINT_LEN_U64 && (byte & 0xfe) != 0 {
                return Err(SerialError::VarintOverflow);
            }
            result |= u64::from(byte & 0x7f) << shift;
            if byte & 0x80 == 0 {
                // A zero final byte after a continuation byte adds nothing:
                // the value has a shorter encoding. Only then is the
                // configuration consulted, so lenient decoding pays one
                // compare on multi-byte varints and nothing else. (The check
                // stays inline: an out-of-line call here, even a cold one,
                // measurably slowed every decode that inlines this loop.)
                if byte == 0 && self.config().canonical {
                    return Err(SerialError::NonCanonical { reason: "varint" });
                }
                return Ok(result);
            }
            shift += 7;
        }
        Err(SerialError::VarintOverflow)
    }

    /// Read a LEB128 varint as a `u128`.
    ///
    /// # Errors
    ///
    /// See [`Decode::read_varint_u64`] (the width limit is 19 bytes).
    #[inline]
    fn read_varint_u128(&mut self) -> Result<u128> {
        let mut result: u128 = 0;
        let mut shift: u32 = 0;
        for consumed in 1..=varint::MAX_VARINT_LEN_U128 {
            let byte = self.read_byte()?;
            // The 19th byte may only set the low two bits.
            if consumed == varint::MAX_VARINT_LEN_U128 && (byte & 0xfc) != 0 {
                return Err(SerialError::VarintOverflow);
            }
            result |= u128::from(byte & 0x7f) << shift;
            if byte & 0x80 == 0 {
                if consumed > 1 && byte == 0 && self.config().canonical {
                    return Err(SerialError::NonCanonical { reason: "varint" });
                }
                return Ok(result);
            }
            shift += 7;
        }
        Err(SerialError::VarintOverflow)
    }

    /// Read a length-prefixed byte run into a fresh `Vec<u8>`.
    ///
    /// The length is read as a varint, validated against
    /// [`Decode::max_alloc`] and charged against the total budget
    /// ([`Decode::charge_unbacked`]); the bytes are then read with
    /// [`Decode::read_byte_cow`], which never allocates ahead of the data
    /// that actually exists.
    ///
    /// # Errors
    ///
    /// - [`SerialError::InvalidLength`] if the prefix exceeds `max_alloc`
    ///   or the remaining total budget.
    /// - [`SerialError::UnexpectedEof`] if the source runs out before the
    ///   declared length is satisfied.
    #[inline]
    fn read_length_prefixed(&mut self) -> Result<Vec<u8>> {
        let declared = self.read_varint_u64()?;
        let len = guard_owned_bytes(declared, self)?;
        self.read_byte_cow(len).map(Cow::into_owned)
    }
}

/// LEB128 `u64` decode loop specialised at compile time: with `CANONICAL`
/// a redundant zero final byte is rejected, without it the loop is exactly
/// the lenient 1.0 decoder. The integer `deserialize_many` impls read the
/// canonical flag once per collection and call the matching instance, so
/// lenient integer arrays pay nothing for canonical mode existing.
#[inline]
pub(crate) fn read_varint_u64_with<D: Decode + ?Sized, const CANONICAL: bool>(
    decoder: &mut D,
) -> Result<u64> {
    let first = decoder.read_byte()?;
    if first < 0x80 {
        return Ok(u64::from(first));
    }
    let mut result: u64 = u64::from(first & 0x7f);
    let mut shift: u32 = 7;
    for consumed in 2..=varint::MAX_VARINT_LEN_U64 {
        let byte = decoder.read_byte()?;
        // The 10th byte may only set bit 0 — anything else overflows u64.
        if consumed == varint::MAX_VARINT_LEN_U64 && (byte & 0xfe) != 0 {
            return Err(SerialError::VarintOverflow);
        }
        result |= u64::from(byte & 0x7f) << shift;
        if byte & 0x80 == 0 {
            if CANONICAL && byte == 0 {
                return Err(SerialError::NonCanonical { reason: "varint" });
            }
            return Ok(result);
        }
        shift += 7;
    }
    Err(SerialError::VarintOverflow)
}

/// The default body of [`Decode::read_byte_cow`]: read `len` bytes into a
/// buffer that doubles (starting at [`PREALLOC_BYTES`]) only after the
/// previous chunk has been filled from the source.
///
/// Peak allocation is therefore at most about twice the bytes the source
/// actually delivered, plus one chunk — never the declared `len`.
fn read_bytes_incrementally<D: Decode + ?Sized>(decoder: &mut D, len: usize) -> Result<Vec<u8>> {
    if let Some(remaining) = decoder.remaining_hint() {
        if len > remaining {
            return Err(SerialError::UnexpectedEof {
                needed: len,
                remaining,
            });
        }
    }
    // The first chunk comes from a zeroed allocation (cheap for the common
    // short value); later chunks double only after the previous one filled.
    let mut filled = len.min(PREALLOC_BYTES);
    let mut out: Vec<u8> = alloc::vec![0u8; filled];
    decoder.read_into(&mut out)?;
    while filled < len {
        let target = len.min(filled.saturating_mul(2).max(PREALLOC_BYTES));
        out.resize(target, 0);
        decoder.read_into(&mut out[filled..target])?;
        filled = target;
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// In-memory Encoder
// ---------------------------------------------------------------------------

/// In-memory encoder. Writes into an owned `Vec<u8>`; the buffer can be
/// reused across encodes by calling [`Encoder::take`] to swap it out.
///
/// Implements [`Encode`], so [`Serialize`] impls written generically over
/// `E: Encode` work directly through it.
///
/// # Examples
///
/// ```
/// use pack_io::Encoder;
///
/// let mut enc = Encoder::new();
/// enc.write(&7_u64).unwrap();
/// enc.write(&"hello").unwrap();
/// let bytes = enc.into_inner();
/// assert!(bytes.len() > 0);
/// ```
#[derive(Debug, Default)]
pub struct Encoder {
    out: Vec<u8>,
}

impl Encoder {
    /// Construct an encoder with an empty output buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// let enc = pack_io::Encoder::new();
    /// assert!(enc.as_bytes().is_empty());
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self { out: Vec::new() }
    }

    /// Construct an encoder with an output buffer pre-allocated to
    /// `capacity` bytes.
    ///
    /// Choose this over [`Encoder::new`] when the encoded size is roughly
    /// known: a single `Vec::with_capacity` up front avoids the four to
    /// eight grow-and-copy reallocations that a zero-capacity `Vec`
    /// performs while doubling to the final size.
    ///
    /// `capacity` is a hint — the encoder still grows the buffer if the
    /// encoded value exceeds it. Setting it slightly too high is harmless;
    /// setting it slightly too low costs at most one growth.
    ///
    /// The Tier-1 [`crate::encode`] free function uses a small default
    /// capacity internally so most one-shot encodes never grow at all.
    ///
    /// # Examples
    ///
    /// ```
    /// let enc = pack_io::Encoder::with_capacity(256);
    /// assert!(enc.as_bytes().is_empty());
    /// ```
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            out: Vec::with_capacity(capacity),
        }
    }

    /// Construct an encoder backed by `buffer`. The encoder appends to the
    /// buffer rather than allocating its own — callers that re-use a single
    /// `Vec<u8>` across many encodes avoid the per-call allocation.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Encoder;
    ///
    /// let buf = Vec::with_capacity(64);
    /// let mut enc = Encoder::into_buffer(buf);
    /// enc.write(&42_u64).unwrap();
    /// let buf = enc.into_inner();
    /// assert!(!buf.is_empty());
    /// ```
    #[must_use]
    pub fn into_buffer(buffer: Vec<u8>) -> Self {
        Self { out: buffer }
    }

    /// Borrow the encoded bytes accumulated so far, without consuming the
    /// encoder.
    ///
    /// Prefer this over [`Encoder::into_inner`] when the caller wants to
    /// send / hash / inspect the bytes but keep writing more values.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Encoder;
    ///
    /// let mut enc = Encoder::new();
    /// enc.write(&1_u64).unwrap();
    /// let snapshot = enc.as_bytes().to_vec();
    /// enc.write(&2_u64).unwrap();
    /// assert_eq!(snapshot, &[0x01]);
    /// assert_eq!(enc.as_bytes(), &[0x01, 0x02]);
    /// ```
    #[inline]
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.out
    }

    /// Consume the encoder and return its underlying buffer.
    ///
    /// The returned `Vec<u8>` is the exact bytes accumulated by every
    /// preceding `write` call.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Encoder;
    ///
    /// let mut enc = Encoder::new();
    /// enc.write(&7_u64).unwrap();
    /// let bytes: Vec<u8> = enc.into_inner();
    /// assert_eq!(bytes, &[0x07]);
    /// ```
    #[inline]
    #[must_use]
    pub fn into_inner(self) -> Vec<u8> {
        self.out
    }

    /// Swap the encoder's buffer with a fresh empty one, returning the bytes
    /// written so far. Useful for "encode then send" loops that want to
    /// re-use the encoder across many messages.
    ///
    /// After `take`, the encoder is empty and ready to encode the next
    /// message. The returned buffer is the previous contents.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Encoder;
    ///
    /// let mut enc = Encoder::new();
    /// enc.write(&1_u64).unwrap();
    /// let first = enc.take();
    /// enc.write(&2_u64).unwrap();
    /// let second = enc.take();
    /// assert_eq!(first, &[0x01]);
    /// assert_eq!(second, &[0x02]);
    /// assert!(enc.as_bytes().is_empty());
    /// ```
    #[must_use]
    pub fn take(&mut self) -> Vec<u8> {
        core::mem::take(&mut self.out)
    }

    /// Encode `value`, appending its bytes to the internal buffer.
    ///
    /// This is the [`Serialize`]-aware sibling of [`Encode::write_bytes`].
    /// Prefer it for typed values; reserve `write_bytes` for raw byte
    /// passthrough.
    ///
    /// # Errors
    ///
    /// Propagates any error returned by the type's [`Serialize`]
    /// implementation. The built-in primitive and collection impls in this
    /// crate never error on an in-memory encoder.
    ///
    /// # Examples
    ///
    /// Writing several values in sequence:
    ///
    /// ```
    /// use pack_io::Encoder;
    ///
    /// let mut enc = Encoder::new();
    /// enc.write(&7_u64).unwrap();
    /// enc.write(&"hello").unwrap();
    /// enc.write(&vec![1u8, 2, 3]).unwrap();
    /// assert!(!enc.as_bytes().is_empty());
    /// ```
    ///
    /// Encoding a tuple in one call (anything that implements `Serialize`
    /// works):
    ///
    /// ```
    /// use pack_io::Encoder;
    ///
    /// let mut enc = Encoder::new();
    /// enc.write(&(7_u64, "hello", true)).unwrap();
    /// assert!(!enc.as_bytes().is_empty());
    /// ```
    #[inline]
    pub fn write<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<()> {
        value.serialize(self)
    }
}

impl Encode for Encoder {
    #[inline(always)]
    fn write_byte(&mut self, byte: u8) -> Result<()> {
        self.out.push(byte);
        Ok(())
    }

    #[inline(always)]
    fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> {
        self.out.extend_from_slice(bytes);
        Ok(())
    }

    #[inline(always)]
    fn reserve(&mut self, additional: usize) {
        self.out.reserve(additional);
    }

    #[inline]
    fn in_memory_buffer(&mut self) -> Option<&mut Vec<u8>> {
        Some(&mut self.out)
    }

    /// Override of [`Encode::write_varint_u64`] specialised for the in-memory
    /// encoder. Pushes each varint byte directly onto the underlying `Vec`,
    /// reserving the full max-width up front so the loop never re-checks
    /// capacity. Avoids the stack-buffer + `extend_from_slice` round-trip
    /// the default impl would perform.
    #[inline]
    fn write_varint_u64(&mut self, value: u64) -> Result<()> {
        if value < 0x80 {
            self.out.push(value as u8);
            return Ok(());
        }
        // Up to 10 bytes for u64. Reserve once, then push without further
        // capacity checks.
        self.out.reserve(varint::MAX_VARINT_LEN_U64);
        let mut n = value;
        while n >= 0x80 {
            self.out.push((n as u8) | 0x80);
            n >>= 7;
        }
        self.out.push(n as u8);
        Ok(())
    }

    /// Same specialisation as [`Encode::write_varint_u64`], widened to 128
    /// bits.
    #[inline]
    fn write_varint_u128(&mut self, value: u128) -> Result<()> {
        if value < 0x80 {
            self.out.push(value as u8);
            return Ok(());
        }
        self.out.reserve(varint::MAX_VARINT_LEN_U128);
        let mut n = value;
        while n >= 0x80 {
            self.out.push((n as u8) | 0x80);
            n >>= 7;
        }
        self.out.push(n as u8);
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// In-memory Decoder
// ---------------------------------------------------------------------------

/// In-memory decoder. Borrows from an input slice and advances a position
/// pointer as values are read. Bounds-checked on every operation.
///
/// Implements [`Decode`], so [`Deserialize`] impls written generically over
/// `D: Decode` work directly through it.
///
/// # Examples
///
/// ```
/// 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_eq!(n, 7);
/// assert!(b);
/// assert!(dec.is_empty());
/// ```
#[derive(Debug)]
pub struct Decoder<'a> {
    input: &'a [u8],
    pos: usize,
    config: Config,
    depth: usize,
    unbacked: usize,
}

impl<'a> Decoder<'a> {
    /// Construct a decoder over `bytes` using the default [`Config`]
    /// (1 GiB `max_alloc`).
    ///
    /// For tighter allocation limits on untrusted input, use
    /// [`Decoder::with_config`] instead.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Decoder;
    ///
    /// let bytes = pack_io::encode(&42_u64).unwrap();
    /// let mut dec = Decoder::new(&bytes);
    /// let n: u64 = dec.read().unwrap();
    /// assert_eq!(n, 42);
    /// ```
    #[inline]
    #[must_use]
    pub fn new(bytes: &'a [u8]) -> Self {
        Self {
            input: bytes,
            pos: 0,
            config: Config::new(),
            depth: 0,
            unbacked: 0,
        }
    }

    /// Construct a decoder with the supplied configuration.
    ///
    /// Use this when the input comes from an untrusted producer and the
    /// caller wants to bound per-value allocations.
    ///
    /// # Errors
    ///
    /// [`SerialError::InvalidConfig`] if `config.max_alloc == 0` or
    /// `config.max_depth == 0`.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Config, Decoder};
    ///
    /// let cfg = Config::new().with_max_alloc(16 * 1024); // 16 KiB cap
    /// let bytes = pack_io::encode(&"hello").unwrap();
    /// let mut dec = Decoder::with_config(&bytes, cfg).unwrap();
    /// let s: String = dec.read().unwrap();
    /// assert_eq!(s, "hello");
    /// ```
    pub fn with_config(bytes: &'a [u8], config: Config) -> Result<Self> {
        Ok(Self {
            input: bytes,
            pos: 0,
            config: config.validate()?,
            depth: 0,
            unbacked: 0,
        })
    }

    /// Decoder over a sub-slice that inherits `config`, the current nesting
    /// `depth` and the running [`Decode::unbacked`] total of the decoder it
    /// was carved out of. Used for the bodies of versioned structs, so a
    /// body is decoded under exactly the limits the caller asked for.
    #[inline]
    pub(crate) fn nested(bytes: &'a [u8], config: Config, depth: usize, unbacked: usize) -> Self {
        Self {
            input: bytes,
            pos: 0,
            config,
            depth,
            unbacked,
        }
    }

    /// The configuration this decoder enforces.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Config, Decoder};
    ///
    /// let cfg = Config::new().with_max_depth(8);
    /// let dec = Decoder::with_config(&[], cfg).unwrap();
    /// assert_eq!(dec.config().max_depth, 8);
    /// ```
    #[inline]
    #[must_use]
    pub fn config(&self) -> Config {
        self.config
    }

    /// Bytes consumed so far from the start of the input.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Decoder;
    ///
    /// let bytes = pack_io::encode(&(1_u8, 2_u8)).unwrap();
    /// let mut dec = Decoder::new(&bytes);
    /// assert_eq!(dec.position(), 0);
    /// let _: u8 = dec.read().unwrap();
    /// assert_eq!(dec.position(), 1);
    /// ```
    #[inline]
    #[must_use]
    pub fn position(&self) -> usize {
        self.pos
    }

    /// Number of bytes remaining in the input.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Decoder;
    ///
    /// let bytes = pack_io::encode(&(1_u8, 2_u8, 3_u8)).unwrap();
    /// let mut dec = Decoder::new(&bytes);
    /// assert_eq!(dec.remaining(), 3);
    /// let _: u8 = dec.read().unwrap();
    /// assert_eq!(dec.remaining(), 2);
    /// ```
    #[inline]
    #[must_use]
    pub fn remaining(&self) -> usize {
        self.input.len().saturating_sub(self.pos)
    }

    /// True when there are no more bytes to read.
    ///
    /// Useful as the loop condition for multi-value decode passes that
    /// don't have an explicit count up front.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::{Encoder, Decoder};
    ///
    /// let mut enc = Encoder::new();
    /// enc.write(&1_u64).unwrap();
    /// enc.write(&2_u64).unwrap();
    /// let bytes = enc.into_inner();
    ///
    /// let mut dec = Decoder::new(&bytes);
    /// let mut sum = 0_u64;
    /// while !dec.is_empty() {
    ///     sum += dec.read::<u64>().unwrap();
    /// }
    /// assert_eq!(sum, 3);
    /// ```
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.remaining() == 0
    }

    /// Decode a value of type `T` from the current position, advancing the
    /// cursor by the number of bytes the value consumed.
    ///
    /// Each top-level call starts a fresh [`Config::total_alloc_limit`]
    /// budget for the value it decodes.
    ///
    /// # Errors
    ///
    /// Returns any [`SerialError`] surfaced by `T::deserialize`.
    ///
    /// # Examples
    ///
    /// Reading several values in sequence:
    ///
    /// ```
    /// use pack_io::{Encoder, Decoder};
    ///
    /// let mut enc = Encoder::new();
    /// enc.write(&7_u64).unwrap();
    /// enc.write(&"hello").unwrap();
    /// let bytes = enc.into_inner();
    ///
    /// let mut dec = Decoder::new(&bytes);
    /// let n: u64 = dec.read().unwrap();
    /// let s: String = dec.read().unwrap();
    /// assert_eq!((n, s.as_str()), (7, "hello"));
    /// ```
    ///
    /// Reading a tuple in a single call:
    ///
    /// ```
    /// use pack_io::Decoder;
    ///
    /// let bytes = pack_io::encode(&(7_u64, true)).unwrap();
    /// let mut dec = Decoder::new(&bytes);
    /// let pair: (u64, bool) = dec.read().unwrap();
    /// assert_eq!(pair, (7, true));
    /// ```
    #[inline]
    pub fn read<T: Deserialize>(&mut self) -> Result<T> {
        if self.depth == 0 {
            self.unbacked = 0;
        }
        T::deserialize(self)
    }

    /// Read a length-prefixed byte run as a **borrowed** slice of the
    /// underlying input — no allocation, no copy.
    ///
    /// The borrowed slice has the same lifetime `'a` as the decoder's
    /// input buffer, which lets caller-side `&'a str` / `&'a [u8]` decode
    /// paths return a borrow directly into that buffer. This is the seam
    /// the zero-copy [`crate::DeserializeView`] surface plugs into for
    /// `&'a str` and `&'a [u8]`.
    ///
    /// # Errors
    ///
    /// - [`SerialError::InvalidLength`] if the prefix exceeds the
    ///   configured `max_alloc`, OR exceeds the remaining input.
    /// - [`SerialError::UnexpectedEof`] is folded into `InvalidLength` for
    ///   this method, since the buffer length is known up front and a
    ///   declared length running off the end is logically a length-prefix
    ///   error, not a streaming EOF.
    #[inline]
    pub fn read_length_prefixed_borrowed(&mut self) -> Result<&'a [u8]> {
        let declared = <Self as Decode>::read_varint_u64(self)?;
        let remaining = self.remaining();
        let max = self.config.max_alloc as u64;
        if declared > max || declared > remaining as u64 {
            return Err(SerialError::InvalidLength {
                declared,
                remaining,
            });
        }
        // `declared <= remaining`, so the conversion is lossless.
        self.read_borrowed(declared as usize)
    }

    /// Read exactly `len` bytes as a **borrowed** slice of the input — no
    /// length prefix, no allocation, no copy.
    ///
    /// Use it for fixed-size fields whose length is known from the type or
    /// from an earlier field.
    ///
    /// # Errors
    ///
    /// [`SerialError::UnexpectedEof`] if fewer than `len` bytes remain; the
    /// cursor does not move.
    ///
    /// # Examples
    ///
    /// ```
    /// use pack_io::Decoder;
    ///
    /// let mut dec = Decoder::new(&[1, 2, 3, 4]);
    /// let head: &[u8] = dec.read_borrowed(3).unwrap();
    /// assert_eq!(head, &[1, 2, 3]);
    /// assert_eq!(dec.remaining(), 1);
    /// ```
    #[inline]
    pub fn read_borrowed(&mut self, len: usize) -> Result<&'a [u8]> {
        let remaining = self.remaining();
        if len > remaining {
            return Err(SerialError::UnexpectedEof {
                needed: len,
                remaining,
            });
        }
        let start = self.pos;
        let end = start + len;
        let input: &'a [u8] = self.input;
        self.pos = end;
        Ok(&input[start..end])
    }
}

impl Decode for Decoder<'_> {
    #[inline]
    fn read_byte(&mut self) -> Result<u8> {
        match self.input.get(self.pos) {
            Some(&b) => {
                self.pos += 1;
                Ok(b)
            }
            None => Err(SerialError::UnexpectedEof {
                needed: 1,
                remaining: 0,
            }),
        }
    }

    #[inline]
    fn read_into(&mut self, out: &mut [u8]) -> Result<()> {
        let n = out.len();
        let remaining = self.remaining();
        if n > remaining {
            return Err(SerialError::UnexpectedEof {
                needed: n,
                remaining,
            });
        }
        let start = self.pos;
        let end = start + n;
        out.copy_from_slice(&self.input[start..end]);
        self.pos = end;
        Ok(())
    }

    #[inline]
    fn max_alloc(&self) -> usize {
        self.config.max_alloc
    }

    #[inline]
    fn remaining_hint(&self) -> Option<usize> {
        Some(self.remaining())
    }

    #[inline]
    fn config(&self) -> Config {
        self.config
    }

    #[inline]
    fn depth(&self) -> usize {
        self.depth
    }

    #[inline]
    fn enter_nested(&mut self) -> Result<()> {
        if self.depth >= self.config.max_depth {
            return Err(SerialError::DepthLimitExceeded {
                max_depth: self.config.max_depth,
            });
        }
        self.depth += 1;
        Ok(())
    }

    #[inline]
    fn leave_nested(&mut self) {
        self.depth = self.depth.saturating_sub(1);
    }

    #[inline]
    fn consumed(&self) -> Option<&[u8]> {
        self.input.get(..self.pos)
    }

    #[inline]
    fn bytes_read(&self) -> Option<u64> {
        Some(self.pos as u64)
    }

    #[inline]
    fn unbacked(&self) -> usize {
        self.unbacked
    }

    #[inline]
    fn charge_unbacked(&mut self, bytes: usize) -> Result<()> {
        let total = self.unbacked.saturating_add(bytes);
        if total > self.config.total_alloc_limit() {
            return Err(unbacked_limit_error(total, Some(self.remaining())));
        }
        self.unbacked = total;
        Ok(())
    }

    #[inline]
    fn reset_unbacked(&mut self) {
        self.unbacked = 0;
    }

    /// In-memory specialisation: a bounds check and a borrow of the input.
    #[inline]
    fn read_byte_cow(&mut self, len: usize) -> Result<Cow<'_, [u8]>> {
        self.read_borrowed(len).map(Cow::Borrowed)
    }

    /// In-memory specialisation: validates the length against the actual
    /// buffer as well as `max_alloc`, so a truncated input is rejected
    /// without allocating, charges it against the total budget, then
    /// copies exactly the declared bytes.
    #[inline]
    fn read_length_prefixed(&mut self) -> Result<Vec<u8>> {
        let bytes = self.read_length_prefixed_borrowed()?;
        if !bytes.is_empty() {
            self.charge_unbacked(bytes.len())?;
        }
        Ok(bytes.to_vec())
    }
}

// ---------------------------------------------------------------------------
// Tier-1 free functions
// ---------------------------------------------------------------------------

/// Encode `value` into a freshly allocated `Vec<u8>`.
///
/// This is the **Tier-1** entry point — the one-line surface for the common
/// case. Allocates one buffer with room for 512 bytes up front (measured
/// faster than an exact-size pre-pass), which grows if the value is larger.
/// To allocate exactly once at the exact size, use
/// `Encoder::with_capacity(value.encoded_len()?)`.
///
/// # Examples
///
/// ```
/// let bytes = pack_io::encode(&42_u64).unwrap();
/// let back: u64 = pack_io::decode(&bytes).unwrap();
/// assert_eq!(back, 42);
/// ```
///
/// # Errors
///
/// Propagates any error returned by the type's [`Serialize`] implementation.
/// The built-in primitive and collection impls never error on an in-memory
/// encoder.
#[inline]
pub fn encode<T: Serialize + ?Sized>(value: &T) -> Result<Vec<u8>> {
    // Pre-reserve enough to hold a typical small-to-medium message in a
    // single allocation. A zero-capacity `Vec` doubles 8+ times before
    // hitting 512 bytes, with each doubling memcpy-ing the prior contents
    // — accounting for a large fraction of the encode-time gap vs codecs
    // that pre-size their output buffer. 512 bytes covers most network
    // messages without growth; larger payloads pay at most one or two
    // doublings instead of the eight-plus a fresh `Vec` would.
    let mut enc = Encoder::with_capacity(512);
    value.serialize(&mut enc)?;
    Ok(enc.into_inner())
}

/// Peek the schema version of a payload produced by a `#[pack_io(version = N)]`
/// type without consuming the buffer.
///
/// Reads only the leading varint and returns it as `u32`, leaving the
/// caller free to dispatch decode to the right `T` based on what they find.
/// On a non-versioned payload (no `#[pack_io(version = N)]` on the type)
/// this returns whatever the first varint of the encoding happens to be —
/// callers should only use it on payloads they know are versioned. The
/// value is not validated: a leading `0` peeks as `Ok(0)`, although every
/// decoder rejects version `0` with [`SerialError::InvalidVersion`].
///
/// # Examples
///
/// ```
/// # #[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);
/// # }
/// ```
///
/// # Errors
///
/// - [`SerialError::UnexpectedEof`] if `bytes` is empty or the leading
///   varint is truncated.
/// - [`SerialError::VarintOverflow`] / [`SerialError::IntegerOutOfRange`]
///   if the leading varint does not fit in `u32`.
#[inline]
pub fn peek_version(bytes: &[u8]) -> Result<u32> {
    let mut dec = Decoder::new(bytes);
    let v = dec.read_varint_u64()?;
    u32::try_from(v).map_err(|_| SerialError::IntegerOutOfRange)
}

/// Decode a value of type `T` from `bytes`, requiring the input to be fully
/// consumed.
///
/// This is the **Tier-1** entry point — the one-line surface for the common
/// case. After the value has been read, the decoder checks that no bytes
/// remain; trailing input is reported as [`SerialError::TrailingBytes`].
/// Callers that want to read several values from a single buffer should use
/// [`Decoder`] directly.
///
/// # Examples
///
/// ```
/// let bytes = pack_io::encode(&"hello").unwrap();
/// let back: String = pack_io::decode(&bytes).unwrap();
/// assert_eq!(back, "hello");
/// ```
///
/// # Errors
///
/// - Returns [`SerialError::TrailingBytes`] when extra bytes follow the value.
/// - Propagates any [`SerialError`] from the type's [`Deserialize`] impl.
#[inline]
pub fn decode<T: Deserialize>(bytes: &[u8]) -> Result<T> {
    let mut dec = Decoder::new(bytes);
    let value = T::deserialize(&mut dec)?;
    finish(&dec, value)
}

/// [`decode`] under an explicit [`Config`]: decode a value of type `T` from
/// `bytes`, requiring the input to be fully consumed.
///
/// Use it to tighten [`Config::max_alloc`] / [`Config::max_depth`] for
/// untrusted input, or to turn on [`Config::canonical`] where the bytes are
/// hashed or signed.
///
/// # Examples
///
/// ```
/// use pack_io::{Config, SerialError, decode_with_config, encode};
///
/// let bytes = encode(&vec![0u8; 1024]).unwrap();
/// let tight = Config::new().with_max_alloc(256);
/// let err = decode_with_config::<Vec<u8>>(&bytes, tight).unwrap_err();
/// assert!(matches!(err, SerialError::InvalidLength { declared: 1024, .. }));
/// ```
///
/// # Errors
///
/// - Any error [`Decoder::with_config`] reports for an invalid `config`.
/// - [`SerialError::TrailingBytes`] when extra bytes follow the value.
/// - Any [`SerialError`] from the type's [`Deserialize`] impl.
#[inline]
pub fn decode_with_config<T: Deserialize>(bytes: &[u8], config: Config) -> Result<T> {
    let mut dec = Decoder::with_config(bytes, config)?;
    let value = T::deserialize(&mut dec)?;
    finish(&dec, value)
}

/// Reject trailing input after a whole-buffer decode.
#[inline]
pub(crate) fn finish<T>(dec: &Decoder<'_>, value: T) -> Result<T> {
    let remaining = dec.remaining();
    if remaining != 0 {
        return Err(SerialError::TrailingBytes { remaining });
    }
    Ok(value)
}

// ---------------------------------------------------------------------------
// Size counting
// ---------------------------------------------------------------------------

/// Number of bytes the LEB128 varint encoding of `value` occupies (1–10).
///
/// Handy for hand-written [`Serialize::encoded_len`] overrides, which must
/// account for every length prefix exactly.
///
/// # Examples
///
/// ```
/// assert_eq!(pack_io::varint_len(0), 1);
/// assert_eq!(pack_io::varint_len(127), 1);
/// assert_eq!(pack_io::varint_len(128), 2);
/// assert_eq!(pack_io::varint_len(u64::MAX), 10);
/// assert_eq!(pack_io::varint_len(300), pack_io::encode(&300_u64).unwrap().len());
/// ```
#[inline]
#[must_use]
pub const fn varint_len(value: u64) -> usize {
    // 1 byte per started group of 7 significant bits; zero still takes 1.
    let bits = 64 - (value | 1).leading_zeros() as usize;
    bits.div_ceil(7)
}

/// An [`Encode`] sink that discards bytes and only counts them. Backs the
/// default [`Serialize::encoded_len`].
#[derive(Debug, Default)]
pub struct SizeCounter {
    len: usize,
}

impl SizeCounter {
    /// Bytes counted so far.
    #[inline]
    pub fn count(&self) -> usize {
        self.len
    }
}

impl Encode for SizeCounter {
    #[inline]
    fn write_byte(&mut self, _byte: u8) -> Result<()> {
        self.len = self.len.saturating_add(1);
        Ok(())
    }

    #[inline]
    fn write_bytes(&mut self, bytes: &[u8]) -> Result<()> {
        self.len = self.len.saturating_add(bytes.len());
        Ok(())
    }

    #[inline]
    fn write_varint_u64(&mut self, value: u64) -> Result<()> {
        self.len = self.len.saturating_add(varint_len(value));
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn config_default_has_one_gib_cap() {
        let cfg = Config::default();
        assert_eq!(cfg.max_alloc, 1 << 30);
    }

    #[test]
    fn decoder_with_zero_cap_is_rejected() {
        let cfg = Config::new().with_max_alloc(0);
        let err = Decoder::with_config(&[], cfg).expect_err("zero cap is invalid");
        assert!(matches!(err, SerialError::InvalidConfig { .. }));
    }

    #[test]
    fn encoder_into_buffer_reuses_caller_vec() {
        let mut buf = Vec::with_capacity(64);
        buf.push(0xff);
        let mut enc = Encoder::into_buffer(buf);
        enc.write(&7_u64).unwrap();
        let out = enc.into_inner();
        assert_eq!(out[0], 0xff);
        assert!(out.len() > 1);
    }

    #[test]
    fn encoder_take_returns_buffer_and_resets() {
        let mut enc = Encoder::new();
        enc.write(&1_u64).unwrap();
        let first = enc.take();
        assert!(!first.is_empty());
        assert!(enc.as_bytes().is_empty());

        enc.write(&2_u64).unwrap();
        let second = enc.take();
        assert_eq!(second, [0x02]);
    }

    #[test]
    fn decode_rejects_trailing_bytes() {
        let mut bytes = encode(&7_u8).unwrap();
        bytes.push(0xff);
        let err = decode::<u8>(&bytes).expect_err("trailing bytes should fail");
        assert!(matches!(err, SerialError::TrailingBytes { remaining: 1 }));
    }

    #[test]
    fn decoder_read_past_end_returns_unexpected_eof() {
        let mut dec = Decoder::new(&[0x01]);
        let _: u8 = dec.read().unwrap();
        let err = dec.read::<u8>().expect_err("past end should fail");
        assert!(matches!(err, SerialError::UnexpectedEof { .. }));
    }

    #[test]
    fn decoder_length_prefix_above_cap_is_rejected() {
        let cfg = Config::new().with_max_alloc(4);
        let bytes = [0x05, b'h', b'e', b'l', b'l', b'o'];
        let mut dec = Decoder::with_config(&bytes, cfg).expect("non-zero cap");
        let err = dec
            .read_length_prefixed()
            .expect_err("length > cap should fail");
        assert!(matches!(
            err,
            SerialError::InvalidLength { declared: 5, .. }
        ));
    }

    #[test]
    fn decoder_length_prefix_overflowing_remaining_is_rejected() {
        let bytes = [0x10, b'a', b'b'];
        let mut dec = Decoder::new(&bytes);
        let err = dec
            .read_length_prefixed()
            .expect_err("length > remaining should fail");
        assert!(matches!(err, SerialError::InvalidLength { .. }));
    }

    #[test]
    fn decoder_position_advances_with_reads() {
        let bytes = [0x01, 0x02, 0x03];
        let mut dec = Decoder::new(&bytes);
        assert_eq!(dec.position(), 0);
        let _ = dec.read_byte().unwrap();
        assert_eq!(dec.position(), 1);
        let mut buf = [0u8; 2];
        dec.read_into(&mut buf).unwrap();
        assert_eq!(dec.position(), 3);
        assert!(dec.is_empty());
    }

    #[test]
    fn test_config_builders_and_validation() {
        let cfg = Config::new()
            .with_max_alloc(10)
            .with_max_depth(3)
            .with_canonical(true);
        assert_eq!((cfg.max_alloc, cfg.max_depth, cfg.canonical), (10, 3, true));
        assert_eq!(Config::default(), Config::new());
        assert!(matches!(
            Decoder::with_config(&[], Config::new().with_max_depth(0)),
            Err(SerialError::InvalidConfig { .. })
        ));
    }

    #[test]
    fn test_varint_len_matches_the_encoder_at_every_boundary() {
        for shift in 0..64 {
            for v in [
                1u64 << shift,
                (1u64 << shift) - 1,
                (1u64 << shift).wrapping_add(1),
            ] {
                let mut enc = Encoder::new();
                enc.write_varint_u64(v).unwrap();
                assert_eq!(varint_len(v), enc.as_bytes().len(), "{v}");
            }
        }
        assert_eq!(varint_len(u64::MAX), 10);
    }

    /// A minimal third-party decoder: only the three required methods.
    struct Minimal<'a> {
        bytes: &'a [u8],
        reads: usize,
    }

    impl Decode for Minimal<'_> {
        fn read_byte(&mut self) -> Result<u8> {
            let (first, rest) = self.bytes.split_first().ok_or(SerialError::UnexpectedEof {
                needed: 1,
                remaining: 0,
            })?;
            self.bytes = rest;
            Ok(*first)
        }

        fn read_into(&mut self, out: &mut [u8]) -> Result<()> {
            if out.len() > self.bytes.len() {
                return Err(SerialError::UnexpectedEof {
                    needed: out.len(),
                    remaining: self.bytes.len(),
                });
            }
            self.reads += 1;
            let (head, rest) = self.bytes.split_at(out.len());
            out.copy_from_slice(head);
            self.bytes = rest;
            Ok(())
        }

        fn max_alloc(&self) -> usize {
            1 << 30
        }
    }

    #[test]
    fn test_provided_methods_work_for_a_minimal_decoder() {
        let big = alloc::vec![7u8; PREALLOC_BYTES * 3 + 5];
        let mut encoded = encode(&big).unwrap();
        encoded.extend(encode(&"tail").unwrap());
        let mut dec = Minimal {
            bytes: &encoded,
            reads: 0,
        };
        // The incremental reader fills in chunks (64 KiB, then doubling).
        let back: Vec<u8> = Deserialize::deserialize(&mut dec).unwrap();
        assert_eq!(back, big);
        assert!(dec.reads >= 2, "read in chunks, got {}", dec.reads);
        let tail: alloc::string::String = Deserialize::deserialize(&mut dec).unwrap();
        assert_eq!(tail, "tail");
        // Defaults: no depth tracking, no consumed bytes, default config.
        assert_eq!(dec.depth(), 0);
        assert!(dec.enter_nested().is_ok());
        dec.leave_nested();
        assert!(dec.consumed().is_none());
        assert_eq!(dec.remaining_hint(), None);
        assert_eq!(dec.config().max_alloc, 1 << 30);
    }

    /// A decoder that counts how often its configuration is consulted.
    struct CountingConfig<'a> {
        inner: Decoder<'a>,
        config_calls: core::cell::Cell<usize>,
    }

    impl Decode for CountingConfig<'_> {
        fn read_byte(&mut self) -> Result<u8> {
            self.inner.read_byte()
        }

        fn read_into(&mut self, out: &mut [u8]) -> Result<()> {
            self.inner.read_into(out)
        }

        fn max_alloc(&self) -> usize {
            self.inner.max_alloc()
        }

        fn config(&self) -> Config {
            self.config_calls.set(self.config_calls.get() + 1);
            self.inner.config()
        }
    }

    /// PACK-R04: the varint hot path must not read the configuration.
    /// 2.0.0 checked `config().canonical` on every varint, which cost
    /// lenient decoding about 10 % on small-integer vectors.
    #[test]
    fn test_varint_decode_consults_config_only_for_overlong_encodings() {
        let mut bytes = Vec::new();
        for v in [0u64, 1, 127, 128, 300, 16_384, u32::MAX as u64, u64::MAX] {
            bytes.extend(encode(&v).unwrap());
        }
        for strict in [false, true] {
            let config = Config::new().with_canonical(strict);
            let mut dec = CountingConfig {
                inner: Decoder::with_config(&bytes, config).unwrap(),
                config_calls: core::cell::Cell::new(0),
            };
            for _ in 0..8 {
                let _ = dec.read_varint_u64().unwrap();
            }
            assert_eq!(dec.config_calls.get(), 0, "canonical = {strict}");
        }
        // Only a redundant zero final byte reaches the configuration.
        let overlong = [0x80, 0x00];
        for (strict, expected) in [
            (false, Ok(0)),
            (true, Err(SerialError::NonCanonical { reason: "varint" })),
        ] {
            let mut dec = CountingConfig {
                inner: Decoder::with_config(&overlong, Config::new().with_canonical(strict))
                    .unwrap(),
                config_calls: core::cell::Cell::new(0),
            };
            assert_eq!(dec.read_varint_u64(), expected);
            assert_eq!(dec.config_calls.get(), 1);
        }
    }

    #[test]
    fn test_unbacked_charges_accumulate_and_stop_at_max_alloc() {
        let cfg = Config::new().with_max_alloc(10);
        let mut dec = Decoder::with_config(&[], cfg).unwrap();
        dec.charge_unbacked(4).unwrap();
        dec.charge_unbacked(6).unwrap();
        assert_eq!(dec.unbacked(), 10);
        assert!(matches!(
            dec.charge_unbacked(1),
            Err(SerialError::InvalidLength { declared: 11, .. })
        ));
        assert_eq!(dec.unbacked(), 10, "a refused charge is not recorded");
        // The provided default keeps no total: it refuses only a single
        // charge above `max_alloc`.
        let mut minimal = Minimal {
            bytes: &[],
            reads: 0,
        };
        minimal.charge_unbacked(1 << 30).unwrap();
        minimal.charge_unbacked(1 << 30).unwrap();
        assert!(minimal.charge_unbacked((1 << 30) + 1).is_err());
        assert_eq!(minimal.unbacked(), 0);
        assert_eq!(minimal.bytes_read(), None);
    }

    #[test]
    fn test_canonical_mode_rejects_overlong_u128_varints() {
        let strict = Config::new().with_canonical(true);
        let mut dec = Decoder::with_config(&[0x80, 0x00], strict).unwrap();
        assert_eq!(
            dec.read_varint_u128(),
            Err(SerialError::NonCanonical { reason: "varint" })
        );
        let mut dec = Decoder::new(&[0x80, 0x00]);
        assert_eq!(dec.read_varint_u128(), Ok(0));
    }

    #[test]
    fn test_read_borrowed_and_consumed() {
        let mut dec = Decoder::new(&[1, 2, 3]);
        assert_eq!(dec.read_borrowed(2).unwrap(), &[1, 2]);
        assert_eq!(dec.consumed(), Some(&[1u8, 2][..]));
        assert!(matches!(
            dec.read_borrowed(2),
            Err(SerialError::UnexpectedEof {
                needed: 2,
                remaining: 1
            })
        ));
        assert_eq!(dec.position(), 2, "a failed read does not move the cursor");
    }

    #[test]
    fn test_size_counter_counts_without_storing() {
        let mut counter = SizeCounter::default();
        counter.write_bytes(&[0; 10]).unwrap();
        counter.write_byte(1).unwrap();
        counter.write_varint_u64(300).unwrap();
        assert_eq!(counter.count(), 13);
    }

    #[test]
    fn read_into_short_read_is_rejected() {
        let mut dec = Decoder::new(&[0x01, 0x02]);
        let mut buf = [0u8; 4];
        let err = dec.read_into(&mut buf).expect_err("short read");
        assert!(matches!(err, SerialError::UnexpectedEof { .. }));
    }
}