buffa 0.9.0

A pure Rust Protocol Buffers implementation with first-class editions support
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
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
//! The core [`Message`] trait and [`DecodeOptions`] builder.
//!
//! Every generated message type implements [`Message`], which provides
//! encode/decode/merge methods and a two-pass serialization model
//! (`compute_size` populates a [`SizeCache`](crate::SizeCache), `write_to`
//! consumes it) that avoids the exponential-time problem affecting naïve
//! length-delimited encoders.

use crate::encode_sink::EncodeSink;
use bytes::Buf;

use crate::error::{DecodeError, EncodeError};
use crate::message_field::DefaultInstance;

/// Default recursion depth limit for decoding nested messages.
///
/// Protobuf implementations are required to enforce a recursion limit to
/// prevent stack overflow from deeply nested messages in untrusted input.
/// This value (100) matches the limit used by the official protobuf
/// implementations and the protobuf conformance suite.
///
/// Pass this constant as the depth when constructing a [`DecodeContext`] for
/// a top-level [`Message::merge`] call.  The provided convenience methods
/// ([`Message::decode`], [`Message::decode_from_slice`],
/// [`Message::merge_from_slice`]) use this limit automatically.
pub const RECURSION_LIMIT: u32 = 100;

/// Default limit on unknown fields decoded per top-level decode: 1,000,000.
///
/// Bounds the number of [`UnknownField`](crate::UnknownField) values the
/// decoder will materialize in a single top-level decode, independent of
/// the input size. Without this bound, wire data can force allocation far
/// in excess of its own size: every 2-byte unknown varint field
/// materialises a ~40-byte `UnknownField`, a ~20× amplification, so a
/// 64 MiB payload of unknown fields would otherwise force over 1 GiB of
/// heap. The count limit caps that overhead at roughly `limit × 40` bytes
/// (~40 MB at the default); unknown length-delimited *payload* bytes are
/// not counted against the limit because they are already bounded by the
/// input size, which [`DecodeOptions::with_max_message_size`] governs.
///
/// A million unknown fields is far more than any realistic
/// forward-compatibility scenario needs. Raise the limit with
/// [`DecodeOptions::with_unknown_field_limit`] if you decode trusted
/// messages that legitimately carry more (e.g. a proxy forwarding messages
/// with a huge unpacked repeated field from a much newer schema).
pub const DEFAULT_UNKNOWN_FIELD_LIMIT: usize = 1_000_000;

/// Default element-memory budget: 32 MiB per top-level decode.
///
/// Bounds the memory a single decode may materialize in the elements of
/// length-delimited containers — repeated message, string and bytes fields, and
/// map entries — independent of the input size. These amplify the same way unknown fields do, and further: an
/// empty repeated message element is 2 wire bytes and materializes
/// `size_of::<T>()` in the `Vec` it lands in, measured at 256 bytes for a
/// message of a few `Vec`/`String` fields — a 128x ratio, so 4 MiB of such
/// elements would otherwise force ~512 MiB. Empty `bytes` and `string`
/// elements amplify 16x and 12x by the same route.
///
/// A map entry is charged for both halves, key and value: an omitted message
/// value still materializes in the map, and a few bytes of key buy a distinct
/// slot, so `map<string, Message>` amplifies exactly as a repeated message does.
///
/// Only the element footprint is counted. The *contents* of a string or bytes
/// element are not, being already bounded by the input size that
/// [`DecodeOptions::with_max_message_size`] governs, and packed scalars are not
/// charged at all: their worst case is a 1-byte varint becoming a 4-byte `i32`,
/// which is not an amplification vector, and bounding them would reject
/// legitimate columnar payloads that carry millions of elements by design.
///
/// 32 MiB of elements is far more than a realistic message carries, and sits
/// alongside what [`DEFAULT_UNKNOWN_FIELD_LIMIT`] already permits (~38 MiB of
/// `UnknownField`). Raise it with
/// [`DecodeOptions::with_element_memory_limit`] for trusted inputs that
/// legitimately decode into more. Note `Vec` grows by doubling, so peak
/// resident memory can reach roughly twice the budget; this bounds what is
/// materialized, not what the allocator reserves.
pub const DEFAULT_ELEMENT_MEMORY_LIMIT: usize = 32 * 1024 * 1024;

/// Per-decode limits threaded through every merge call.
///
/// Carries the remaining recursion depth and a shared unknown-field
/// allowance. The context is `Copy` — passing it to a callee hands over the
/// current depth by value, while the unknown-field allowance lives in a
/// [`Cell`](core::cell::Cell) owned by the top-level decode entry point, so
/// every field decoded under one entry point draws from the same
/// allowance.
///
/// Constructed automatically by the [`Message`] convenience methods
/// ([`decode`](Message::decode), [`decode_from_slice`](Message::decode_from_slice),
/// [`merge_from_slice`](Message::merge_from_slice)) and by [`DecodeOptions`].
/// Construct one manually only when calling [`Message::merge`] or the other
/// depth-threading methods directly — and construct a **fresh limit cell
/// per top-level decode**. Reusing one cell across decode calls makes the
/// limit cumulative: each call drains it further until every decode fails
/// with [`DecodeError::UnknownFieldLimitExceeded`].
///
/// ```rust
/// # use buffa::__doctest_fixtures::Person;
/// use core::cell::Cell;
/// use buffa::{DecodeContext, Message, DEFAULT_UNKNOWN_FIELD_LIMIT, RECURSION_LIMIT};
///
/// # fn example(mut bytes: &[u8]) -> Result<(), buffa::DecodeError> {
/// let limit = Cell::new(DEFAULT_UNKNOWN_FIELD_LIMIT);
/// let mut msg = Person::default();
/// msg.merge(&mut bytes, DecodeContext::new(RECURSION_LIMIT, &limit))?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug)]
pub struct DecodeContext<'a> {
    depth: u32,
    unknown_fields_remaining: &'a core::cell::Cell<usize>,
    element_memory_remaining: Option<&'a core::cell::Cell<usize>>,
}

impl<'a> DecodeContext<'a> {
    /// Create a context with `depth` remaining recursion levels and the
    /// remaining unknown-field allowance stored in `unknown_field_limit`.
    #[must_use]
    pub fn new(depth: u32, unknown_field_limit: &'a core::cell::Cell<usize>) -> Self {
        Self {
            depth,
            unknown_fields_remaining: unknown_field_limit,
            element_memory_remaining: None,
        }
    }

    /// Attach the shared element-memory budget in `element_memory_limit`.
    ///
    /// Without this the budget is absent and [`register_element_memory`] is a
    /// no-op, which is what a [`DecodeContext::new`] built elsewhere — older
    /// generated code, say — gets. buffa's own entry points attach it, so a
    /// decode through [`DecodeOptions`] or the [`Message`] conveniences is
    /// bounded by [`DEFAULT_ELEMENT_MEMORY_LIMIT`].
    ///
    /// Attach a **fresh cell per top-level decode**, for the same reason the
    /// unknown-field allowance needs one: a reused cell drains across calls.
    ///
    /// [`register_element_memory`]: DecodeContext::register_element_memory
    #[must_use]
    pub fn with_element_memory(
        mut self,
        element_memory_limit: &'a core::cell::Cell<usize>,
    ) -> Self {
        self.element_memory_remaining = Some(element_memory_limit);
        self
    }

    /// The remaining recursion depth.
    #[must_use]
    pub fn depth(&self) -> u32 {
        self.depth
    }

    /// The number of additional unknown fields this decode may materialize.
    #[must_use]
    pub fn remaining_unknown_fields(&self) -> usize {
        self.unknown_fields_remaining.get()
    }

    /// Consume one level of recursion depth.
    ///
    /// # Errors
    ///
    /// Returns [`DecodeError::RecursionLimitExceeded`] when the depth budget
    /// is exhausted.
    pub fn descend(self) -> Result<Self, DecodeError> {
        let depth = self
            .depth
            .checked_sub(1)
            .ok_or(DecodeError::RecursionLimitExceeded)?;
        Ok(Self { depth, ..self })
    }

    /// Consume one slot of the shared unknown-field allowance.
    ///
    /// Call **before** materializing an [`UnknownField`](crate::UnknownField).
    ///
    /// # Errors
    ///
    /// Returns [`DecodeError::UnknownFieldLimitExceeded`] (leaving the
    /// allowance unchanged) when no slots remain.
    pub fn register_unknown_field(&self) -> Result<(), DecodeError> {
        let remaining = self.unknown_fields_remaining.get();
        if remaining == 0 {
            return Err(DecodeError::UnknownFieldLimitExceeded);
        }
        self.unknown_fields_remaining.set(remaining - 1);
        Ok(())
    }

    /// The element-memory budget left to this decode, or `None` when no budget
    /// is attached.
    #[must_use]
    pub fn remaining_element_memory(&self) -> Option<usize> {
        self.element_memory_remaining.map(core::cell::Cell::get)
    }

    /// Charge `bytes` against the shared element-memory budget.
    ///
    /// Call **before** materializing an element of a repeated
    /// length-delimited field, passing that element's `size_of`. Those are the
    /// fields where the wire is far cheaper than what it decodes into: an empty
    /// message element is two bytes and costs `size_of::<T>()`, so a payload
    /// well inside [`DecodeOptions::with_max_message_size`] can still expand by
    /// two orders of magnitude. Packed scalars are not charged — their worst
    /// case is a 1-byte varint becoming a 4-byte `i32`, and bounding them would
    /// reject legitimate columnar payloads.
    ///
    /// No-op when no budget is attached (see
    /// [`with_element_memory`](DecodeContext::with_element_memory)).
    ///
    /// # Errors
    ///
    /// Returns [`DecodeError::ElementMemoryLimitExceeded`] (leaving the budget
    /// unchanged) when `bytes` exceeds what remains.
    pub fn register_element_memory(&self, bytes: usize) -> Result<(), DecodeError> {
        let Some(cell) = self.element_memory_remaining else {
            return Ok(());
        };
        let remaining = cell.get();
        if bytes > remaining {
            return Err(DecodeError::ElementMemoryLimitExceeded);
        }
        cell.set(remaining - bytes);
        Ok(())
    }
}

/// Maximum encoded size of a protobuf message: 2 GiB − 1 (`0x7FFF_FFFF`).
///
/// The protobuf specification limits any message — top-level or nested — to
/// 2 GiB. buffa enforces this symmetrically:
///
/// - **Decode** rejects length-delimited payloads declared larger than this
///   with [`DecodeError::MessageTooLarge`], matching protobuf C++ and Java.
/// - **Encode** refuses to serialize a message whose encoded size would
///   exceed it: the panicking entry points ([`Message::encode`] and friends)
///   panic, the `try_*` twins ([`Message::try_encode`] and friends) return
///   [`EncodeError::MessageTooLarge`]. Without this check a writer could
///   produce bytes that no conforming decoder — including buffa's own —
///   will read back.
pub const MAX_MESSAGE_BYTES: u32 = 0x7FFF_FFFF;

/// Saturate a `u64` size accumulator to `u32`.
///
/// Generated `compute_size` implementations accumulate in `u64` (which
/// cannot overflow for any message that fits in memory) and saturate to
/// `u32` once at each message node's return. A saturated value is
/// necessarily greater than [`MAX_MESSAGE_BYTES`], so any over-limit
/// message — whether from one huge field or from aggregation — surfaces at
/// the encode entry points' size check; the byte-exact value is preserved
/// for every message the wire format can actually represent.
///
/// Manual [`Message`] implementations should use the same pattern:
/// accumulate the encoded size in a `u64` and `saturate_size` it at return.
#[inline]
#[must_use]
pub fn saturate_size(size: u64) -> u32 {
    u32::try_from(size).unwrap_or(u32::MAX)
}

/// Validate a computed encode size against [`MAX_MESSAGE_BYTES`].
///
/// The error-returning half of the encode-size funnel: the `try_encode*`
/// methods (and generated inherent encode entry points, e.g. on lazy view
/// types) validate their [`compute_size`](Message::compute_size) result
/// through this before writing anything.
///
/// # Errors
///
/// Returns [`EncodeError::MessageTooLarge`] if `size` exceeds
/// [`MAX_MESSAGE_BYTES`].
#[inline]
pub fn checked_encode_size(size: u32) -> Result<u32, EncodeError> {
    if size > MAX_MESSAGE_BYTES {
        Err(EncodeError::MessageTooLarge)
    } else {
        Ok(size)
    }
}

/// Debug-build two-pass coherence ledger: asserts `write_to` produced
/// exactly the byte count `compute_size` declared.
///
/// Called by the provided `encode_to_vec` / `encode_to_bytes` entry points
/// (and their generated lazy-view counterparts) after the write pass. The
/// write pass is ground truth — leaf writers emit `len as u64` prefixes and
/// full payloads — so any divergence indicates a size-pass bug (wrong
/// presence check, traversal drift) in a generated or manual
/// implementation. Free in release builds.
#[doc(hidden)]
#[inline]
#[track_caller]
pub fn debug_assert_two_pass(written: usize, declared: usize) {
    debug_assert_eq!(
        written, declared,
        "write_to produced a different byte count than compute_size \
         declared (two-pass traversal mismatch)"
    );
}

/// Panic shim shared by every panicking encode entry point — the provided
/// `Message` / `ViewEncode` methods, `SizeCachePool`, and generated
/// lazy-view inherent methods all delegate to their `try_*` twin and route
/// the `Err` here, so the panicking and fallible paths cannot diverge.
///
/// # Panics
///
/// Always — that is its entire job: one cold, out-of-line panic site with
/// the canonical over-limit message.
#[doc(hidden)]
#[cold]
#[inline(never)]
pub fn encode_size_overflow() -> ! {
    panic!(
        "message encoded size exceeds the 2 GiB protobuf limit \
         (the try_* variant of this method returns this as an error instead)"
    )
}

/// The core trait implemented by all protobuf message types.
///
/// This trait is implemented by **generated code** — you write a `.proto` file,
/// codegen emits the Rust struct and its `Message` impl. You should almost
/// never implement this trait by hand.
///
/// # Manual implementation is discouraged
///
/// The only reason to implement `Message` yourself is when you need a
/// custom in-memory representation that codegen cannot produce — for
/// example, wrapping a `std::ops::Range<i64>` as a leaf message so the
/// rest of your code uses the natural Rust type. If you just want a message
/// type, **write a `.proto` file instead.**
///
/// Manual implementation is intentionally high-friction:
/// - You must correctly implement the two-pass serialization contract
///   (`compute_size` populates the [`SizeCache`](crate::SizeCache) in the
///   same traversal order that `write_to` consumes it).
/// - You must implement wire-format decoding in `merge_field`.
/// - You must implement the [`DefaultInstance`] supertrait, which provides
///   the lazily-initialized static default that [`MessageField`](crate::MessageField)
///   dereferences to when unset.
///
/// If you still need to do this, see the [custom types section of the
/// user guide](https://github.com/anthropics/buffa/blob/main/docs/guide.md#custom-type-implementations)
/// for a complete worked example.
///
/// # Serialization model
///
/// Serialization is a two-pass process to avoid the exponential-time problem
/// that affects prost with deeply nested messages:
///
/// 1. **`compute_size()`** — walks the message tree and records the encoded
///    size of every length-delimited sub-message in a [`SizeCache`].
/// 2. **`write_to()`** — walks the tree again, writing bytes and consuming
///    cached sizes for length-prefixed sub-messages.
///
/// The provided [`encode`](Self::encode) method performs both passes with a
/// fresh [`SizeCache`] — most callers use that and never touch the cache
/// directly. `compute_size` / `write_to` take the cache explicitly so that
/// manual `Message` implementations can thread it through nested-message
/// recursion.
///
/// # Thread safety
///
/// `Message` requires `Send + Sync`. Generated structs contain no interior
/// mutability — serialization state lives in the external [`SizeCache`], not
/// in the message — so messages can be placed in an `Arc` and shared across
/// threads freely. `merge` requires `&mut self`, so mutation is exclusive.
///
/// # Struct evolution policy
///
/// Generated message structs (and their [`MessageView`](crate::MessageView) /
/// [`LazyMessageView`](crate::LazyMessageView) counterparts) may gain fields
/// across releases — both when the source `.proto` schema evolves and when
/// buffa adds internal bookkeeping such as `__buffa_unknown_fields` or the
/// required-field presence bitmaps. **Exhaustive struct literals and
/// exhaustive destructuring patterns are not covered by buffa's semver
/// guarantees**: code that names every field will fail to compile when a field
/// is added, and that breakage is not considered a breaking change.
///
/// The forward-compatible ways to construct a generated struct are:
///
/// - decode it from bytes;
/// - struct-update syntax over the default: `Foo { x, y, ..Default::default() }`;
/// - start from `Foo::default()` and assign fields (or call generated `with_*`
///   setters when `generate_with_setters` is enabled).
///
/// The structs are deliberately *not* `#[non_exhaustive]`, so struct-update
/// syntax remains available from downstream crates; this policy is a documented
/// contract rather than a compiler-enforced one.
///
/// [`SizeCache`]: crate::SizeCache
pub trait Message: DefaultInstance + Clone + PartialEq + Send + Sync {
    /// Compute the encoded byte size of this message, recording nested
    /// sub-message sizes in `cache` for `write_to` to consume.
    ///
    /// Most callers should use [`encode`](Self::encode) instead, which runs
    /// both passes with a fresh cache. Manual `Message` implementations call
    /// this recursively on nested message fields, wrapping each call in
    /// [`SizeCache::reserve`] / [`SizeCache::set`] for length-delimited
    /// fields — see the user guide's custom-types section for the pattern.
    ///
    /// # Size limit
    ///
    /// The protobuf specification limits messages to 2 GiB
    /// ([`MAX_MESSAGE_BYTES`]). Generated implementations accumulate in
    /// `u64` and saturate the return value via [`saturate_size`], so an
    /// over-limit message yields a return greater than [`MAX_MESSAGE_BYTES`]
    /// rather than a wrapped value; the provided encode methods check this
    /// and refuse to produce over-limit output. Manual implementations must
    /// follow the same pattern — if their arithmetic can wrap, over-limit
    /// messages may encode corrupt bytes that bypass the check.
    ///
    /// [`SizeCache::reserve`]: crate::SizeCache::reserve
    /// [`SizeCache::set`]: crate::SizeCache::set
    fn compute_size(&self, cache: &mut crate::SizeCache) -> u32;

    /// Write this message's encoded bytes to a buffer, consuming
    /// nested-message sizes from `cache` (populated by a prior
    /// `compute_size` call on the same cache).
    ///
    /// Most callers should use [`encode`](Self::encode) instead. This is a
    /// low-level primitive: the 2 GiB size check ([`MAX_MESSAGE_BYTES`])
    /// lives in the provided encode entry points, so callers driving
    /// `compute_size` / `write_to` directly must validate the size
    /// themselves (via [`checked_encode_size`]).
    fn write_to(&self, cache: &mut crate::SizeCache, buf: &mut impl EncodeSink);

    /// Compute size, then write. This is the primary encoding API.
    ///
    /// The sink can be any [`BufMut`](bytes::BufMut) (contiguous output) or
    /// a [`Rope`](crate::Rope), which captures large `bytes::Bytes` fields
    /// as reference-counted segments for zero-copy handoff to networking
    /// code — see [`encode_sink`](crate::encode_sink).
    ///
    /// # Panics
    ///
    /// Panics if the encoded size exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]) — see [`try_encode`](Self::try_encode) for
    /// the error-returning variant.
    #[inline]
    fn encode(&self, buf: &mut impl EncodeSink) {
        self.try_encode(buf)
            .unwrap_or_else(|_| encode_size_overflow())
    }

    /// Encode, returning an error instead of panicking if the encoded size
    /// exceeds the 2 GiB protobuf limit ([`MAX_MESSAGE_BYTES`]).
    ///
    /// On `Err`, nothing is written to `buf`.
    ///
    /// # Errors
    ///
    /// Returns [`EncodeError::MessageTooLarge`] if the encoded size exceeds
    /// [`MAX_MESSAGE_BYTES`].
    fn try_encode(&self, buf: &mut impl EncodeSink) -> Result<(), EncodeError> {
        let mut cache = crate::SizeCache::new();
        checked_encode_size(self.compute_size(&mut cache))?;
        self.write_to(&mut cache, buf);
        Ok(())
    }

    /// Encode using a caller-supplied [`SizeCache`](crate::SizeCache), for
    /// reuse across many encodes in a hot loop. Clears the cache first.
    ///
    /// # Panics
    ///
    /// Panics if the encoded size exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]) — see
    /// [`try_encode_with_cache`](Self::try_encode_with_cache) for the
    /// error-returning variant.
    #[inline]
    fn encode_with_cache(&self, cache: &mut crate::SizeCache, buf: &mut impl EncodeSink) {
        self.try_encode_with_cache(cache, buf)
            .unwrap_or_else(|_| encode_size_overflow())
    }

    /// Encode with a caller-supplied [`SizeCache`](crate::SizeCache),
    /// returning an error instead of panicking if the encoded size exceeds
    /// the 2 GiB protobuf limit ([`MAX_MESSAGE_BYTES`]). Clears the cache
    /// first.
    ///
    /// On `Err`, nothing is written to `buf`.
    ///
    /// # Errors
    ///
    /// Returns [`EncodeError::MessageTooLarge`] if the encoded size exceeds
    /// [`MAX_MESSAGE_BYTES`].
    fn try_encode_with_cache(
        &self,
        cache: &mut crate::SizeCache,
        buf: &mut impl EncodeSink,
    ) -> Result<(), EncodeError> {
        cache.clear();
        checked_encode_size(self.compute_size(cache))?;
        self.write_to(cache, buf);
        Ok(())
    }

    /// Encode this message into `buf` only if its encoded size fits within
    /// `max_bytes`, using a single size pass that is then reused for the write.
    ///
    /// This avoids the double tree-walk that `try_encoded_len` + `encode`
    /// would require: one `compute_size` pass populates the [`SizeCache`];
    /// the budget check happens before `write_to` runs, so on `Err` nothing
    /// is written to `buf`.
    ///
    /// Returns the encoded body length on success (excludes any length prefix
    /// you add for framing) — useful for metrics or frame sizing. Note that
    /// this return type is `u32`, unlike `try_encode`'s `()`. `max_bytes` is
    /// also `u32` to match the encode-size domain; callers with a `usize`
    /// budget can cast with `u32::try_from(budget).unwrap_or(u32::MAX)`.
    ///
    /// # Errors
    ///
    /// - [`EncodeError::MessageTooLarge`] if the encoded size exceeds the
    ///   2 GiB protobuf limit ([`MAX_MESSAGE_BYTES`]).
    ///   `MessageTooLarge` takes precedence if both limits are exceeded.
    /// - [`EncodeError::ExceedsBudget`] if the encoded size is within the
    ///   protobuf limit but exceeds `max_bytes`.
    ///
    /// [`SizeCache`]: crate::SizeCache
    fn try_encode_bounded(
        &self,
        max_bytes: u32,
        buf: &mut impl EncodeSink,
    ) -> Result<u32, EncodeError> {
        let mut cache = crate::SizeCache::new();
        self.try_encode_bounded_with_cache(max_bytes, &mut cache, buf)
    }

    /// Like [`try_encode_bounded`](Self::try_encode_bounded) but reuses an
    /// existing [`SizeCache`], clearing it first.
    ///
    /// Prefer [`SizeCachePool::try_encode_bounded`](crate::SizeCachePool::try_encode_bounded)
    /// for hot-loop use — the pool amortizes the cache's spill allocation.
    ///
    /// # Errors
    ///
    /// Same as [`try_encode_bounded`](Self::try_encode_bounded).
    ///
    /// [`SizeCache`]: crate::SizeCache
    fn try_encode_bounded_with_cache(
        &self,
        max_bytes: u32,
        cache: &mut crate::SizeCache,
        buf: &mut impl EncodeSink,
    ) -> Result<u32, EncodeError> {
        cache.clear();
        let len = checked_encode_size(self.compute_size(cache))?;
        if len > max_bytes {
            return Err(EncodeError::ExceedsBudget { len, max_bytes });
        }
        self.write_to(cache, buf);
        Ok(len)
    }

    /// Compute the encoded byte size of this message.
    ///
    /// Walks the message tree, discarding the intermediate [`SizeCache`].
    /// If you also intend to encode, prefer [`encode`](Self::encode) or
    /// [`encode_to_vec`](Self::encode_to_vec) — they do a single size pass
    /// and reuse the cache for the write.
    ///
    /// # Panics
    ///
    /// Panics if the encoded size exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]) — see [`try_encoded_len`](Self::try_encoded_len)
    /// for the error-returning variant.
    ///
    /// [`SizeCache`]: crate::SizeCache
    #[inline]
    #[must_use]
    fn encoded_len(&self) -> u32 {
        self.try_encoded_len()
            .unwrap_or_else(|_| encode_size_overflow())
    }

    /// Compute the encoded byte size, returning an error instead of
    /// panicking if it exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]).
    ///
    /// # Errors
    ///
    /// Returns [`EncodeError::MessageTooLarge`] if the encoded size exceeds
    /// [`MAX_MESSAGE_BYTES`].
    fn try_encoded_len(&self) -> Result<u32, EncodeError> {
        checked_encode_size(self.compute_size(&mut crate::SizeCache::new()))
    }

    /// Encode this message as a length-delimited byte sequence.
    ///
    /// # Panics
    ///
    /// Panics if the encoded size exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]); the check runs before the length prefix is
    /// written, so nothing reaches `buf` on failure. See
    /// [`try_encode_length_delimited`](Self::try_encode_length_delimited)
    /// for the error-returning variant.
    #[inline]
    fn encode_length_delimited(&self, buf: &mut impl EncodeSink) {
        self.try_encode_length_delimited(buf)
            .unwrap_or_else(|_| encode_size_overflow())
    }

    /// Encode as a length-delimited byte sequence, returning an error
    /// instead of panicking if the encoded size exceeds the 2 GiB protobuf
    /// limit ([`MAX_MESSAGE_BYTES`]).
    ///
    /// On `Err`, nothing is written to `buf`.
    ///
    /// # Errors
    ///
    /// Returns [`EncodeError::MessageTooLarge`] if the encoded size exceeds
    /// [`MAX_MESSAGE_BYTES`].
    fn try_encode_length_delimited(&self, buf: &mut impl EncodeSink) -> Result<(), EncodeError> {
        let mut cache = crate::SizeCache::new();
        let len = checked_encode_size(self.compute_size(&mut cache))?;
        crate::encoding::encode_varint(len as u64, buf);
        self.write_to(&mut cache, buf);
        Ok(())
    }

    /// Encode this message to a new `Vec<u8>`.
    ///
    /// # Panics
    ///
    /// Panics if the encoded size exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]) — see
    /// [`try_encode_to_vec`](Self::try_encode_to_vec) for the
    /// error-returning variant. In debug builds, also panics if a manual
    /// implementation's `write_to` produces a different byte count than
    /// its `compute_size` declared.
    // Direct body rather than delegating to try_encode_to_vec: LLVM does
    // not fold the Result<Vec<u8>> niche away even under full inlining, so
    // the delegating form re-checks the capacity sentinel and round-trips
    // the Vec through a Result temp in every caller — measured +7.5% on
    // dense-small-message encode (google_message1, quieted metal,
    // layout-normalized). Same for encode_to_bytes. The unit- and
    // scalar-returning entry points delegate — their Results stay in
    // registers and fold cleanly.
    #[inline]
    #[must_use]
    fn encode_to_vec(&self) -> alloc::vec::Vec<u8> {
        let mut cache = crate::SizeCache::new();
        let size = match checked_encode_size(self.compute_size(&mut cache)) {
            Ok(size) => size as usize,
            Err(_) => encode_size_overflow(),
        };
        let mut buf = alloc::vec::Vec::with_capacity(size);
        self.write_to(&mut cache, &mut buf);
        debug_assert_two_pass(buf.len(), size);
        buf
    }

    /// Encode to a new `Vec<u8>`, returning an error instead of panicking
    /// if the encoded size exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]).
    ///
    /// # Errors
    ///
    /// Returns [`EncodeError::MessageTooLarge`] if the encoded size exceeds
    /// [`MAX_MESSAGE_BYTES`].
    ///
    /// # Panics
    ///
    /// In debug builds, panics if a manual implementation's `write_to`
    /// produces a different byte count than its `compute_size` declared.
    fn try_encode_to_vec(&self) -> Result<alloc::vec::Vec<u8>, EncodeError> {
        let mut cache = crate::SizeCache::new();
        let size = checked_encode_size(self.compute_size(&mut cache))? as usize;
        let mut buf = alloc::vec::Vec::with_capacity(size);
        self.write_to(&mut cache, &mut buf);
        debug_assert_two_pass(buf.len(), size);
        Ok(buf)
    }

    /// Encode this message to a new [`bytes::Bytes`].
    ///
    /// Useful when handing off to networking code (hyper, tonic, axum)
    /// that expects `Bytes` frame or body payloads. Works in `no_std`.
    ///
    /// This is equivalent to `Bytes::from(self.encode_to_vec())` — both
    /// are zero-copy with respect to the encoded bytes — but saves readers
    /// from having to know that `From<Vec<u8>> for Bytes` is zero-copy.
    ///
    /// # Panics
    ///
    /// Panics if the encoded size exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]) — see
    /// [`try_encode_to_bytes`](Self::try_encode_to_bytes) for the
    /// error-returning variant. In debug builds, also panics if a manual
    /// implementation's `write_to` produces a different byte count than
    /// its `compute_size` declared.
    // Direct body — see encode_to_vec for why the fat-payload entry points
    // do not delegate to their try_ twins.
    #[inline]
    #[must_use]
    fn encode_to_bytes(&self) -> bytes::Bytes {
        let mut cache = crate::SizeCache::new();
        let size = match checked_encode_size(self.compute_size(&mut cache)) {
            Ok(size) => size as usize,
            Err(_) => encode_size_overflow(),
        };
        let mut buf = bytes::BytesMut::with_capacity(size);
        self.write_to(&mut cache, &mut buf);
        debug_assert_two_pass(buf.len(), size);
        buf.freeze()
    }

    /// Encode to a new [`bytes::Bytes`], returning an error instead of
    /// panicking if the encoded size exceeds the 2 GiB protobuf limit
    /// ([`MAX_MESSAGE_BYTES`]).
    ///
    /// # Errors
    ///
    /// Returns [`EncodeError::MessageTooLarge`] if the encoded size exceeds
    /// [`MAX_MESSAGE_BYTES`].
    ///
    /// # Panics
    ///
    /// In debug builds, panics if a manual implementation's `write_to`
    /// produces a different byte count than its `compute_size` declared.
    fn try_encode_to_bytes(&self) -> Result<bytes::Bytes, EncodeError> {
        let mut cache = crate::SizeCache::new();
        let size = checked_encode_size(self.compute_size(&mut cache))? as usize;
        let mut buf = bytes::BytesMut::with_capacity(size);
        self.write_to(&mut cache, &mut buf);
        debug_assert_two_pass(buf.len(), size);
        Ok(buf.freeze())
    }

    /// Decode a message from a buffer.
    fn decode(buf: &mut impl Buf) -> Result<Self, DecodeError>
    where
        Self: Sized,
    {
        let limit = core::cell::Cell::new(DEFAULT_UNKNOWN_FIELD_LIMIT);
        let elem_budget = core::cell::Cell::new(DEFAULT_ELEMENT_MEMORY_LIMIT);
        let mut msg = Self::default();
        msg.merge(
            buf,
            DecodeContext::new(RECURSION_LIMIT, &limit).with_element_memory(&elem_budget),
        )?;
        Ok(msg)
    }

    /// Decode a message from a byte slice.
    ///
    /// Convenience wrapper around [`decode`](Self::decode) that avoids the
    /// `&mut bytes.as_slice()` incantation.
    fn decode_from_slice(mut data: &[u8]) -> Result<Self, DecodeError>
    where
        Self: Sized,
    {
        // `mut data` creates a local mutable copy of the fat pointer so that
        // `Buf::advance` can move the read cursor without affecting the caller.
        Self::decode(&mut data)
    }

    /// Decode a length-delimited message from a buffer.
    ///
    /// This is a **top-level** entry point.  It reads a varint length prefix,
    /// then decodes using arithmetic bounds checking, calling
    /// [`merge_to_limit`](Self::merge_to_limit) with a fresh
    /// [`RECURSION_LIMIT`] budget.  Any sub-messages inside are decoded via
    /// [`merge_length_delimited`](Self::merge_length_delimited), which tracks
    /// and decrements the budget.
    ///
    /// Do **not** call this method from within a
    /// [`merge_to_limit`](Self::merge_to_limit) implementation to decode a
    /// nested sub-message field; use
    /// [`merge_length_delimited`](Self::merge_length_delimited) instead so
    /// that the caller's depth budget is propagated correctly.
    fn decode_length_delimited(buf: &mut impl Buf) -> Result<Self, DecodeError>
    where
        Self: Sized,
    {
        // Refuse messages larger than 2 GiB to prevent allocating attacker-
        // controlled amounts of memory from a crafted length prefix.
        let len_u64 = crate::encoding::decode_varint(buf)?;
        if len_u64 > MAX_MESSAGE_BYTES as u64 {
            return Err(DecodeError::MessageTooLarge);
        }
        // Safe on 32-bit: len_u64 <= 2 GiB - 1 < u32::MAX, so the cast never truncates.
        let len = usize::try_from(len_u64).map_err(|_| DecodeError::MessageTooLarge)?;
        if buf.remaining() < len {
            return Err(DecodeError::UnexpectedEof);
        }
        // Arithmetic limit: decode `len` bytes from the buffer without
        // wrapping it in `Take`.  This keeps the buffer type `B` unchanged
        // through every recursion level, avoiding E0275 for recursive
        // message types like `google.protobuf.Struct ↔ Value`.
        let limit = buf.remaining() - len;
        let field_limit = core::cell::Cell::new(DEFAULT_UNKNOWN_FIELD_LIMIT);
        let elem_budget = core::cell::Cell::new(DEFAULT_ELEMENT_MEMORY_LIMIT);
        let mut msg = Self::default();
        msg.merge_to_limit(
            buf,
            DecodeContext::new(RECURSION_LIMIT, &field_limit).with_element_memory(&elem_budget),
            limit,
        )?;
        if buf.remaining() != limit {
            let remaining = buf.remaining();
            if remaining > limit {
                buf.advance(remaining - limit);
            } else {
                return Err(DecodeError::UnexpectedEof);
            }
        }
        Ok(msg)
    }

    /// Processes a single already-decoded tag and its associated field data
    /// from `buf`.
    ///
    /// This is the per-field dispatch method generated for each message type.
    /// Both [`merge_to_limit`](Self::merge_to_limit) and
    /// [`merge_group`](Self::merge_group) call this in their respective loops.
    ///
    /// `ctx` carries the remaining nesting depth and the shared allocation
    /// budget.
    ///
    /// # Errors
    ///
    /// Returns a [`DecodeError`] if:
    /// - the buffer is truncated or malformed,
    /// - a wire-type mismatch is detected for a known field,
    /// - the recursion limit is exceeded, or
    /// - the allocation budget is exhausted.
    fn merge_field(
        &mut self,
        tag: crate::encoding::Tag,
        buf: &mut impl Buf,
        ctx: DecodeContext<'_>,
    ) -> Result<(), DecodeError>;

    /// Merge fields from a buffer until `buf.remaining()` reaches `limit`.
    ///
    /// This is the core decode loop.  [`merge`](Self::merge) delegates to this
    /// with `limit = 0` (read until exhausted).
    /// [`merge_length_delimited`](Self::merge_length_delimited) computes
    /// `limit` from the declared sub-message length and calls this directly.
    ///
    /// The caller must ensure `limit <= buf.remaining()`.  The default
    /// implementations of [`merge`](Self::merge) and
    /// [`merge_length_delimited`](Self::merge_length_delimited) uphold this
    /// invariant.
    ///
    /// `ctx` carries the remaining nesting depth and the shared allocation
    /// budget.  Each call to
    /// [`merge_length_delimited`](Self::merge_length_delimited) consumes one
    /// depth level before recursing; when the depth reaches zero the call
    /// returns [`DecodeError::RecursionLimitExceeded`].
    fn merge_to_limit(
        &mut self,
        buf: &mut impl Buf,
        ctx: DecodeContext<'_>,
        limit: usize,
    ) -> Result<(), DecodeError> {
        while buf.remaining() > limit {
            let tag = crate::encoding::Tag::decode(buf)?;
            self.merge_field(tag, buf, ctx)?;
        }
        Ok(())
    }

    /// Merges a group-encoded message from `buf`, reading fields until an
    /// EndGroup tag with the given `field_number` is encountered.
    ///
    /// Proto2 groups use StartGroup/EndGroup wire types instead of
    /// length-delimited encoding. The opening StartGroup tag has already been
    /// consumed by the caller; this method reads the group body and the
    /// closing EndGroup tag.
    ///
    /// # Errors
    ///
    /// Returns a [`DecodeError`] if:
    /// - the buffer is truncated before the EndGroup tag,
    /// - an EndGroup tag is encountered with a mismatched field number,
    /// - a wire-type mismatch is detected for a known field, or
    /// - the recursion limit is exceeded.
    fn merge_group(
        &mut self,
        buf: &mut impl Buf,
        ctx: DecodeContext<'_>,
        field_number: u32,
    ) -> Result<(), DecodeError> {
        let ctx = ctx.descend()?;
        loop {
            if !buf.has_remaining() {
                return Err(DecodeError::UnexpectedEof);
            }
            let tag = crate::encoding::Tag::decode(buf)?;
            if tag.wire_type() == crate::encoding::WireType::EndGroup {
                return if tag.field_number() == field_number {
                    Ok(())
                } else {
                    Err(DecodeError::InvalidEndGroup(tag.field_number()))
                };
            }
            self.merge_field(tag, buf, ctx)?;
        }
    }

    /// Merge fields from a buffer into this message.
    ///
    /// Fields that are already set will be overwritten for singular fields,
    /// or appended for repeated fields, following standard protobuf merge
    /// semantics.
    ///
    /// `ctx` carries the remaining nesting depth and the shared allocation
    /// budget.  Each call to
    /// [`merge_length_delimited`](Self::merge_length_delimited) consumes one
    /// depth level before recursing; when the depth reaches zero the call
    /// returns [`DecodeError::RecursionLimitExceeded`].  Construct a fresh
    /// [`DecodeContext`] at the outermost call site, or use the convenience
    /// methods ([`decode`](Self::decode),
    /// [`merge_from_slice`](Self::merge_from_slice)) which do this
    /// automatically.
    fn merge(&mut self, buf: &mut impl Buf, ctx: DecodeContext<'_>) -> Result<(), DecodeError> {
        self.merge_to_limit(buf, ctx, 0)
    }

    /// Merge fields from a byte slice into this message.
    ///
    /// Convenience wrapper around [`merge`](Self::merge) that avoids the
    /// `&mut bytes.as_slice()` incantation.
    fn merge_from_slice(&mut self, mut data: &[u8]) -> Result<(), DecodeError> {
        let limit = core::cell::Cell::new(DEFAULT_UNKNOWN_FIELD_LIMIT);
        let elem_budget = core::cell::Cell::new(DEFAULT_ELEMENT_MEMORY_LIMIT);
        self.merge(
            &mut data,
            DecodeContext::new(RECURSION_LIMIT, &limit).with_element_memory(&elem_budget),
        )
    }

    /// Merge fields from a length-delimited sub-message payload into this message.
    ///
    /// Reads a varint length prefix, then calls [`merge_to_limit`](Self::merge_to_limit)
    /// with an arithmetic bound derived from the declared sub-message length.
    /// The buffer type `B` passes through unchanged at every recursion level,
    /// avoiding the `E0275` trait-solver recursion limit that occurs with
    /// `Take<&mut Take<&mut T>>` type growth.
    ///
    /// Used by generated code when decoding singular `MessageField<T>` fields
    /// — the sub-message is merged into the existing value rather than
    /// replaced, per protobuf merge semantics.
    ///
    /// `ctx` carries the remaining nesting depth and the shared allocation
    /// budget passed down from the enclosing
    /// [`merge_to_limit`](Self::merge_to_limit) call.  This method consumes
    /// one depth level before calling the inner `merge_to_limit`; when the
    /// depth reaches zero it returns
    /// [`DecodeError::RecursionLimitExceeded`].
    ///
    /// Enforces the same 2 GiB safety limit as [`decode_length_delimited`](Self::decode_length_delimited).
    ///
    /// # Errors
    ///
    /// Returns an error if the buffer is too short, if the declared length
    /// exceeds 2 GiB, if the recursion limit is reached, or if the inner
    /// `merge_to_limit` call fails.
    fn merge_length_delimited(
        &mut self,
        buf: &mut impl Buf,
        ctx: DecodeContext<'_>,
    ) -> Result<(), DecodeError> {
        let ctx = ctx.descend()?;
        let len_u64 = crate::encoding::decode_varint(buf)?;
        if len_u64 > MAX_MESSAGE_BYTES as u64 {
            return Err(DecodeError::MessageTooLarge);
        }
        let len = usize::try_from(len_u64).map_err(|_| DecodeError::MessageTooLarge)?;
        if buf.remaining() < len {
            return Err(DecodeError::UnexpectedEof);
        }
        // Arithmetic limit: the sub-message occupies `len` bytes, so the
        // decode loop should stop when `buf.remaining()` drops to
        // `remaining - len`.  This avoids wrapping the buffer in `Take`,
        // which would grow the type at each recursion level and trigger
        // E0275 for recursive message types like `Struct ↔ Value`.
        let limit = buf.remaining() - len;
        self.merge_to_limit(buf, ctx, limit)?;
        if buf.remaining() != limit {
            let remaining = buf.remaining();
            if remaining > limit {
                // Sub-message consumed fewer bytes than declared; skip the rest.
                buf.advance(remaining - limit);
            } else {
                return Err(DecodeError::UnexpectedEof);
            }
        }
        Ok(())
    }

    /// Clear all fields to their default values.
    fn clear(&mut self);
}

/// Compile-time access to a generated message's protobuf identifiers.
///
/// Generic code that needs to *name* a message type — type-erased event
/// registries, structured logging, `Any` packing, schema lookups — can
/// bound on `T: MessageName` and read [`PACKAGE`], [`NAME`],
/// [`FULL_NAME`], or [`TYPE_URL`] without descriptor machinery or runtime
/// reflection. All four are `&'static str` literals computed at codegen
/// time, so there's no allocation or concatenation at runtime — unlike
/// `prost::Name`, whose `full_name()` and `type_url()` are runtime
/// `format!` calls.
///
/// Bring `buffa::MessageName` into scope to use `MyMessage::FULL_NAME`.
/// Without the trait in scope, use
/// `<MyMessage as buffa::MessageName>::FULL_NAME`.
///
/// Codegen implements `MessageName` for both the owned message type and
/// its zero-copy view type (`MyMessageView<'a>`), so the same generic
/// bound dispatches either. The trait has **no** [`Message`] supertrait —
/// it doesn't reach into the wire codec and a name-keyed registry should
/// be able to register a type without proving it can encode.
///
/// Hand-written [`Message`] implementations can opt in by also
/// implementing `MessageName`; it is a separate trait specifically so
/// that omitting it stays non-breaking. For messages that also implement
/// [`ExtensionSet`](crate::ExtensionSet), [`FULL_NAME`] is guaranteed
/// equal to [`ExtensionSet::PROTO_FQN`](crate::ExtensionSet::PROTO_FQN);
/// the inherent `MyMessage::TYPE_URL` const is equal to [`TYPE_URL`].
/// All derive from the same `proto_fqn` source in codegen.
///
/// Because the only items are associated `const`s, this trait is **not**
/// object-safe (`dyn MessageName` does not compile). Use it as a generic
/// bound (`fn foo<T: MessageName>()`), not a trait object.
///
/// ```
/// # use buffa::MessageName;
/// /// A name-keyed registry can register any `MessageName` type — owned
/// /// or view — without proving it can encode.
/// fn registry_key<T: MessageName>() -> &'static str {
///     T::FULL_NAME
/// }
/// # // No generated types in `buffa` itself; just check it monomorphises.
/// # struct Demo;
/// # impl MessageName for Demo {
/// #     const PACKAGE: &'static str = "demo";
/// #     const NAME: &'static str = "Demo";
/// #     const FULL_NAME: &'static str = "demo.Demo";
/// #     const TYPE_URL: &'static str = "type.googleapis.com/demo.Demo";
/// # }
/// assert_eq!(registry_key::<Demo>(), "demo.Demo");
/// ```
///
/// [`PACKAGE`]: Self::PACKAGE
/// [`NAME`]: Self::NAME
/// [`FULL_NAME`]: Self::FULL_NAME
/// [`TYPE_URL`]: Self::TYPE_URL
pub trait MessageName {
    /// The protobuf package the message is declared in.
    ///
    /// `"my.pkg"` for `package my.pkg;`. Empty string for the unnamed
    /// root package. Does not include a leading or trailing dot.
    const PACKAGE: &'static str;

    /// The unqualified message name, with `.` between nesting levels.
    ///
    /// `"Foo"` for a top-level message; `"Outer.Inner"` for a message
    /// nested inside `Outer`. This is the "type name relative to the
    /// package" — what `prost::Name::NAME` calls the same thing — *not*
    /// `DescriptorProto.name`, which is only the leaf segment (`"Inner"`)
    /// for nested types.
    const NAME: &'static str;

    /// The fully-qualified protobuf type name with no leading dot.
    ///
    /// `"my.pkg.Outer.Inner"` for a nested message in package `my.pkg`,
    /// or just `"Foo"` for a top-level message in the unnamed root
    /// package. Equal to `PACKAGE` + `"."` + `NAME` (with the joining
    /// dot omitted when `PACKAGE` is empty); shipped as its own const
    /// because consumers almost always want the joined form and the
    /// dotted string can't be re-split unambiguously
    /// (`foo.Bar.Baz` could be package `foo.Bar` + message `Baz`, or
    /// package `foo` + nested `Bar.Baz`).
    const FULL_NAME: &'static str;

    /// The `google.protobuf.Any.type_url` form for this message.
    ///
    /// `"type.googleapis.com/" + FULL_NAME`. This is the value the
    /// runtime stores in [`Any::type_url`] when packing a message, and
    /// the one a generic `Any` registry should key on.
    ///
    /// [`Any::type_url`]: https://protobuf.dev/programming-guides/proto3/#any
    const TYPE_URL: &'static str;
}

/// Options for configuring message decoding behavior.
///
/// Use this to set custom recursion depth limits or maximum message sizes
/// when decoding from untrusted input.
///
/// # Examples
///
/// ```no_run
/// # use buffa::__doctest_fixtures::Person;
/// use buffa::DecodeOptions;
///
/// # fn example(bytes: &[u8]) -> Result<(), buffa::DecodeError> {
/// // Restrict recursion depth to 50 and message size to 1 MiB:
/// let msg: Person = DecodeOptions::new()
///     .with_recursion_limit(50)
///     .with_max_message_size(1024 * 1024)
///     .decode_from_slice(bytes)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct DecodeOptions {
    recursion_limit: u32,
    max_message_size: usize,
    unbounded_reader_size: bool,
    unknown_field_limit: usize,
    element_memory_limit: usize,
}

/// Default maximum message size: 2 GiB - 1 (matches the sub-message limit
/// in `merge_length_delimited` and the encode-side limit — see
/// [`MAX_MESSAGE_BYTES`]).
const DEFAULT_MAX_MESSAGE_SIZE: usize = MAX_MESSAGE_BYTES as usize;

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

impl DecodeOptions {
    /// Create new decode options with defaults.
    ///
    /// Defaults:
    /// - `recursion_limit`: 100 (same as [`RECURSION_LIMIT`])
    /// - `max_message_size`: 2 GiB - 1
    /// - `unknown_field_limit`: 1,000,000 (same as [`DEFAULT_UNKNOWN_FIELD_LIMIT`])
    /// - `element_memory_limit`: 32 MiB (same as [`DEFAULT_ELEMENT_MEMORY_LIMIT`])
    pub fn new() -> Self {
        Self {
            recursion_limit: RECURSION_LIMIT,
            max_message_size: DEFAULT_MAX_MESSAGE_SIZE,
            unbounded_reader_size: false,
            unknown_field_limit: DEFAULT_UNKNOWN_FIELD_LIMIT,
            element_memory_limit: DEFAULT_ELEMENT_MEMORY_LIMIT,
        }
    }

    /// Set the maximum recursion depth for nested messages.
    ///
    /// Each nested sub-message consumes one level of depth budget. When
    /// the budget reaches zero, decoding returns
    /// [`DecodeError::RecursionLimitExceeded`].
    ///
    /// Default: 100.
    #[must_use]
    pub fn with_recursion_limit(mut self, limit: u32) -> Self {
        self.recursion_limit = limit;
        self
    }

    /// Set the maximum total message size in bytes.
    ///
    /// If the input buffer or length-delimited payload exceeds this size,
    /// decoding returns [`DecodeError::MessageTooLarge`].
    ///
    /// Values above the protobuf message-size limit (2 GiB - 1) are clamped to
    /// that limit. Debug builds assert on out-of-range values so accidental
    /// `usize::MAX` sentinels are caught during development. On `std` builds,
    /// use `without_reader_size_limit` when EOF-bounded `decode_reader` input
    /// intentionally has no byte cap.
    ///
    /// Calling this re-enables the reader byte cap, overriding any prior
    /// `without_reader_size_limit`.
    ///
    /// This is checked at the top-level decode entry point. Individual
    /// sub-messages are still bounded by the internal 2 GiB limit
    /// regardless of this setting.
    ///
    /// Default: 2 GiB - 1 (0x7FFF_FFFF).
    #[must_use]
    pub fn with_max_message_size(mut self, max_bytes: usize) -> Self {
        debug_assert!(
            max_bytes <= DEFAULT_MAX_MESSAGE_SIZE,
            "DecodeOptions::with_max_message_size clamps values above the protobuf 2 GiB limit; \
             on std builds, use DecodeOptions::without_reader_size_limit for intentionally \
             unbounded reader input — there is no unbounded slice/Buf path"
        );
        self.max_message_size = max_bytes.min(DEFAULT_MAX_MESSAGE_SIZE);
        self.unbounded_reader_size = false;
        self
    }

    /// Remove the byte cap for EOF-bounded [`decode_reader`](Self::decode_reader)
    /// input.
    ///
    /// This is only used by the `std::io::Read` entry point that reads until
    /// EOF. Slice-, [`Buf`]-, and view-based entry points remain bounded by
    /// the configured [`with_max_message_size`](Self::with_max_message_size)
    /// (itself capped at the protobuf 2 GiB - 1 maximum), and length-delimited
    /// paths keep the same hard cap because their declared length is
    /// attacker-controlled.
    ///
    /// An unbounded reader can exhaust memory if the source does not end or is
    /// larger than available allocation capacity. Prefer
    /// [`with_max_message_size`](Self::with_max_message_size) for untrusted
    /// input.
    #[cfg(feature = "std")]
    #[must_use]
    pub fn without_reader_size_limit(mut self) -> Self {
        self.unbounded_reader_size = true;
        self
    }

    /// Set the maximum number of unknown fields decoded per decode call.
    ///
    /// Each decoded unknown field occupies a ~40-byte
    /// [`UnknownField`](crate::UnknownField) slot regardless of its wire
    /// size (a minimal field is 2 wire bytes — a ~20× amplification), so an
    /// input-size cap alone does not bound decoder memory; this limit does,
    /// at roughly `limit × 40` bytes of slot overhead. Unknown
    /// length-delimited *payload* bytes are not counted — they are bounded
    /// by the input size, which
    /// [`with_max_message_size`](Self::with_max_message_size) governs. When
    /// the limit is exceeded, decoding returns
    /// [`DecodeError::UnknownFieldLimitExceeded`].
    ///
    /// Zero-copy view decoding ([`decode_view`](Self::decode_view)) charges
    /// one slot per unknown field — including fields nested inside unknown
    /// groups — even though views store unknown fields as coalesced spans
    /// (~16 bytes per contiguous run): coalescing bounds view memory, while
    /// this limit bounds what converting the view to an owned message would
    /// materialize. Conversion replays under exactly the budget decoding
    /// charged, so a view that decodes within this limit always converts.
    ///
    /// Default: 1,000,000 ([`DEFAULT_UNKNOWN_FIELD_LIMIT`]).
    #[must_use]
    pub fn with_unknown_field_limit(mut self, count: usize) -> Self {
        self.unknown_field_limit = count;
        self
    }

    /// Set the memory this decode may materialize in the elements of
    /// length-delimited containers — repeated message, string and bytes fields,
    /// and map entries — shared across the whole decode tree rather than per
    /// field or per message.
    ///
    /// This is not [`with_max_message_size`](Self::with_max_message_size) by
    /// another name: that bounds the bytes going *in*, this bounds what they
    /// expand *into*. They are not redundant, because the two are not
    /// proportional — an empty repeated message element is 2 wire bytes and
    /// `size_of::<T>()` of `Vec` footprint (measured at 256 bytes for a message
    /// of a few `Vec`/`String` fields), so a payload well inside any input
    /// bound can still materialize 128x its own size. Charging is by element
    /// footprint, so a budget means the same amount of memory whatever the
    /// element size — which a count limit could not offer.
    ///
    /// Packed scalar fields are never charged; see
    /// [`DEFAULT_ELEMENT_MEMORY_LIMIT`] for why, and for the `Vec`-doubling
    /// caveat on peak memory.
    ///
    /// Default: 32 MiB ([`DEFAULT_ELEMENT_MEMORY_LIMIT`]).
    #[must_use]
    pub fn with_element_memory_limit(mut self, bytes: usize) -> Self {
        self.element_memory_limit = bytes;
        self
    }

    /// Returns the configured element-memory budget.
    #[must_use]
    pub fn element_memory_limit(&self) -> usize {
        self.element_memory_limit
    }

    /// Returns the configured recursion depth limit.
    pub fn recursion_limit(&self) -> u32 {
        self.recursion_limit
    }

    /// Returns the configured unknown-field limit.
    pub fn unknown_field_limit(&self) -> usize {
        self.unknown_field_limit
    }

    /// Returns the configured maximum message size in bytes for bounded decode
    /// entry points.
    ///
    /// This returns the configured (clamped) value even when
    /// `without_reader_size_limit` is enabled for EOF-bounded reader input —
    /// the slice/`Buf`/view paths still honor it. Use
    /// `is_reader_size_unbounded` (std only) to inspect the reader flag.
    pub fn max_message_size(&self) -> usize {
        self.max_message_size
    }

    /// Returns whether EOF-bounded reader input has no byte cap.
    #[cfg(feature = "std")]
    pub fn is_reader_size_unbounded(&self) -> bool {
        self.unbounded_reader_size
    }

    /// Decode a message from a buffer.
    pub fn decode<M: Message>(&self, buf: &mut impl Buf) -> Result<M, DecodeError> {
        if buf.remaining() > self.max_message_size {
            return Err(DecodeError::MessageTooLarge);
        }
        let limit = core::cell::Cell::new(self.unknown_field_limit);
        let elem_budget = core::cell::Cell::new(self.element_memory_limit);
        let mut msg = M::default();
        msg.merge(
            buf,
            DecodeContext::new(self.recursion_limit, &limit).with_element_memory(&elem_budget),
        )?;
        Ok(msg)
    }

    /// Decode a message from a byte slice.
    pub fn decode_from_slice<M: Message>(&self, data: &[u8]) -> Result<M, DecodeError> {
        if data.len() > self.max_message_size {
            return Err(DecodeError::MessageTooLarge);
        }
        self.decode_from_slice_unchecked_size(data)
    }

    fn decode_from_slice_unchecked_size<M: Message>(&self, data: &[u8]) -> Result<M, DecodeError> {
        let limit = core::cell::Cell::new(self.unknown_field_limit);
        let elem_budget = core::cell::Cell::new(self.element_memory_limit);
        let mut msg = M::default();
        msg.merge(
            &mut &*data,
            DecodeContext::new(self.recursion_limit, &limit).with_element_memory(&elem_budget),
        )?;
        Ok(msg)
    }

    /// Decode a length-delimited message from a buffer.
    pub fn decode_length_delimited<M: Message>(
        &self,
        buf: &mut impl Buf,
    ) -> Result<M, DecodeError> {
        // Enforce the 2 GiB internal safety cap even if the user sets a
        // larger max_message_size, to prevent allocating attacker-controlled
        // amounts of memory from a crafted length prefix.
        let max = core::cmp::min(
            self.max_message_size as u64,
            DEFAULT_MAX_MESSAGE_SIZE as u64,
        );
        let len_u64 = crate::encoding::decode_varint(buf)?;
        if len_u64 > max {
            return Err(DecodeError::MessageTooLarge);
        }
        let len = usize::try_from(len_u64).map_err(|_| DecodeError::MessageTooLarge)?;
        if buf.remaining() < len {
            return Err(DecodeError::UnexpectedEof);
        }
        let limit = buf.remaining() - len;
        let field_limit = core::cell::Cell::new(self.unknown_field_limit);
        let elem_budget = core::cell::Cell::new(self.element_memory_limit);
        let mut msg = M::default();
        msg.merge_to_limit(
            buf,
            DecodeContext::new(self.recursion_limit, &field_limit)
                .with_element_memory(&elem_budget),
            limit,
        )?;
        if buf.remaining() != limit {
            let remaining = buf.remaining();
            if remaining > limit {
                buf.advance(remaining - limit);
            } else {
                return Err(DecodeError::UnexpectedEof);
            }
        }
        Ok(msg)
    }

    /// Merge fields from a buffer into an existing message.
    pub fn merge<M: Message>(&self, msg: &mut M, buf: &mut impl Buf) -> Result<(), DecodeError> {
        if buf.remaining() > self.max_message_size {
            return Err(DecodeError::MessageTooLarge);
        }
        let limit = core::cell::Cell::new(self.unknown_field_limit);
        let elem_budget = core::cell::Cell::new(self.element_memory_limit);
        msg.merge(
            buf,
            DecodeContext::new(self.recursion_limit, &limit).with_element_memory(&elem_budget),
        )
    }

    /// Merge fields from a byte slice into an existing message.
    pub fn merge_from_slice<M: Message>(
        &self,
        msg: &mut M,
        data: &[u8],
    ) -> Result<(), DecodeError> {
        if data.len() > self.max_message_size {
            return Err(DecodeError::MessageTooLarge);
        }
        let limit = core::cell::Cell::new(self.unknown_field_limit);
        let elem_budget = core::cell::Cell::new(self.element_memory_limit);
        msg.merge(
            &mut &*data,
            DecodeContext::new(self.recursion_limit, &limit).with_element_memory(&elem_budget),
        )
    }

    /// Decode a zero-copy view from a byte slice.
    ///
    /// Enforces `max_message_size` on the input, and passes the recursion
    /// limit and unknown-field limit to the view decoder (views charge the
    /// unknown-field limit per field, including fields nested in unknown
    /// groups — see
    /// [`with_unknown_field_limit`](Self::with_unknown_field_limit)).
    ///
    /// # Errors
    ///
    /// Returns [`DecodeError::MessageTooLarge`] for oversized input, or any
    /// error from the view decoder (malformed wire data, recursion limit,
    /// unknown-field limit).
    pub fn decode_view<'a, V: crate::view::MessageView<'a>>(
        &self,
        buf: &'a [u8],
    ) -> Result<V, DecodeError> {
        if buf.len() > self.max_message_size {
            return Err(DecodeError::MessageTooLarge);
        }
        let limit = core::cell::Cell::new(self.unknown_field_limit);
        let elem_budget = core::cell::Cell::new(self.element_memory_limit);
        V::decode_view_with_ctx(
            buf,
            DecodeContext::new(self.recursion_limit, &limit).with_element_memory(&elem_budget),
        )
    }

    /// Decode a lazy view from a byte slice (see
    /// [`LazyMessageView`](crate::view::LazyMessageView)).
    ///
    /// The budgets remaining at each deferred field's position are recorded
    /// and charged when that field is accessed, so the configured limits
    /// flow through deferred decoding. Unlike
    /// [`decode_view`](Self::decode_view), the unknown-field limit is not
    /// enforced globally across the message tree at decode time: each
    /// deferred subtree independently replays the allowance recorded at its
    /// position, so a full traversal can materialize unknown-field records
    /// proportional to input size. Prefer `decode_view` for untrusted input
    /// if the global bound matters.
    ///
    /// # Errors
    ///
    /// Returns [`DecodeError::MessageTooLarge`] for oversized input, or any
    /// error from decoding the message's own fields — including
    /// [`DecodeError::RecursionLimitExceeded`] /
    /// [`DecodeError::UnknownFieldLimitExceeded`] when the configured limits
    /// are exhausted by them. Deferred sub-message bytes surface errors on
    /// access instead.
    pub fn decode_lazy_view<'a, L: crate::view::LazyMessageView<'a>>(
        &self,
        buf: &'a [u8],
    ) -> Result<L, DecodeError> {
        if buf.len() > self.max_message_size {
            return Err(DecodeError::MessageTooLarge);
        }
        let limit = core::cell::Cell::new(self.unknown_field_limit);
        let elem_budget = core::cell::Cell::new(self.element_memory_limit);
        L::decode_lazy_with_ctx(
            buf,
            DecodeContext::new(self.recursion_limit, &limit).with_element_memory(&elem_budget),
        )
    }

    /// Decode a message by reading all bytes from a [`std::io::Read`] source.
    ///
    /// Reads until EOF, enforces the configured reader size limit unless
    /// [`without_reader_size_limit`](Self::without_reader_size_limit) was
    /// selected, then decodes the buffered bytes. Returns `std::io::Error` to
    /// be compatible with `Read`-based error handling.
    #[cfg(feature = "std")]
    pub fn decode_reader<M: Message>(
        &self,
        reader: &mut impl std::io::Read,
    ) -> Result<M, std::io::Error> {
        let bytes = self.read_limited(reader)?;
        self.decode_from_slice_unchecked_size::<M>(&bytes)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
    }

    /// Decode a length-delimited message from a [`std::io::Read`] source.
    ///
    /// Reads a varint length prefix, enforces `max_message_size`, reads
    /// exactly that many bytes, then decodes. Useful for reading sequential
    /// length-delimited messages from a file or stream.
    ///
    /// The declared length is treated as untrusted: the read buffer grows
    /// as bytes actually arrive rather than being allocated up front, so a
    /// source that declares a large length but never delivers the bytes
    /// cannot force a large allocation.
    #[cfg(feature = "std")]
    pub fn decode_length_delimited_reader<M: Message>(
        &self,
        reader: &mut impl std::io::Read,
    ) -> Result<M, std::io::Error> {
        use std::io::Read as _;
        let len = read_varint(reader)?;
        let max = core::cmp::min(
            self.max_message_size as u64,
            DEFAULT_MAX_MESSAGE_SIZE as u64,
        );
        if len > max {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                DecodeError::MessageTooLarge,
            ));
        }
        let len = usize::try_from(len).map_err(|_| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                DecodeError::MessageTooLarge,
            )
        })?;
        // Pre-size only up to a small bound; `read_to_end` grows the buffer
        // geometrically as data is actually delivered, so peak allocation
        // tracks delivered bytes, not the wire-declared length.
        const INITIAL_CAPACITY_CAP: usize = 64 * 1024;
        let mut buf = alloc::vec::Vec::with_capacity(len.min(INITIAL_CAPACITY_CAP));
        reader.take(len as u64).read_to_end(&mut buf)?;
        if buf.len() < len {
            return Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                DecodeError::UnexpectedEof,
            ));
        }
        self.decode_from_slice::<M>(&buf)
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
    }

    /// Read all bytes from a reader up to the configured reader limit.
    #[cfg(feature = "std")]
    fn read_limited(
        &self,
        reader: &mut impl std::io::Read,
    ) -> Result<alloc::vec::Vec<u8>, std::io::Error> {
        use std::io::Read as _;
        let mut buf = alloc::vec::Vec::new();
        if self.unbounded_reader_size {
            reader.read_to_end(&mut buf)?;
            return Ok(buf);
        }
        reader
            .take((self.max_message_size as u64).saturating_add(1))
            .read_to_end(&mut buf)?;
        if buf.len() > self.max_message_size {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                DecodeError::MessageTooLarge,
            ));
        }
        Ok(buf)
    }
}

/// Read a varint from a `std::io::Read` source, one byte at a time.
///
/// Mirrors the validation in [`decode_varint_slow`](crate::encoding): a
/// 10th byte > `0x01` (overflow bits set, or continuation bit implying an
/// 11th byte) is rejected.
#[cfg(feature = "std")]
fn read_varint(reader: &mut impl std::io::Read) -> Result<u64, std::io::Error> {
    let mut value: u64 = 0;
    let mut shift: u32 = 0;
    loop {
        let mut byte = [0u8; 1];
        reader.read_exact(&mut byte)?;
        let b = byte[0];
        if shift < 63 {
            value |= ((b & 0x7F) as u64) << shift;
            if b < 0x80 {
                return Ok(value);
            }
            shift += 7;
        } else {
            // 10th byte: only bit 0 maps to bit 63 of the result. A byte
            // > 0x01 means either data overflow (bits 1-6 set) or an 11th
            // byte (continuation bit 0x80 set).
            if b > 0x01 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    DecodeError::VarintTooLong,
                ));
            }
            value |= (b as u64) << 63;
            return Ok(value);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::encoding::encode_varint;
    use crate::error::DecodeError;
    use crate::message_field::DefaultInstance;
    use crate::SizeCache;

    // Minimal hand-written Message for testing merge_length_delimited.
    #[derive(Clone, Debug, Default, PartialEq)]
    struct FlatMsg {
        value: i32,
    }

    impl DefaultInstance for FlatMsg {
        fn default_instance() -> &'static Self {
            static INST: crate::__private::OnceBox<FlatMsg> = crate::__private::OnceBox::new();
            INST.get_or_init(|| alloc::boxed::Box::new(FlatMsg::default()))
        }
    }

    impl Message for FlatMsg {
        fn compute_size(&self, _cache: &mut SizeCache) -> u32 {
            if self.value != 0 {
                1 + crate::types::int32_encoded_len(self.value) as u32
            } else {
                0
            }
        }

        fn write_to(&self, _cache: &mut SizeCache, buf: &mut impl EncodeSink) {
            if self.value != 0 {
                crate::encoding::Tag::new(1, crate::encoding::WireType::Varint).encode(buf);
                crate::types::encode_int32(self.value, buf);
            }
        }

        fn merge_field(
            &mut self,
            tag: crate::encoding::Tag,
            buf: &mut impl Buf,
            _ctx: DecodeContext<'_>,
        ) -> Result<(), DecodeError> {
            match tag.field_number() {
                1 => {
                    self.value = crate::types::decode_int32(buf)?;
                }
                _ => {
                    crate::encoding::skip_field(tag, buf)?;
                }
            }
            Ok(())
        }

        fn clear(&mut self) {
            *self = Self::default();
        }
    }

    fn wire_bytes(msg: &FlatMsg) -> alloc::vec::Vec<u8> {
        let mut buf = alloc::vec::Vec::new();
        msg.encode_length_delimited(&mut buf);
        buf
    }

    #[test]
    fn test_merge_length_delimited_basic() {
        let src = FlatMsg { value: 42 };
        let mut dst = FlatMsg::default();
        dst.merge_length_delimited(
            &mut wire_bytes(&src).as_slice(),
            crate::test_ctx(RECURSION_LIMIT),
        )
        .unwrap();
        assert_eq!(dst.value, 42);
    }

    #[test]
    fn test_merge_length_delimited_merges_into_existing() {
        // Second merge overwrites (proto3 last-wins for scalar fields).
        let mut dst = FlatMsg::default();
        dst.merge_length_delimited(
            &mut wire_bytes(&FlatMsg { value: 1 }).as_slice(),
            crate::test_ctx(RECURSION_LIMIT),
        )
        .unwrap();
        assert_eq!(dst.value, 1);
        dst.merge_length_delimited(
            &mut wire_bytes(&FlatMsg { value: 2 }).as_slice(),
            crate::test_ctx(RECURSION_LIMIT),
        )
        .unwrap();
        assert_eq!(dst.value, 2);
    }

    #[test]
    fn test_merge_length_delimited_truncated() {
        // Length prefix says 10 bytes but buffer contains only 2.
        let mut buf = alloc::vec::Vec::new();
        encode_varint(10, &mut buf);
        buf.extend_from_slice(&[0x01, 0x01]);
        let mut dst = FlatMsg::default();
        assert_eq!(
            dst.merge_length_delimited(&mut buf.as_slice(), crate::test_ctx(RECURSION_LIMIT)),
            Err(DecodeError::UnexpectedEof)
        );
    }

    #[test]
    fn test_merge_length_delimited_oversized() {
        // Length prefix exceeds the 2 GiB safety limit.
        let mut buf = alloc::vec::Vec::new();
        encode_varint(0x8000_0000u64, &mut buf); // 2 GiB + 1
        let mut dst = FlatMsg::default();
        assert_eq!(
            dst.merge_length_delimited(&mut buf.as_slice(), crate::test_ctx(RECURSION_LIMIT)),
            Err(DecodeError::MessageTooLarge)
        );
    }

    #[test]
    fn test_merge_length_delimited_recursion_limit() {
        // depth=1 means merge_length_delimited will decrement to 0, then any
        // nested call would return RecursionLimitExceeded.  Passing depth=1
        // to merge_length_delimited itself is the boundary: it decrements to
        // 0 and calls merge with depth=0, which for FlatMsg (a leaf) succeeds.
        // Passing depth=0 directly must return RecursionLimitExceeded.
        let src = FlatMsg { value: 7 };
        let mut dst = FlatMsg::default();
        assert_eq!(
            dst.merge_length_delimited(&mut wire_bytes(&src).as_slice(), crate::test_ctx(0)),
            Err(DecodeError::RecursionLimitExceeded)
        );
        // depth=1 succeeds: exactly one level is consumed.
        dst.merge_length_delimited(&mut wire_bytes(&src).as_slice(), crate::test_ctx(1))
            .unwrap();
        assert_eq!(dst.value, 7);
    }

    #[test]
    fn test_decode_from_slice_basic() {
        let src = FlatMsg { value: 42 };
        let bytes = src.encode_to_vec();
        let dst = FlatMsg::decode_from_slice(&bytes).unwrap();
        assert_eq!(dst.value, 42);
    }

    #[test]
    fn test_encode_to_bytes_matches_encode_to_vec() {
        let src = FlatMsg { value: 42 };
        let vec = src.encode_to_vec();
        let bytes = src.encode_to_bytes();
        assert_eq!(vec.as_slice(), bytes.as_ref());
        // Round-trip through the Bytes variant.
        let dst = FlatMsg::decode_from_slice(&bytes).unwrap();
        assert_eq!(dst.value, 42);
        // Empty-message case: zero-length buffer is well-defined.
        assert!(FlatMsg::default().encode_to_bytes().is_empty());
    }

    #[test]
    fn test_decode_from_slice_empty() {
        let dst = FlatMsg::decode_from_slice(&[]).unwrap();
        assert_eq!(dst.value, 0);
    }

    #[test]
    fn test_decode_from_slice_invalid_returns_error() {
        // A lone 0xFF byte is not a valid varint tag.
        let result = FlatMsg::decode_from_slice(&[0xFF]);
        assert!(result.is_err());
    }

    #[test]
    fn test_merge_from_slice_basic() {
        let src = FlatMsg { value: 7 };
        let bytes = src.encode_to_vec();
        let mut dst = FlatMsg::default();
        dst.merge_from_slice(&bytes).unwrap();
        assert_eq!(dst.value, 7);
    }

    #[test]
    fn test_merge_from_slice_last_wins() {
        let src1 = FlatMsg { value: 1 };
        let src2 = FlatMsg { value: 2 };
        let mut dst = FlatMsg::default();
        dst.merge_from_slice(&src1.encode_to_vec()).unwrap();
        dst.merge_from_slice(&src2.encode_to_vec()).unwrap();
        // Proto3 last-wins semantics for scalar fields.
        assert_eq!(dst.value, 2);
    }

    // ── DecodeOptions tests ──────────────────────────────────────────

    #[test]
    fn test_decode_options_default_works() {
        let src = FlatMsg { value: 99 };
        let bytes = src.encode_to_vec();
        let msg: FlatMsg = DecodeOptions::new().decode_from_slice(&bytes).unwrap();
        assert_eq!(msg.value, 99);
    }

    #[test]
    fn test_decode_options_max_message_size_rejects() {
        let src = FlatMsg { value: 42 };
        let bytes = src.encode_to_vec();
        // Set max size to 1 byte — smaller than the encoded message.
        let result: Result<FlatMsg, _> = DecodeOptions::new()
            .with_max_message_size(1)
            .decode_from_slice(&bytes);
        assert_eq!(result, Err(DecodeError::MessageTooLarge));
    }

    #[test]
    fn test_decode_options_max_message_size_exact_boundary() {
        let src = FlatMsg { value: 42 };
        let bytes = src.encode_to_vec();
        // Exact size should succeed.
        let msg: FlatMsg = DecodeOptions::new()
            .with_max_message_size(bytes.len())
            .decode_from_slice(&bytes)
            .unwrap();
        assert_eq!(msg.value, 42);
        // One byte less should fail.
        let result: Result<FlatMsg, _> = DecodeOptions::new()
            .with_max_message_size(bytes.len() - 1)
            .decode_from_slice(&bytes);
        assert_eq!(result, Err(DecodeError::MessageTooLarge));
    }

    #[test]
    fn test_decode_options_custom_recursion_limit() {
        // FlatMsg has no nested messages, so any recursion limit >= 0 works.
        // Just verify the API compiles and runs.
        let src = FlatMsg { value: 7 };
        let bytes = src.encode_to_vec();
        let msg: FlatMsg = DecodeOptions::new()
            .with_recursion_limit(1)
            .decode_from_slice(&bytes)
            .unwrap();
        assert_eq!(msg.value, 7);
    }

    #[test]
    fn test_decode_options_merge() {
        let src = FlatMsg { value: 55 };
        let bytes = src.encode_to_vec();
        let mut msg = FlatMsg::default();
        DecodeOptions::new()
            .merge_from_slice(&mut msg, &bytes)
            .unwrap();
        assert_eq!(msg.value, 55);
    }

    #[test]
    fn test_decode_options_merge_rejects_oversize() {
        let src = FlatMsg { value: 55 };
        let bytes = src.encode_to_vec();
        let mut msg = FlatMsg::default();
        let result = DecodeOptions::new()
            .with_max_message_size(1)
            .merge_from_slice(&mut msg, &bytes);
        assert_eq!(result, Err(DecodeError::MessageTooLarge));
    }

    #[test]
    fn test_decode_options_length_delimited() {
        let src = FlatMsg { value: 42 };
        let mut ld_bytes = alloc::vec::Vec::new();
        src.encode_length_delimited(&mut ld_bytes);
        let msg: FlatMsg = DecodeOptions::new()
            .decode_length_delimited(&mut ld_bytes.as_slice())
            .unwrap();
        assert_eq!(msg.value, 42);
    }

    #[test]
    fn test_decode_options_length_delimited_rejects_oversize() {
        let src = FlatMsg { value: 42 };
        let mut ld_bytes = alloc::vec::Vec::new();
        src.encode_length_delimited(&mut ld_bytes);
        let result: Result<FlatMsg, _> = DecodeOptions::new()
            .with_max_message_size(1)
            .decode_length_delimited(&mut ld_bytes.as_slice());
        assert_eq!(result, Err(DecodeError::MessageTooLarge));
    }

    #[test]
    fn decode_options_getters_return_defaults() {
        let opts = DecodeOptions::new();
        assert_eq!(opts.recursion_limit(), RECURSION_LIMIT);
        assert_eq!(opts.max_message_size(), 0x7FFF_FFFF);
        assert_eq!(opts.unknown_field_limit(), DEFAULT_UNKNOWN_FIELD_LIMIT);
    }

    #[test]
    fn decode_options_getters_return_custom_values() {
        let opts = DecodeOptions::new()
            .with_recursion_limit(42)
            .with_max_message_size(1024)
            .with_unknown_field_limit(2048);
        assert_eq!(opts.recursion_limit(), 42);
        assert_eq!(opts.max_message_size(), 1024);
        assert_eq!(opts.unknown_field_limit(), 2048);
    }

    #[cfg(debug_assertions)]
    #[test]
    #[should_panic(expected = "protobuf 2 GiB limit")]
    fn decode_options_max_message_size_above_protobuf_limit_debug_asserts() {
        let _ = DecodeOptions::new().with_max_message_size(DEFAULT_MAX_MESSAGE_SIZE + 1);
    }

    #[cfg(not(debug_assertions))]
    #[test]
    fn decode_options_max_message_size_above_protobuf_limit_saturates() {
        let opts = DecodeOptions::new().with_max_message_size(DEFAULT_MAX_MESSAGE_SIZE + 1);
        assert_eq!(opts.max_message_size(), DEFAULT_MAX_MESSAGE_SIZE);
    }

    #[test]
    fn test_decode_options_default_impl() {
        // DecodeOptions::default() ≡ DecodeOptions::new().
        let opts = DecodeOptions::default();
        assert_eq!(opts.recursion_limit(), RECURSION_LIMIT);
        assert_eq!(opts.max_message_size(), 0x7FFF_FFFF);
        assert_eq!(opts.unknown_field_limit(), DEFAULT_UNKNOWN_FIELD_LIMIT);
    }

    #[test]
    fn test_decode_options_decode_buf() {
        // The Buf-taking decode() variant (vs decode_from_slice).
        let src = FlatMsg { value: 123 };
        let bytes = src.encode_to_vec();
        let msg: FlatMsg = DecodeOptions::new().decode(&mut bytes.as_slice()).unwrap();
        assert_eq!(msg.value, 123);
        // Oversize check on the Buf variant.
        let result: Result<FlatMsg, _> = DecodeOptions::new()
            .with_max_message_size(1)
            .decode(&mut bytes.as_slice());
        assert_eq!(result, Err(DecodeError::MessageTooLarge));
    }

    #[test]
    fn test_decode_options_merge_buf() {
        // The Buf-taking merge() variant (vs merge_from_slice).
        let src = FlatMsg { value: 77 };
        let bytes = src.encode_to_vec();
        let mut msg = FlatMsg::default();
        DecodeOptions::new()
            .merge(&mut msg, &mut bytes.as_slice())
            .unwrap();
        assert_eq!(msg.value, 77);
        // Oversize check.
        let mut msg = FlatMsg::default();
        let result = DecodeOptions::new()
            .with_max_message_size(1)
            .merge(&mut msg, &mut bytes.as_slice());
        assert_eq!(result, Err(DecodeError::MessageTooLarge));
    }

    // ── Message trait default methods ─────────────────────────────────

    #[test]
    fn test_message_encode_trait_default() {
        // Message::encode(buf) ≡ compute_size() then write_to(buf).
        let src = FlatMsg { value: 42 };
        let mut buf = alloc::vec::Vec::new();
        src.encode(&mut buf);
        assert_eq!(buf, src.encode_to_vec());
    }

    #[test]
    fn test_message_decode_length_delimited_trait_default() {
        // The trait-level decode_length_delimited (distinct from
        // DecodeOptions::decode_length_delimited).
        let src = FlatMsg { value: 42 };
        let mut ld = alloc::vec::Vec::new();
        src.encode_length_delimited(&mut ld);
        let got = FlatMsg::decode_length_delimited(&mut ld.as_slice()).unwrap();
        assert_eq!(got.value, 42);
    }

    #[test]
    fn test_message_decode_length_delimited_oversize() {
        // Length prefix > 2 GiB → MessageTooLarge.
        let mut buf = alloc::vec::Vec::new();
        encode_varint(0x8000_0000u64, &mut buf);
        let result = FlatMsg::decode_length_delimited(&mut buf.as_slice());
        assert_eq!(result, Err(DecodeError::MessageTooLarge));
    }

    #[test]
    fn test_message_decode_length_delimited_truncated() {
        // Length prefix says 10 bytes, buffer has 2.
        let mut buf = alloc::vec::Vec::new();
        encode_varint(10, &mut buf);
        buf.push(0x08);
        buf.push(0x01);
        let result = FlatMsg::decode_length_delimited(&mut buf.as_slice());
        assert_eq!(result, Err(DecodeError::UnexpectedEof));
    }

    #[test]
    fn test_message_decode_length_delimited_with_trailing() {
        // Buffer has two back-to-back length-delimited messages.
        // decode_length_delimited should consume exactly the first one
        // and leave the buffer positioned at the second.
        let a = FlatMsg { value: 1 };
        let b = FlatMsg { value: 2 };
        let mut buf = alloc::vec::Vec::new();
        a.encode_length_delimited(&mut buf);
        b.encode_length_delimited(&mut buf);

        let mut cur = buf.as_slice();
        let first = FlatMsg::decode_length_delimited(&mut cur).unwrap();
        assert_eq!(first.value, 1);
        let second = FlatMsg::decode_length_delimited(&mut cur).unwrap();
        assert_eq!(second.value, 2);
        assert!(cur.is_empty());
    }

    // ── merge_group tests ─────────────────────────────────────────────

    /// Build a group body for FlatMsg (field 1 = value) terminated by
    /// EndGroup with the given field number.
    fn group_bytes(value: i32, group_field_number: u32) -> alloc::vec::Vec<u8> {
        use crate::encoding::{Tag, WireType};
        let mut buf = alloc::vec::Vec::new();
        if value != 0 {
            Tag::new(1, WireType::Varint).encode(&mut buf);
            crate::types::encode_int32(value, &mut buf);
        }
        Tag::new(group_field_number, WireType::EndGroup).encode(&mut buf);
        buf
    }

    #[test]
    fn test_merge_group_basic() {
        let data = group_bytes(42, 5);
        let mut dst = FlatMsg::default();
        dst.merge_group(&mut data.as_slice(), crate::test_ctx(RECURSION_LIMIT), 5)
            .unwrap();
        assert_eq!(dst.value, 42);
    }

    #[test]
    fn test_merge_group_empty() {
        // Group with no fields — just EndGroup.
        let data = group_bytes(0, 3);
        let mut dst = FlatMsg::default();
        dst.merge_group(&mut data.as_slice(), crate::test_ctx(RECURSION_LIMIT), 3)
            .unwrap();
        assert_eq!(dst.value, 0);
    }

    #[test]
    fn test_merge_group_merges_into_existing() {
        let data1 = group_bytes(1, 5);
        let data2 = group_bytes(2, 5);
        let mut dst = FlatMsg::default();
        dst.merge_group(&mut data1.as_slice(), crate::test_ctx(RECURSION_LIMIT), 5)
            .unwrap();
        assert_eq!(dst.value, 1);
        dst.merge_group(&mut data2.as_slice(), crate::test_ctx(RECURSION_LIMIT), 5)
            .unwrap();
        assert_eq!(dst.value, 2);
    }

    #[test]
    fn test_merge_group_recursion_limit_zero() {
        // depth=0 should immediately fail with RecursionLimitExceeded
        // because merge_group decrements before entering the loop.
        let data = group_bytes(42, 5);
        let mut dst = FlatMsg::default();
        assert_eq!(
            dst.merge_group(&mut data.as_slice(), crate::test_ctx(0), 5),
            Err(DecodeError::RecursionLimitExceeded)
        );
    }

    #[test]
    fn test_merge_group_recursion_limit_one_succeeds() {
        // depth=1 succeeds: merge_group decrements to 0, but FlatMsg's
        // merge_field doesn't recurse further.
        let data = group_bytes(7, 5);
        let mut dst = FlatMsg::default();
        dst.merge_group(&mut data.as_slice(), crate::test_ctx(1), 5)
            .unwrap();
        assert_eq!(dst.value, 7);
    }

    #[test]
    fn test_merge_group_mismatched_end() {
        // EndGroup with wrong field number.
        use crate::encoding::{Tag, WireType};
        let mut data = alloc::vec::Vec::new();
        Tag::new(99, WireType::EndGroup).encode(&mut data);

        let mut dst = FlatMsg::default();
        assert_eq!(
            dst.merge_group(&mut data.as_slice(), crate::test_ctx(RECURSION_LIMIT), 5),
            Err(DecodeError::InvalidEndGroup(99))
        );
    }

    #[test]
    fn test_merge_group_truncated() {
        // Buffer ends without EndGroup tag.
        use crate::encoding::{Tag, WireType};
        let mut data = alloc::vec::Vec::new();
        Tag::new(1, WireType::Varint).encode(&mut data);
        crate::types::encode_int32(42, &mut data);
        // No EndGroup.

        let mut dst = FlatMsg::default();
        assert_eq!(
            dst.merge_group(&mut data.as_slice(), crate::test_ctx(RECURSION_LIMIT), 5),
            Err(DecodeError::UnexpectedEof)
        );
    }

    #[test]
    fn test_merge_group_empty_buffer() {
        let mut dst = FlatMsg::default();
        assert_eq!(
            dst.merge_group(&mut [].as_slice(), crate::test_ctx(RECURSION_LIMIT), 5),
            Err(DecodeError::UnexpectedEof)
        );
    }

    #[test]
    fn test_merge_group_unknown_fields_skipped() {
        // Group body contains an unknown field (field 99) which FlatMsg
        // routes to skip_field; the known field (field 1) should still
        // be decoded.
        use crate::encoding::{Tag, WireType};
        let mut data = alloc::vec::Vec::new();
        // Unknown varint field 99 = 0
        Tag::new(99, WireType::Varint).encode(&mut data);
        crate::encoding::encode_varint(0, &mut data);
        // Known field 1 = 99
        Tag::new(1, WireType::Varint).encode(&mut data);
        crate::types::encode_int32(99, &mut data);
        // EndGroup(5)
        Tag::new(5, WireType::EndGroup).encode(&mut data);

        let mut dst = FlatMsg::default();
        dst.merge_group(&mut data.as_slice(), crate::test_ctx(RECURSION_LIMIT), 5)
            .unwrap();
        assert_eq!(dst.value, 99);
    }

    #[test]
    fn test_merge_group_trailing_data_preserved() {
        // After the EndGroup tag, trailing data should remain in the buffer.
        let mut data = group_bytes(42, 5);
        data.extend_from_slice(&[0xDE, 0xAD]);

        let mut cur = data.as_slice();
        let mut dst = FlatMsg::default();
        dst.merge_group(&mut cur, crate::test_ctx(RECURSION_LIMIT), 5)
            .unwrap();
        assert_eq!(dst.value, 42);
        assert_eq!(cur, &[0xDE, 0xAD]);
    }

    // ── read_varint (std::io::Read) tests ──────────────────────────────

    #[cfg(feature = "std")]
    mod read_varint_tests {
        use super::super::read_varint;
        use crate::encoding::encode_varint;

        #[test]
        fn roundtrip_values() {
            let cases: &[u64] = &[0, 1, 127, 128, 300, 1 << 14, 1 << 35, 1 << 63, u64::MAX];
            for &v in cases {
                let mut buf = Vec::new();
                encode_varint(v, &mut buf);
                let got = read_varint(&mut buf.as_slice()).unwrap();
                assert_eq!(got, v, "roundtrip failed for {v}");
            }
        }

        #[test]
        fn rejects_10th_byte_overflow() {
            // 9 continuation bytes + 10th byte with overflow bit (0x02).
            // Mirrors encoding::tests::test_varint_10th_byte_overflow_rejected.
            let mut bad: Vec<u8> = vec![0xFF; 9];
            bad.push(0x02);
            let err = read_varint(&mut bad.as_slice()).unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        }

        #[test]
        fn rejects_11th_byte() {
            // 10 continuation bytes — implies an 11th byte is needed.
            let bad: &[u8] = &[0xFF; 10];
            let err = read_varint(&mut &bad[..]).unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        }

        #[test]
        fn u64_max_roundtrips() {
            // u64::MAX requires 10 bytes with the 10th byte == 0x01.
            let mut buf = Vec::new();
            encode_varint(u64::MAX, &mut buf);
            assert_eq!(buf.len(), 10);
            assert_eq!(buf[9], 0x01);
            let got = read_varint(&mut buf.as_slice()).unwrap();
            assert_eq!(got, u64::MAX);
        }

        #[test]
        fn eof_before_terminator_is_error() {
            // Single continuation byte with no follow-up.
            let bad: &[u8] = &[0x80];
            let err = read_varint(&mut &bad[..]).unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
        }

        #[test]
        fn empty_input_is_error() {
            let err = read_varint(&mut &[][..]).unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
        }
    }

    // ── Encode-side 2 GiB guard tests ──────────────────────────────────

    use crate::test_doubles::SizedMsg;

    const OVER_LIMIT: u32 = MAX_MESSAGE_BYTES + 1;

    #[test]
    fn saturate_size_is_exact_below_u32_max_and_saturates_above() {
        assert_eq!(saturate_size(0), 0);
        assert_eq!(saturate_size(MAX_MESSAGE_BYTES as u64), MAX_MESSAGE_BYTES);
        // 2–4 GiB window: exact (this is what makes the guard byte-precise).
        assert_eq!(saturate_size(0x8000_0000), 0x8000_0000u32);
        assert_eq!(saturate_size(u32::MAX as u64), u32::MAX);
        assert_eq!(saturate_size(u32::MAX as u64 + 1), u32::MAX);
        assert_eq!(saturate_size(u64::MAX), u32::MAX);
    }

    #[test]
    fn encode_at_exactly_max_size_is_allowed() {
        // The guard is strictly-greater-than: a (fake) message of exactly
        // MAX_MESSAGE_BYTES passes. `encode` has no write-count ledger, so
        // the no-op write_to is fine here.
        let msg = SizedMsg {
            reported_size: MAX_MESSAGE_BYTES,
        };
        let mut buf = alloc::vec::Vec::new();
        msg.encode(&mut buf);
        assert!(msg.try_encode(&mut buf).is_ok());
    }

    #[test]
    #[should_panic(expected = "2 GiB protobuf limit")]
    fn encode_over_limit_panics() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let mut buf = alloc::vec::Vec::new();
        msg.encode(&mut buf);
    }

    #[test]
    #[should_panic(expected = "2 GiB protobuf limit")]
    fn encoded_len_over_limit_panics() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let _ = msg.encoded_len();
    }

    #[test]
    #[should_panic(expected = "2 GiB protobuf limit")]
    fn encode_with_cache_over_limit_panics() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let mut cache = SizeCache::new();
        let mut buf = alloc::vec::Vec::new();
        msg.encode_with_cache(&mut cache, &mut buf);
    }

    #[test]
    #[should_panic(expected = "2 GiB protobuf limit")]
    fn encode_to_vec_over_limit_panics() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let _ = msg.encode_to_vec();
    }

    #[test]
    #[should_panic(expected = "2 GiB protobuf limit")]
    fn encode_to_bytes_over_limit_panics() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let _ = msg.encode_to_bytes();
    }

    #[test]
    #[should_panic(expected = "2 GiB protobuf limit")]
    fn encode_length_delimited_over_limit_panics() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let mut buf = alloc::vec::Vec::new();
        msg.encode_length_delimited(&mut buf);
    }

    #[test]
    fn try_encode_over_limit_errors_and_writes_nothing() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let mut buf = alloc::vec::Vec::new();
        assert_eq!(msg.try_encode(&mut buf), Err(EncodeError::MessageTooLarge));
        assert!(buf.is_empty(), "no bytes may reach the buffer on Err");
        let mut cache = SizeCache::new();
        assert_eq!(
            msg.try_encode_with_cache(&mut cache, &mut buf),
            Err(EncodeError::MessageTooLarge)
        );
        assert!(buf.is_empty(), "no bytes may reach the buffer on Err");
        assert_eq!(
            msg.try_encode_length_delimited(&mut buf),
            Err(EncodeError::MessageTooLarge)
        );
        assert!(buf.is_empty(), "not even the length prefix");
        assert_eq!(msg.try_encode_to_vec(), Err(EncodeError::MessageTooLarge));
        assert_eq!(msg.try_encode_to_bytes(), Err(EncodeError::MessageTooLarge));
        assert_eq!(msg.try_encoded_len(), Err(EncodeError::MessageTooLarge));
    }

    #[test]
    fn try_encode_matches_encode_for_normal_messages() {
        let msg = FlatMsg { value: 42 };
        let mut expected = alloc::vec::Vec::new();
        msg.encode(&mut expected);
        let mut actual = alloc::vec::Vec::new();
        msg.try_encode(&mut actual).unwrap();
        assert_eq!(actual, expected);
        let mut cache = SizeCache::new();
        let mut cached = alloc::vec::Vec::new();
        msg.try_encode_with_cache(&mut cache, &mut cached).unwrap();
        assert_eq!(cached, expected);
        assert_eq!(msg.try_encode_to_vec().unwrap(), expected);
        assert_eq!(msg.try_encode_to_bytes().unwrap(), expected);
        assert_eq!(msg.try_encoded_len().unwrap(), expected.len() as u32);

        let mut ld_expected = alloc::vec::Vec::new();
        msg.encode_length_delimited(&mut ld_expected);
        let mut ld_actual = alloc::vec::Vec::new();
        msg.try_encode_length_delimited(&mut ld_actual).unwrap();
        assert_eq!(ld_actual, ld_expected);
    }

    #[test]
    fn try_encode_bounded_within_budget_encodes_and_returns_len() {
        let msg = FlatMsg { value: 42 };
        let mut expected = alloc::vec::Vec::new();
        msg.encode(&mut expected);
        let budget = expected.len() as u32;

        let mut buf = alloc::vec::Vec::new();
        let len = msg.try_encode_bounded(budget, &mut buf).unwrap();
        assert_eq!(buf, expected);
        assert_eq!(len, budget);

        // with_cache variant agrees
        let mut cache = SizeCache::new();
        let mut buf2 = alloc::vec::Vec::new();
        let len2 = msg
            .try_encode_bounded_with_cache(budget, &mut cache, &mut buf2)
            .unwrap();
        assert_eq!(buf2, expected);
        assert_eq!(len2, budget);
    }

    #[test]
    fn try_encode_bounded_at_exact_limit_succeeds() {
        let msg = FlatMsg { value: 42 };
        let exact = msg.encoded_len();
        let mut buf = alloc::vec::Vec::new();
        assert!(msg.try_encode_bounded(exact, &mut buf).is_ok());
    }

    #[test]
    fn try_encode_bounded_over_budget_errors_and_writes_nothing() {
        let msg = FlatMsg { value: 42 };
        let len = msg.encoded_len();
        let budget = len - 1; // one byte under

        let mut buf = alloc::vec::Vec::new();
        assert_eq!(
            msg.try_encode_bounded(budget, &mut buf),
            Err(EncodeError::ExceedsBudget {
                len,
                max_bytes: budget
            })
        );
        assert!(buf.is_empty(), "nothing written on budget exceeded");
    }

    /// "Nothing written on `Err`" has to hold for a sink that already has
    /// bytes in it, which is how a caller framing several messages uses one
    /// buffer. Every other test starts from an empty `Vec`, so the append
    /// case — where a partial write would corrupt the *preceding* message
    /// rather than produce an obviously empty one — would go unnoticed.
    #[test]
    fn try_encode_bounded_over_budget_leaves_a_populated_buffer_untouched() {
        let msg = FlatMsg { value: 42 };
        let len = msg.encoded_len();
        let budget = len - 1;

        let prefix = b"already framed".to_vec();
        let mut buf = prefix.clone();
        assert_eq!(
            msg.try_encode_bounded(budget, &mut buf),
            Err(EncodeError::ExceedsBudget {
                len,
                max_bytes: budget
            })
        );
        assert_eq!(buf, prefix, "the bytes already in the sink must survive");
    }

    #[test]
    fn try_encode_bounded_zero_budget_with_empty_message_succeeds() {
        // A default FlatMsg with value=0 encodes to 0 bytes (all defaults
        // are elided in proto3), so budget=0 is exactly satisfied.
        let msg = FlatMsg { value: 0 };
        let mut buf = alloc::vec::Vec::new();
        let len = msg.try_encode_bounded(0, &mut buf).unwrap();
        assert_eq!(len, 0);
        assert!(buf.is_empty());
    }

    #[test]
    fn try_encode_bounded_over_protobuf_limit_errors_as_too_large() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let mut buf = alloc::vec::Vec::new();
        assert_eq!(
            msg.try_encode_bounded(u32::MAX, &mut buf),
            Err(EncodeError::MessageTooLarge)
        );
        assert!(buf.is_empty());
    }

    #[test]
    fn pool_try_encode_bounded_within_budget_encodes_and_returns_len() {
        let msg = FlatMsg { value: 42 };
        let mut expected = alloc::vec::Vec::new();
        msg.encode(&mut expected);
        let budget = expected.len() as u32;

        let mut pool = crate::SizeCachePool::sequential(64);
        let mut buf = alloc::vec::Vec::new();
        let len = pool.try_encode_bounded(&msg, budget, &mut buf).unwrap();
        assert_eq!(buf, expected);
        assert_eq!(len, budget);
    }

    #[test]
    fn pool_try_encode_bounded_over_budget_errors_and_writes_nothing() {
        let msg = FlatMsg { value: 42 };
        let len = msg.encoded_len();
        let budget = len - 1;

        let mut pool = crate::SizeCachePool::sequential(64);
        let mut buf = alloc::vec::Vec::new();
        assert_eq!(
            pool.try_encode_bounded(&msg, budget, &mut buf),
            Err(EncodeError::ExceedsBudget {
                len,
                max_bytes: budget
            })
        );
        assert!(buf.is_empty());
        // Pool buffer must be returned even on Err.
        assert_eq!(pool.try_encode_bounded(&msg, len, &mut buf).unwrap(), len);
    }

    #[test]
    fn pool_try_encode_bounded_over_protobuf_limit_errors_as_too_large() {
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let mut pool = crate::SizeCachePool::sequential(64);
        let mut buf = alloc::vec::Vec::new();
        assert_eq!(
            pool.try_encode_bounded(&msg, u32::MAX, &mut buf),
            Err(EncodeError::MessageTooLarge)
        );
        assert!(buf.is_empty());
    }

    #[test]
    #[should_panic(expected = "2 GiB protobuf limit")]
    fn pool_encoded_len_over_limit_panics() {
        // SizeCachePool::encoded_len documents itself as the pooled
        // equivalent of Message::encoded_len — it must share the guard.
        let msg = SizedMsg {
            reported_size: OVER_LIMIT,
        };
        let mut pool = crate::SizeCachePool::sequential(1024);
        let _ = pool.encoded_len(&msg);
    }

    #[cfg(debug_assertions)]
    #[test]
    #[should_panic(expected = "two-pass traversal mismatch")]
    fn encode_to_vec_ledger_catches_size_write_disagreement() {
        // An under-limit reported size with a no-op write_to: the guard
        // passes, then the debug ledger flags the two-pass divergence.
        let msg = SizedMsg { reported_size: 3 };
        let _ = msg.encode_to_vec();
    }

    // ── DecodeOptions std::io::Read tests ─────────────────────────────

    #[cfg(feature = "std")]
    mod reader_tests {
        use super::*;

        #[test]
        fn decode_reader_basic() {
            let src = FlatMsg { value: 42 };
            let bytes = src.encode_to_vec();
            let msg: FlatMsg = DecodeOptions::new()
                .decode_reader(&mut bytes.as_slice())
                .unwrap();
            assert_eq!(msg.value, 42);
        }

        #[test]
        fn decode_reader_rejects_oversize() {
            let src = FlatMsg { value: 42 };
            let bytes = src.encode_to_vec();
            let err = DecodeOptions::new()
                .with_max_message_size(1)
                .decode_reader::<FlatMsg>(&mut bytes.as_slice())
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        }

        #[test]
        fn decode_reader_exact_boundary() {
            // max_message_size == encoded length → success.
            let src = FlatMsg { value: 42 };
            let bytes = src.encode_to_vec();
            let msg: FlatMsg = DecodeOptions::new()
                .with_max_message_size(bytes.len())
                .decode_reader(&mut bytes.as_slice())
                .unwrap();
            assert_eq!(msg.value, 42);
        }

        #[test]
        fn decode_options_reader_size_unbounded_getter_tracks_builder_order() {
            let opts = DecodeOptions::new();
            assert!(!opts.is_reader_size_unbounded());

            let opts = opts.without_reader_size_limit();
            assert!(opts.is_reader_size_unbounded());

            let opts = opts.with_max_message_size(1024);
            assert!(!opts.is_reader_size_unbounded());
        }

        #[test]
        fn decode_reader_without_size_limit_does_not_overflow() {
            let src = FlatMsg { value: 42 };
            let bytes = src.encode_to_vec();
            let msg: FlatMsg = DecodeOptions::new()
                .without_reader_size_limit()
                .decode_reader(&mut bytes.as_slice())
                .unwrap();
            assert_eq!(msg.value, 42);
        }

        #[test]
        fn decode_reader_with_max_message_size_after_without_limit_reenables_limit() {
            let src = FlatMsg { value: 42 };
            let bytes = src.encode_to_vec();
            let opts = DecodeOptions::new()
                .without_reader_size_limit()
                .with_max_message_size(1);
            assert!(!opts.is_reader_size_unbounded());

            let err = opts
                .decode_reader::<FlatMsg>(&mut bytes.as_slice())
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        }

        #[test]
        fn decode_reader_without_size_limit_keeps_slice_limit() {
            let src = FlatMsg { value: 42 };
            let bytes = src.encode_to_vec();
            let opts = DecodeOptions::new()
                .with_max_message_size(1)
                .without_reader_size_limit();
            assert!(opts.is_reader_size_unbounded());

            let slice_result: Result<FlatMsg, _> = opts.decode_from_slice(&bytes);
            assert_eq!(slice_result, Err(DecodeError::MessageTooLarge));

            let msg: FlatMsg = opts.decode_reader(&mut bytes.as_slice()).unwrap();
            assert_eq!(msg.value, 42);
        }

        #[test]
        fn decode_reader_propagates_read_error() {
            // A reader that errors immediately.
            struct ErrReader;
            impl std::io::Read for ErrReader {
                fn read(&mut self, _: &mut [u8]) -> std::io::Result<usize> {
                    Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "gone"))
                }
            }
            let err = DecodeOptions::new()
                .decode_reader::<FlatMsg>(&mut ErrReader)
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::BrokenPipe);
        }

        #[test]
        fn decode_length_delimited_reader_basic() {
            let src = FlatMsg { value: 99 };
            let mut ld = Vec::new();
            src.encode_length_delimited(&mut ld);
            let msg: FlatMsg = DecodeOptions::new()
                .decode_length_delimited_reader(&mut ld.as_slice())
                .unwrap();
            assert_eq!(msg.value, 99);
        }

        #[test]
        fn decode_length_delimited_reader_rejects_oversize_prefix() {
            // Length prefix claims more bytes than max_message_size allows.
            let src = FlatMsg { value: 99 };
            let mut ld = Vec::new();
            src.encode_length_delimited(&mut ld);
            let err = DecodeOptions::new()
                .with_max_message_size(1)
                .decode_length_delimited_reader::<FlatMsg>(&mut ld.as_slice())
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        }

        #[test]
        fn decode_length_delimited_reader_zero_length() {
            // A zero length prefix decodes to the default message and
            // consumes only the prefix byte.
            let stream = [0x00u8, 0xFF]; // len=0, then unrelated trailing byte
            let mut cursor = std::io::Cursor::new(stream);
            let msg: FlatMsg = DecodeOptions::new()
                .decode_length_delimited_reader(&mut cursor)
                .unwrap();
            assert_eq!(msg, FlatMsg::default());
            assert_eq!(cursor.position(), 1);
        }

        #[test]
        fn decode_length_delimited_reader_truncated_reports_eof() {
            // Length prefix declares more bytes than the stream delivers.
            let src = FlatMsg { value: 99 };
            let mut ld = Vec::new();
            src.encode_length_delimited(&mut ld);
            ld.truncate(ld.len() - 1);
            let err = DecodeOptions::new()
                .decode_length_delimited_reader::<FlatMsg>(&mut ld.as_slice())
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
        }

        #[test]
        fn decode_length_delimited_reader_huge_claim_without_delivery() {
            // A 5-byte prefix declaring ~2 GiB followed by EOF must fail with
            // UnexpectedEof without allocating the declared length up front —
            // the buffer only grows as bytes are actually delivered. (This
            // test completes instantly; before the incremental-read fix it
            // zero-filled a ~2 GiB buffer first.)
            let mut stream = Vec::new();
            encode_varint(0x7FFF_FFF0, &mut stream); // just under the cap
            let err = DecodeOptions::new()
                .decode_length_delimited_reader::<FlatMsg>(&mut stream.as_slice())
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
        }

        #[test]
        fn decode_length_delimited_without_reader_size_limit_keeps_protobuf_cap() {
            let mut stream = Vec::new();
            encode_varint(0x8000_0000, &mut stream);
            let result: Result<FlatMsg, _> = DecodeOptions::new()
                .without_reader_size_limit()
                .decode_length_delimited(&mut stream.as_slice());
            assert_eq!(result, Err(DecodeError::MessageTooLarge));
        }

        #[test]
        fn decode_length_delimited_reader_without_size_limit_keeps_protobuf_cap() {
            let mut stream = Vec::new();
            encode_varint(0x8000_0000, &mut stream);
            let err = DecodeOptions::new()
                .without_reader_size_limit()
                .decode_length_delimited_reader::<FlatMsg>(&mut stream.as_slice())
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        }

        #[test]
        fn decode_length_delimited_reader_sequential() {
            // Two messages in a stream — typical log-file use case.
            let a = FlatMsg { value: 10 };
            let b = FlatMsg { value: 20 };
            let mut stream = Vec::new();
            a.encode_length_delimited(&mut stream);
            b.encode_length_delimited(&mut stream);

            let mut cursor = std::io::Cursor::new(stream);
            let first: FlatMsg = DecodeOptions::new()
                .decode_length_delimited_reader(&mut cursor)
                .unwrap();
            assert_eq!(first.value, 10);
            let second: FlatMsg = DecodeOptions::new()
                .decode_length_delimited_reader(&mut cursor)
                .unwrap();
            assert_eq!(second.value, 20);
        }

        #[test]
        fn decode_length_delimited_reader_truncated_body() {
            // Length prefix says N bytes but reader EOFs early.
            let mut buf = Vec::new();
            crate::encoding::encode_varint(100, &mut buf);
            buf.push(0x08);
            let err = DecodeOptions::new()
                .decode_length_delimited_reader::<FlatMsg>(&mut buf.as_slice())
                .unwrap_err();
            assert_eq!(err.kind(), std::io::ErrorKind::UnexpectedEof);
        }
    }
}