puns 0.1.0

Type punning proven at compile time: a safe alternative to transmute and to hand-written parsing of bytes
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
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
//! Value-level description of the memory layout and of the valid bit patterns of a type.
//!
//! Every [`Pun`](crate::Pun) type exposes a `const LAYOUT: Layout`: a small tree, built at
//! compile time, that tells for every byte of the type whether it is padding, an unconstrained
//! initialized byte, or part of a *scalar* with a restricted set of valid values.
//!
//! The whole static analysis of the crate is ordinary `const fn` code running over those values
//! (see `analysis.rs`); nothing is encoded in the type system.

use core::mem;

#[doc(hidden)]
pub use crate::discover::Desc;
use crate::ptr;

// -------------------------------------------------------------------------------------------------
// `Endian`

/// Byte order of a scalar.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Endian {
  Little,
  Big,
}

impl Endian {
  /// Byte order of the target.
  pub const NATIVE: Endian =
    if cfg!(target_endian = "little") { Endian::Little } else { Endian::Big };

  #[inline]
  pub(crate) const fn is_little(self) -> bool {
    matches!(self, Endian::Little)
  }

  #[inline]
  pub(crate) const fn equals(self, other: Endian) -> bool {
    self.is_little() == other.is_little()
  }
}

// -------------------------------------------------------------------------------------------------
// `Spec`

/// Values a scalar is allowed to take.
#[derive(Copy, Clone, Debug)]
pub(crate) enum Valid {
  /// Anything, including uninitialized bytes.
  Uninit,
  /// Any initialized value.
  Any,
  /// Sorted, disjoint and non-adjacent inclusive ranges. Never empty, never the full range.
  In(&'static [(u128, u128)]),
}

/// Specification of a scalar: its size, its byte order and the values it accepts.
#[derive(Copy, Clone, Debug)]
pub struct Spec {
  pub(crate) size: usize,
  pub(crate) endian: Endian,
  /// What a value must be to be accepted: what is required of a target.
  pub(crate) valid: Valid,
  /// What a value is known to be, whatever safe code does: what a source guarantees.
  ///
  /// This is `valid`, but for fields that are restricted by `#[pun(valid(..))]`: nothing
  /// prevents safe code from writing any value of the type of the field to it.
  pub(crate) known: Valid,
  /// Whatever its bits are the value of an unsigned integer, which rules read. Not those of a
  /// signed or floating-point number, a `bool`, a `char` or an enum.
  pub(crate) number: bool,
}

/// Greatest value of an unsigned integer of `size` bytes.
#[inline]
pub(crate) const fn max_value(size: usize) -> u128 {
  if size >= 16 { u128::MAX } else { (1u128 << (8 * size)) - 1 }
}

impl Spec {
  /// A scalar the size of `T` that accepts every initialized bit pattern.
  #[inline]
  #[must_use]
  pub const fn any<T>() -> Self {
    let size = size_of::<T>();
    Self { size, endian: Endian::NATIVE, valid: Valid::Any, known: Valid::Any, number: true }
  }

  /// A scalar of `size` bytes (at most 16) of native byte order that accepts every initialized
  /// bit pattern: the tag of an enum that `discover.rs` found.
  pub(crate) const fn of_size(size: usize) -> Self {
    assert!(size <= 16, "a scalar is at most 16 bytes wide");
    Self { size, endian: Endian::NATIVE, valid: Valid::Any, known: Valid::Any, number: true }
  }

  /// A scalar the size of `T` that accepts everything, uninitialized bytes included.
  #[inline]
  #[must_use]
  pub const fn uninit<T>() -> Self {
    let (size, endian) = (size_of::<T>(), Endian::NATIVE);
    Self { size, endian, valid: Valid::Uninit, known: Valid::Uninit, number: true }
  }

  /// A scalar the size of `T` whose value, read as an unsigned integer in `endian` byte order,
  /// must be in one of `ranges`.
  ///
  /// `ranges` are inclusive and must be sorted, disjoint and non-adjacent; this is checked at
  /// compile time. See [`sorted_ranges`] to build them from a list of values.
  #[must_use]
  pub const fn ranges<T>(endian: Endian, ranges: &'static [(u128, u128)]) -> Self {
    let size = size_of::<T>();
    Self { size, endian, valid: Valid::Any, known: Valid::Any, number: true }.with_ranges(ranges)
  }

  /// Same as `self`, with other valid `ranges`, see [`Spec::ranges`].
  ///
  /// # Panics
  /// If the scalar is not 1 to 16 bytes wide, or if `ranges` is empty, not sorted, overlapping,
  /// adjacent or out of the bounds of the scalar.
  #[must_use]
  pub const fn with_ranges(self, ranges: &'static [(u128, u128)]) -> Self {
    let size = self.size;
    assert!(size >= 1 && size <= 16, "a constrained scalar must be 1 to 16 bytes wide");
    assert!(!ranges.is_empty(), "a scalar must have at least one valid value");
    let max = max_value(size);
    let mut i = 0;
    while i < ranges.len() {
      let (lo, hi) = ranges[i];
      assert!(lo <= hi && hi <= max, "scalar range is empty or out of bounds");
      if i > 0 {
        let prev = ranges[i - 1].1;
        assert!(prev < lo && lo - prev > 1, "scalar ranges must be sorted and non-adjacent");
      }
      i += 1;
    }
    let full = ranges.len() == 1 && ranges[0].0 == 0 && ranges[0].1 == max;
    let valid = if full { Valid::Any } else { Valid::In(ranges) };
    Self { valid, known: valid, ..self }
  }

  /// Same as `self`, for a scalar that may be uninitialized: what a target requires is still
  /// `self` (initialized, and one of its values), but nothing is known of what a source holds
  /// there, not even that it is initialized.
  ///
  /// This is a byte that is either a byte of a tag, whose value is the one that `self` accepts,
  /// or padding, without a way to tell which: the bytes of a tag that `rustc` widened past its
  /// values, in an enum without `repr` (see `discover.rs`). Requiring the value is right in
  /// either case, and so is knowing nothing.
  ///
  /// # Panics
  /// If `self` accepts every initialized value, or uninitialized bytes: such a scalar is
  /// [`Spec::uninit`].
  #[must_use]
  pub const fn maybe_uninit(self) -> Self {
    assert!(matches!(self.valid, Valid::In(_)), "a scalar that may be uninitialized restricts");
    Self { known: Valid::Uninit, ..self }
  }

  /// Whatever a source may hold uninitialized bytes in this scalar.
  #[inline]
  pub(crate) const fn may_be_uninit(&self) -> bool {
    matches!(self.known, Valid::Uninit)
  }

  /// Same as `self`, for a scalar whose bits are not the value of an unsigned integer: a signed
  /// or floating-point number, a `bool`, a `char`, an enum. The expressions of `#[pun(..)]`,
  /// which compute on unsigned integers, do not read it.
  #[must_use]
  pub const fn not_a_number(self) -> Self {
    Self { number: false, ..self }
  }

  /// Whatever `T` has no size, for `#[derive(Pun)]`.
  #[doc(hidden)]
  #[must_use]
  pub const fn is_zero_sized<T>() -> bool {
    size_of::<T>() == 0
  }

  /// [`Spec::ranges`], in the byte order of the target.
  #[doc(hidden)]
  #[must_use]
  pub const fn native_ranges<T>(ranges: &'static [(u128, u128)]) -> Self {
    Self::ranges::<T>(Endian::NATIVE, ranges).not_a_number()
  }

  /// [`sorted_ranges`], for `#[derive(Pun)]`.
  #[doc(hidden)]
  #[must_use]
  pub const fn sorted_ranges<const N: usize>(values: [u128; N]) -> ([(u128, u128); N], usize) {
    sorted_ranges(values)
  }

  /// [`range`], for `#[derive(Pun)]`.
  #[doc(hidden)]
  #[must_use]
  pub const fn range<T>(start: Option<u128>, end: Option<u128>, inclusive: bool) -> (u128, u128) {
    range::<T>(start, end, inclusive)
  }

  /// [`merge_ranges`], for `#[derive(Pun)]`.
  #[doc(hidden)]
  #[must_use]
  pub const fn merge_ranges<const N: usize>(
    ranges: [(u128, u128); N],
  ) -> ([(u128, u128); N], usize) {
    merge_ranges(ranges)
  }

  /// [`bits_of`], for `#[derive(Pun)]`.
  ///
  /// # Safety
  /// Every byte of `value` must be initialized.
  #[doc(hidden)]
  #[must_use]
  pub const unsafe fn bits_of<T>(value: &T) -> u128 {
    // SAFETY: guaranteed by the caller.
    unsafe { bits_of(value) }
  }

  /// Whatever the scalar is only restricted by a `#[pun(valid(..))]` that the type system does
  /// not enforce: it is then proven by restrictions of the same kind.
  #[inline]
  pub(crate) const fn is_soft(&self) -> bool {
    matches!(self.known, Valid::Any) && matches!(self.valid, Valid::In(_))
  }
  /// Same as `self`, read in another byte order.
  #[inline]
  #[must_use]
  pub const fn with_endian(self, endian: Endian) -> Self {
    Self { endian, ..self }
  }

  /// Whatever the scalar is an initialized integer of at most 16 bytes.
  #[doc(hidden)]
  #[must_use]
  pub const fn is_integer(&self) -> bool {
    self.size <= 16 && !matches!(self.known, Valid::Uninit) && !matches!(self.valid, Valid::Uninit)
  }

  /// The scalar at `ptr`, as an unsigned integer.
  ///
  /// # Safety
  /// `self` must be [an integer](Self::is_integer), and `ptr` must be valid for reads of one.
  #[doc(hidden)]
  #[inline(always)]
  #[must_use]
  pub const unsafe fn load(&self, ptr: *const u8) -> u128 {
    // SAFETY: guaranteed by the caller.
    unsafe { load(ptr, self.size, self.endian) }
  }

  /// Whatever the scalar restricts the values it accepts.
  #[inline]
  pub(crate) const fn is_constrained(&self) -> bool {
    matches!(self.valid, Valid::In(_))
  }

  /// Whatever `value` is valid for this scalar.
  #[must_use]
  pub const fn contains(&self, value: u128) -> bool {
    match self.valid {
      Valid::Uninit | Valid::Any => self.size >= 16 || value <= max_value(self.size),
      Valid::In(ranges) => contains(ranges, value),
    }
  }

  /// Whatever the scalar stored at `ptr` is valid.
  ///
  /// # Safety
  /// `ptr` must be valid for reads of `self.size` initialized bytes (no alignment requirement).
  #[inline(always)]
  pub(crate) const unsafe fn matches(&self, ptr: *const u8) -> bool {
    match self.valid {
      Valid::Uninit | Valid::Any => true,
      // SAFETY: guaranteed by the caller.
      Valid::In(ranges) => contains(ranges, unsafe { load(ptr, self.size, self.endian) }),
    }
  }
}

#[inline(always)]
pub(crate) const fn contains(ranges: &[(u128, u128)], value: u128) -> bool {
  let mut i = 0;
  // A few ranges: a comparison each, without a branch, so that the validation of a slice can be
  // vectorised (see `validate::validate_elems`).
  if ranges.len() <= 8 {
    let mut found = false;
    while i < ranges.len() {
      found |= value.wrapping_sub(ranges[i].0) <= ranges[i].1 - ranges[i].0;
      i += 1;
    }
    return found;
  }
  while i < ranges.len() {
    if ranges[i].0 <= value && value <= ranges[i].1 {
      return true;
    }
    i += 1;
  }
  false
}

/// Read an unsigned integer of `size <= 16` bytes.
///
/// # Safety
/// `ptr` must be valid for reads of `size` initialized bytes.
#[inline(always)]
pub(crate) const unsafe fn load(ptr: *const u8, size: usize, endian: Endian) -> u128 {
  let mut buf = [0u8; 16];
  // SAFETY: `size <= 16` and `ptr` is readable for `size` bytes.
  unsafe {
    match endian {
      Endian::Little => {
        core::ptr::copy_nonoverlapping(ptr, buf.as_mut_ptr(), size);
        u128::from_le_bytes(buf)
      }
      Endian::Big => {
        core::ptr::copy_nonoverlapping(ptr, buf.as_mut_ptr().add(16 - size), size);
        u128::from_be_bytes(buf)
      }
    }
  }
}

/// Sort `values` and merge them into inclusive ranges; returns the ranges and their count.
///
/// ```
/// use puns::layout::sorted_ranges;
///
/// assert_eq!(sorted_ranges([7, 1, 0, 2]), ([(0, 2), (7, 7), (0, 0), (0, 0)], 2));
/// ```
#[must_use]
pub const fn sorted_ranges<const N: usize>(mut values: [u128; N]) -> ([(u128, u128); N], usize) {
  // Insertion sort.
  let mut i = 1;
  while i < N {
    let mut j = i;
    while j > 0 && values[j - 1] > values[j] {
      values.swap(j - 1, j);
      j -= 1;
    }
    i += 1;
  }
  // Merge duplicates and neighbors.
  let (mut ranges, mut len, mut i) = ([(0, 0); N], 0, 0);
  while i < N {
    if len > 0 && values[i] - ranges[len - 1].1 <= 1 {
      ranges[len - 1].1 = values[i];
    } else {
      ranges[len] = (values[i], values[i]);
      len += 1;
    }
    i += 1;
  }
  (ranges, len)
}

// -------------------------------------------------------------------------------------------------
// `Layout`

/// What all the bytes of a type have in common, if anything.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum Uniform {
  /// Nothing: the type must be looked into.
  No,
  /// Every byte is padding.
  Pad,
  /// Every byte is initialized and unconstrained.
  Any,
}

#[derive(Copy, Clone, Debug)]
pub(crate) enum Kind {
  Scalar(Spec),
  Array(&'static Layout, usize),
  Slice(&'static Layout),
  Struct(&'static [Field], &'static [Rule]),
  /// A tail of bytes that a [`Dfa`] accepts.
  Pattern(&'static Dfa),
  /// A tag, and the fields of the variant that the tag tells.
  Enum(&'static Enum),
  /// A self-relative pointer, see [`Pointer`].
  Pointer(Pointer),
}

/// What a self-relative pointer aims at, elsewhere in the buffer that holds it.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Aim {
  /// One value of the target type, at an address that is aligned for it.
  One,
  /// Values of the target type one after the other, `size_of` apart, the first at an address
  /// that is a multiple of `align`. How many there are is told by the bytes of the buffer, as
  /// the scheme of the pointer says.
  Sequence {
    /// The alignment of the first element that the declarer of the pointer guarantees: a power
    /// of two, at least the alignment of the element type.
    align: usize,
  },
  /// Bytes that are UTF-8. How many there are is told by the bytes of the buffer, as the scheme
  /// of the pointer says.
  Text,
}

/// A self-relative pointer: an offset from its own address to its target, in the same buffer.
///
/// This is what the analysis needs to know of a pointer, and nothing of how the crate that
/// declares it keeps its targets ([`SelfRelative`](crate::SelfRelative) says how it is
/// declared). [`Pointer::one`], [`Pointer::sequence`] and [`Pointer::text`] make one, with:
///
/// - a **scheme**: a name that the declaring crate chooses, which stands for the way a pointer
///   and the bytes of its buffer tell where its target is, and how long it is. A pointer is only
///   read as a pointer of the same scheme;
/// - its **offset**: the width in bytes of the integer (the size of the pointer) and its byte
///   order;
/// - what it **aims** at ([`Aim`]), with the size and the alignment of the target type (of an
///   element, for a sequence);
/// - whatever it is **nullable** ([`Pointer::or_null`]): an offset whose bytes are all zero is
///   then no pointer.
///
/// What is at the target is not part of the layout (the layout of a recursive type would have no
/// end): it is the [`Pun::Pointee`](crate::Pun::Pointee) of the type, which the analysis reaches
/// through types (see `deep.rs`).
///
/// What a pointer guarantees is not a property of its bytes. So no bytes ever prove one (only a
/// pointer does, whose target can be read as the target of the other), no check makes up for
/// one, and no `&mut` to one is ever handed out.
#[derive(Copy, Clone, Debug)]
pub struct Pointer {
  pub(crate) scheme: &'static str,
  pub(crate) aim: Aim,
  /// An offset whose bytes are all zero is no pointer.
  pub(crate) nullable: bool,
  /// The type of the pointer has no value whose bytes are all zero (its offset is a `NonZero`):
  /// found by [`Layout::pointer`], not told.
  pub(crate) nonzero: bool,
  /// Size of the offset, which is the size of the pointer: 1 to 16 bytes.
  pub(crate) width: usize,
  /// Byte order of the offset.
  pub(crate) endian: Endian,
  /// Size and alignment of the target type (`One`) or of an element (`Sequence`); those of
  /// `()` for `Text`.
  pub(crate) size: usize,
  pub(crate) align: usize,
}

impl Pointer {
  const fn new(
    scheme: &'static str,
    aim: Aim,
    width: usize,
    endian: Endian,
    (size, align): (usize, usize),
  ) -> Self {
    assert!(width >= 1 && width <= 16, "the offset of a pointer is 1 to 16 bytes");
    Self { scheme, aim, nullable: false, nonzero: false, width, endian, size, align }
  }

  /// The size and the alignment of `T`, which a pointer aims at.
  const fn target<T>() -> (usize, usize) {
    // A target without size could be anywhere, the pointer itself included, and is nothing that
    // a pointer can be read as: the proof stops at it (`deep::behind`).
    assert!(size_of::<T>() != 0, "a pointer aims at a type with a size");
    (size_of::<T>(), align_of::<T>())
  }

  /// A pointer of the scheme `scheme` to one `T`, whose offset is an integer of `width` bytes in
  /// the byte order `endian`.
  ///
  /// # Panics
  /// If `width` is not 1 to 16, or if `T` has no size.
  #[must_use]
  pub const fn one<T>(scheme: &'static str, width: usize, endian: Endian) -> Self {
    Self::new(scheme, Aim::One, width, endian, Self::target::<T>())
  }

  /// A pointer of the scheme `scheme` to a sequence of `T`, the first of which is at an address
  /// that is a multiple of `align`, whose offset is an integer of `width` bytes in the byte
  /// order `endian`.
  ///
  /// # Panics
  /// If `width` is not 1 to 16, if `T` has no size, or if `align` is not a power of two at least
  /// as large as the alignment of `T`.
  #[must_use]
  pub const fn sequence<T>(
    scheme: &'static str,
    width: usize,
    endian: Endian,
    align: usize,
  ) -> Self {
    assert!(
      align.is_power_of_two() && align >= align_of::<T>(),
      "the elements of a sequence are at an address aligned for them: a power of two, at least \
       the alignment of their type"
    );
    Self::new(scheme, Aim::Sequence { align }, width, endian, Self::target::<T>())
  }

  /// A pointer of the scheme `scheme` to text, whose offset is an integer of `width` bytes in the
  /// byte order `endian`. Its target type is `()`: there is nothing to relate behind it.
  ///
  /// # Panics
  /// If `width` is not 1 to 16.
  #[must_use]
  pub const fn text(scheme: &'static str, width: usize, endian: Endian) -> Self {
    Self::new(scheme, Aim::Text, width, endian, (size_of::<()>(), align_of::<()>()))
  }

  /// The same pointer, but nullable: an offset whose bytes are all zero is no pointer, and aims
  /// at nothing.
  #[must_use]
  pub const fn or_null(self) -> Self {
    Self { nullable: true, nonzero: false, ..self }
  }

  /// Whatever an offset whose bytes are all zero is no pointer ([`Pointer::or_null`]).
  #[must_use]
  pub const fn is_nullable(&self) -> bool {
    self.nullable
  }

  /// The scheme: the name that the declaring crate gave the way to the target.
  #[must_use]
  pub const fn scheme(&self) -> &'static str {
    self.scheme
  }

  /// What the pointer aims at: one value, a sequence (and the alignment that the declarer
  /// guarantees of its first element), or text.
  #[must_use]
  pub const fn aim(&self) -> Aim {
    self.aim
  }

  /// The size of the offset in bytes, which is the size of the pointer.
  #[must_use]
  pub const fn width(&self) -> usize {
    self.width
  }

  /// The byte order of the offset.
  #[must_use]
  pub const fn endian(&self) -> Endian {
    self.endian
  }

  /// The size of the target type (of an element of a sequence; 0 for text).
  #[must_use]
  pub const fn size(&self) -> usize {
    self.size
  }

  /// The alignment of the target type (of an element of a sequence; 1 for text).
  #[must_use]
  pub const fn align(&self) -> usize {
    self.align
  }

  /// Whatever the target of the pointer `self` can be read through the pointer `to`, as far as
  /// the pointers themselves go (what the targets hold is related through the types, see
  /// `deep.rs`): `None` if never, `Some(false)` if only when `self` is not null, which is then to
  /// be checked at run time.
  ///
  /// Both are of the same scheme, with the same offset (width and byte order: one is read as the
  /// other), so they find the same target in the same buffer. A value is read as a prefix of it
  /// that needs no more alignment than it has; a sequence as a sequence of elements of the same
  /// size (its length is a number of elements) that needs no more alignment than the declarer of
  /// `self` guarantees; text as text.
  pub(crate) const fn reads_as(&self, to: &Pointer) -> Option<bool> {
    let fits = str_equals(self.scheme, to.scheme)
      && self.width == to.width
      && self.endian.equals(to.endian)
      && match (self.aim, to.aim) {
        (Aim::One, Aim::One) => to.size <= self.size && to.align <= self.align,
        (Aim::Sequence { align: from }, Aim::Sequence { align: into }) => {
          to.size == self.size && into <= from
        }
        (Aim::Text, Aim::Text) => true,
        _ => false,
      };
    // What may be zero (a null, or an offset of zero of a type that has one) is read as what may
    // not be only when it is not, which is then checked at run time.
    let zero = self.nullable || !self.nonzero;
    if fits { Some(to.nullable || !to.nonzero && !self.nullable || !zero) } else { None }
  }

  /// Whatever two pointers are the same, as far as the pointers themselves go.
  const fn same(&self, other: &Pointer) -> bool {
    let aims = match (self.aim, other.aim) {
      (Aim::One, Aim::One) | (Aim::Text, Aim::Text) => true,
      (Aim::Sequence { align: a }, Aim::Sequence { align: b }) => a == b,
      _ => false,
    };
    aims
      && str_equals(self.scheme, other.scheme)
      && self.nullable == other.nullable
      && self.nonzero == other.nonzero
      && self.width == other.width
      && self.endian.equals(other.endian)
      && self.size == other.size
      && self.align == other.align
  }
}

/// Whatever two strings are the same, in `const`.
const fn str_equals(a: &str, b: &str) -> bool {
  let (a, b) = (a.as_bytes(), b.as_bytes());
  if a.len() != b.len() {
    return false;
  }
  let mut i = 0;
  while i < a.len() {
    if a[i] != b[i] {
      return false;
    }
    i += 1;
  }
  true
}

/// Layout of an enum with fields, see [`Layout::enumeration`].
///
/// The tag is a scalar at a fixed place: of the representation that Rust specifies, or of the one
/// that `rustc` chose, as `discover.rs` found it. In the second case it may be a niche: a scalar
/// of the fields of one variant (the one that holds the tag), whose other values are the
/// other variants.
#[derive(Debug)]
pub struct Enum {
  /// A scalar whose valid values are the tags of the variants.
  pub(crate) tag: Field,
  pub(crate) variants: &'static [Variant],
  /// Size and alignment that the representation of the enum specifies.
  pub(crate) specified: (usize, usize),
  /// A union: no tag, its variants are its fields, and a value is valid if it is one of them.
  pub(crate) union: bool,
}

impl Enum {
  /// The enum that `repr(C)` and `repr(u8)` (or any other integer) specify: a tag, which is a
  /// scalar whose valid values are the tags of the `variants`, then one of the `shadows`, the
  /// `repr(C)` structs of the fields of each variant, of which the sizes and alignments are
  /// given.
  ///
  /// With `repr(C)`, the tag is `outside`: the fields of a variant are at the offset of the
  /// shadows (`fields`) plus their offset in the shadow of the variant. Without, the tag is the
  /// first field of each shadow.
  ///
  /// # Panics
  /// If `fields` is not where the fields of the variants are.
  #[must_use]
  pub const fn new(
    tag: &'static Layout,
    outside: bool,
    shadows: &[(usize, usize)],
    fields: usize,
    variants: &'static [Variant],
  ) -> Self {
    let (mut size, mut align, mut i) = (0, 1, 0);
    while i < shadows.len() {
      if shadows[i].0 > size {
        size = shadows[i].0;
      }
      if shadows[i].1 > align {
        align = shadows[i].1;
      }
      i += 1;
    }
    let specified = if outside {
      assert!(fields == Self::fields(tag, shadows), "not where the fields of the variants are");
      let align = if tag.align > align { tag.align } else { align };
      (tail_offset(fields + size, align, usize::MAX), align)
    } else {
      assert!(fields == 0, "not where the fields of the variants are");
      (tail_offset(size, align, usize::MAX), align)
    };
    Self { tag: Field::new(0, tag), variants, specified, union: false }
  }

  /// The enum that `rustc` laid out, as `discover.rs` found it: a tag at `offset`, and the
  /// `variants` in the order of their declarations, of an enum of size and alignment `specified`.
  #[doc(hidden)]
  #[must_use]
  pub const fn discovered(
    tag: &'static Layout,
    offset: usize,
    variants: &'static [Variant],
    specified: (usize, usize),
  ) -> Self {
    Self { tag: Field::new(offset, tag), variants, specified, union: false }
  }

  /// The `repr(C)` union whose `fields` are given, each as a variant of one field (at offset 0),
  /// in the order of their declarations (see [`Layout::union`]).
  #[doc(hidden)]
  #[must_use]
  pub const fn union(fields: &'static [Variant]) -> Self {
    Self { tag: Field::new(0, &NO_TAG), variants: fields, specified: (0, 1), union: true }
  }

  /// An enum without a variant that a value can be: no value.
  const NEVER: Self =
    Self { tag: Field::new(0, &NO_TAG), variants: &[], specified: (0, 1), union: false };

  /// Offset of the fields of the variants of a `repr(C)` enum, whose tag is outside of the
  /// `shadows` (see [`Enum::new`]).
  #[must_use]
  pub const fn fields(tag: &Layout, shadows: &[(usize, usize)]) -> usize {
    let (mut align, mut i) = (1, 0);
    while i < shadows.len() {
      if shadows[i].1 > align {
        align = shadows[i].1;
      }
      i += 1;
    }
    tail_offset(tag.size, align, usize::MAX)
  }
}

/// A variant of an enum with fields, see [`Layout::enumeration`].
#[derive(Copy, Clone, Debug)]
pub struct Variant {
  /// Value of the tag; with `ranges`, what tells the variant from the others in the lists of
  /// types (its index, see `deep.rs`).
  pub(crate) tag: u128,
  /// The values of the tag of this variant, sorted, if it is not `tag` alone: several for the
  /// variant of a niche, none for a variant that no value can be (it holds an uninhabited type).
  pub(crate) ranges: Option<&'static [(u128, u128)]>,
  /// The tag is in the fields of this variant: a niche.
  pub(crate) holds_tag: bool,
  /// The enum when it is this variant: a struct of its fields, in which the tag is padding (but
  /// for the variant that holds it).
  pub(crate) layout: &'static Layout,
}

/// The values of the tag of a variant, one range at a time.
#[derive(Copy, Clone)]
pub(crate) struct Tags {
  one: (u128, u128),
  ranges: Option<&'static [(u128, u128)]>,
}

impl Tags {
  /// The values `lo..=hi`.
  pub(crate) const fn range(lo: u128, hi: u128) -> Self {
    Self { one: (lo, hi), ranges: None }
  }

  /// The values of `ranges`, sorted.
  pub(crate) const fn of(ranges: &'static [(u128, u128)]) -> Self {
    Self { one: (0, 0), ranges: Some(ranges) }
  }

  pub(crate) const fn len(&self) -> usize {
    match self.ranges {
      Some(ranges) => ranges.len(),
      None => 1,
    }
  }

  pub(crate) const fn get(&self, index: usize) -> (u128, u128) {
    match self.ranges {
      Some(ranges) => ranges[index],
      None => self.one,
    }
  }

  pub(crate) const fn has(&self, value: u128) -> bool {
    let mut i = 0;
    while i < self.len() {
      let (lo, hi) = self.get(i);
      if lo <= value && value <= hi {
        return true;
      }
      i += 1;
    }
    false
  }

  /// Whatever a value of `self` is one of `other`.
  pub(crate) const fn meets(&self, other: &Tags) -> bool {
    let (mut mine, mut theirs) = (0, 0);
    while mine < self.len() && theirs < other.len() {
      let ((lo, hi), (other_lo, other_hi)) = (self.get(mine), other.get(theirs));
      if lo <= other_hi && other_lo <= hi {
        return true;
      }
      if hi < other_hi {
        mine += 1;
      } else {
        theirs += 1;
      }
    }
    false
  }

  /// Whatever every value of `self` is one of `other`.
  pub(crate) const fn within(&self, other: &Tags) -> bool {
    let mut i = 0;
    while i < self.len() {
      let ((lo, hi), mut j, mut inside) = (self.get(i), 0, false);
      while j < other.len() {
        let (a, b) = other.get(j);
        inside |= a <= lo && hi <= b;
        j += 1;
      }
      if !inside {
        return false;
      }
      i += 1;
    }
    true
  }

  const fn equals(&self, other: &Tags) -> bool {
    if self.len() != other.len() {
      return false;
    }
    let mut index = 0;
    while index < self.len() {
      let (mine, theirs) = (self.get(index), other.get(index));
      if mine.0 != theirs.0 || mine.1 != theirs.1 {
        return false;
      }
      index += 1;
    }
    true
  }
}

impl Variant {
  /// - `tag` is what the tag of the enum is for this variant, as an integer of native endianness,
  /// - `layout` is the [`Layout::variant`] of its fields.
  #[must_use]
  pub const fn new(tag: u128, layout: &'static Layout) -> Self {
    Self { tag, ranges: None, holds_tag: false, layout }
  }

  /// The `index`th variant of an enum that `discover.rs` found: its tag is one of `ranges`
  /// (sorted, disjoint, none if no value can be this variant), and if it `holds_tag`, the tag is
  /// in its fields (a niche).
  #[doc(hidden)]
  #[must_use]
  pub const fn discovered(
    index: usize,
    ranges: &'static [(u128, u128)],
    holds_tag: bool,
    layout: &'static Layout,
  ) -> Self {
    Self { tag: index as u128, ranges: Some(ranges), holds_tag, layout }
  }

  /// The values of the tag of this variant.
  pub(crate) const fn tags(&self) -> Tags {
    Tags { one: (self.tag, self.tag), ranges: self.ranges }
  }

  /// Whatever a value can be this variant: a value of the tag is, and its fields can be.
  pub(crate) const fn is_present(&self) -> bool {
    !matches!(self.ranges, Some(ranges) if ranges.is_empty()) && self.layout.is_inhabited()
  }

  /// Fields, by offset from the start of the enum.
  pub(crate) const fn fields(&self) -> &'static [Field] {
    let Kind::Struct(fields, _) = self.layout.kind else { panic!("not a variant") };
    fields
  }
}

/// A field of a struct, see [`Layout::structure`].
#[derive(Copy, Clone, Debug)]
pub struct Field {
  pub(crate) offset: usize,
  pub(crate) layout: &'static Layout,
  /// Declaration index.
  pub(crate) index: usize,
  /// Number of constrained scalars declared before this field in the struct.
  pub(crate) leaf: usize,
  /// The layout is a scalar that accepts less than the type of the field does.
  pub(crate) restricted: bool,
}

impl Field {
  #[inline]
  #[must_use]
  pub const fn new(offset: usize, layout: &'static Layout) -> Self {
    Self { offset, layout, index: 0, leaf: 0, restricted: false }
  }

  /// A scalar field that only accepts some of the values of its type, see
  /// [`Layout::restricted`].
  ///
  /// # Panics
  /// If `layout` is not a scalar.
  #[inline]
  #[must_use]
  pub const fn restricted(offset: usize, layout: &'static Layout) -> Self {
    assert!(matches!(layout.kind, Kind::Scalar(_)), "only scalar fields can be restricted");
    Self { offset, layout, index: 0, leaf: 0, restricted: true }
  }

  /// The specification of a restricted field.
  #[doc(hidden)]
  #[must_use]
  pub const fn restriction(&self) -> Option<Spec> {
    match self.layout.kind {
      Kind::Scalar(spec) if self.restricted => Some(spec),
      _ => None,
    }
  }

  #[doc(hidden)]
  #[must_use]
  pub const fn offset(&self) -> usize {
    self.offset
  }

  #[doc(hidden)]
  #[must_use]
  pub const fn leaf(&self) -> usize {
    self.leaf
  }
}

/// Memory layout and valid bit patterns of a type.
#[derive(Copy, Clone, Debug)]
pub struct Layout {
  pub(crate) kind: Kind,
  /// Size of the type if it is `Sized`, otherwise the offset of the first element of its tail
  /// slice.
  pub(crate) size: usize,
  pub(crate) align: usize,
  /// Size of an element of the tail slice, `0` if the type is `Sized`.
  pub(crate) tail: usize,
  pub(crate) unsized_: bool,
  /// Number of things that validation may have to check: constrained scalars, patterns, and
  /// structs with rules (array and slice elements are counted once).
  pub(crate) leaves: usize,
  pub(crate) uniform: Uniform,
  /// Whatever a struct of the layout has rules.
  pub(crate) has_rules: bool,
}

/// What [`validate`](crate::validate) has to do for a type.
#[derive(Copy, Clone)]
pub(crate) enum Shape {
  Scalar(Spec),
  Array(usize),
  Slice,
  Struct,
  Pattern(&'static Dfa),
  Enum(&'static Enum),
  Pointer(Pointer),
}

/// Round `value` up to a multiple of `align` (a power of two).
#[inline]
pub(crate) const fn round_up(value: usize, align: usize) -> Option<usize> {
  match value.checked_add(align - 1) {
    Some(v) => Some(v & !(align - 1)),
    None => None,
  }
}

/// Offset of the last field of a `repr(C)` struct.
///
/// - `end` is the end offset of the field that precedes it (`0` if there is none),
/// - `align` is the alignment of its type,
/// - `pack` is the `N` of `repr(packed(N))`, or `usize::MAX`.
#[doc(hidden)]
#[must_use]
pub const fn tail_offset(end: usize, align: usize, pack: usize) -> usize {
  match round_up(end, if align < pack { align } else { pack }) {
    Some(offset) => offset,
    None => panic!("struct is too large"),
  }
}

/// Whatever the type `P`, the offset of a self-relative pointer, has no value whose bytes are all
/// zero: `Option<P>` has the size of `P` (a niche in it), with `None` all zeros.
///
/// # Panics
/// If `Option<P>` has the size of `P` with a `None` that is not zeros: a niche that is not zero,
/// which tells of values of the offset that are no pointer, and which is not supported.
const fn excludes_zero<P>() -> bool {
  if size_of::<Option<P>>() != size_of::<P>() {
    return false;
  }
  let none = mem::MaybeUninit::new(None::<P>);
  let mut i = 0;
  while i < size_of::<P>() {
    // SAFETY: in bounds. A byte of `None` that is not initialized fails the build (R2 of
    // `discover.rs`).
    let byte = unsafe { *none.as_ptr().cast::<u8>().add(i) };
    assert!(
      byte == 0,
      "the offset of a self-relative pointer excludes values other than zero (a niche that is not \
       zero): not supported, nothing tells which"
    );
    i += 1;
  }
  true
}

/// The tag of an enum that no value can be.
const NO_TAG: Layout = Layout::scalar::<()>(Spec::any::<()>());

/// What [`Layout::PIN`] is made of.
fn pin<T>(_: T) {}

impl Layout {
  /// The types that the code that `#[derive(Pun)]` generates names by their path in the crate
  /// it is told of (`#[pun(crate = ..)]`), as they must be: the functions that it calls are
  /// associated functions of these types, or of `Layout` and `Spec`, which the traits that it
  /// implements name. A path that leads to other types, with functions of the same name, does
  /// not compile (`fn` types are not paths, nothing can stand for them).
  #[doc(hidden)]
  #[expect(
    clippy::type_complexity,
    reason = "the derive writes this tuple of `fn` types out, with the paths it is told"
  )]
  pub const PIN: (fn(Rule), fn(Op), fn(Field), fn(Enum), fn(Variant)) =
    (pin::<Rule>, pin::<Op>, pin::<Field>, pin::<Enum>, pin::<Variant>);

  /// An uninitialized `T`, whose fields `#[derive(Pun)]` finds the offsets of.
  #[doc(hidden)]
  #[must_use]
  pub const fn uninit<T>() -> mem::MaybeUninit<T> {
    mem::MaybeUninit::uninit()
  }

  /// The layout of the enum `E` without `repr`, found by experiments on a sample of each of its
  /// `V` variants (anything for a variant that has no value) and its `fields`, observed on the
  /// sample: see `discover.rs`. For `#[derive(Pun)]`.
  #[doc(hidden)]
  #[must_use]
  pub const fn discover<E, const V: usize>(
    samples: &[mem::MaybeUninit<E>; V],
    fields: &[&[Field]; V],
  ) -> Desc<V> {
    crate::discover::discover(samples, fields)
  }

  /// A valid `T`, made of nothing but its layout (every scalar the least of its values), for the
  /// samples of the variants of an enum without `repr`.
  #[doc(hidden)]
  #[must_use]
  pub const fn sample<T: crate::Pun>() -> mem::MaybeUninit<T> {
    crate::discover::sample::<T>()
  }

  /// `value`, as a sample (see [`Layout::discover`]).
  #[doc(hidden)]
  #[must_use]
  pub const fn wrap<T>(value: T) -> mem::MaybeUninit<T> {
    mem::MaybeUninit::new(value)
  }

  /// Whatever a value with fields of these layouts can be made.
  #[doc(hidden)]
  #[must_use]
  pub const fn inhabited_all(fields: &[&'static Layout]) -> bool {
    let mut i = 0;
    while i < fields.len() {
      if !fields[i].is_inhabited() {
        return false;
      }
      i += 1;
    }
    true
  }

  /// The layout of a `repr(C)` struct whose fields have the given sizes and alignments, after
  /// what `start` (an end offset and an alignment) says is before them: the offsets of the
  /// fields, and the size and alignment of the struct. For the variants of enums with fields,
  /// whose layout RFC 2195 specifies in these terms.
  #[doc(hidden)]
  #[must_use]
  pub const fn repr_c<const N: usize>(
    start: (usize, usize),
    fields: [(usize, usize); N],
  ) -> ([usize; N], (usize, usize)) {
    let (mut offsets, (mut end, mut align), mut i) = ([0; N], start, 0);
    while i < N {
      offsets[i] = tail_offset(end, fields[i].1, usize::MAX);
      end = offsets[i] + fields[i].0;
      if fields[i].1 > align {
        align = fields[i].1;
      }
      i += 1;
    }
    (offsets, (tail_offset(end, align, usize::MAX), align))
  }

  /// Size and alignment of `T`, for `#[derive(Pun)]`.
  #[doc(hidden)]
  #[must_use]
  pub const fn size_align<T>() -> (usize, usize) {
    (size_of::<T>(), align_of::<T>())
  }

  /// [`tail_offset`] in a struct that is not packed, for `#[derive(Pun)]`.
  #[doc(hidden)]
  #[must_use]
  pub const fn tail_offset(end: usize, align: usize) -> usize {
    tail_offset(end, align, usize::MAX)
  }

  /// [`tail_offset`] in a `repr(packed(pack))` struct, for `#[derive(Pun)]`.
  #[doc(hidden)]
  #[must_use]
  pub const fn tail_offset_packed(end: usize, align: usize, pack: usize) -> usize {
    tail_offset(end, align, pack)
  }

  /// Layout of a scalar.
  ///
  /// # Panics
  /// If `spec` does not have the size of `T`.
  #[must_use]
  pub const fn scalar<T>(spec: Spec) -> Self {
    assert!(spec.size == size_of::<T>(), "`Spec` size mismatch");
    Self {
      kind: Kind::Scalar(spec),
      size: spec.size,
      align: align_of::<T>(),
      tail: 0,
      unsized_: false,
      leaves: spec.is_constrained() as usize,
      uniform: match spec.valid {
        Valid::Uninit => Uniform::Pad,
        Valid::Any => Uniform::Any,
        Valid::In(_) => Uniform::No,
      },
      has_rules: false,
    }
  }

  /// Layout of the tag of an enum that `discover.rs` found: a scalar of no type of its own, so of
  /// no alignment.
  pub(crate) const fn tag(spec: Spec) -> Self {
    Self {
      kind: Kind::Scalar(spec),
      size: spec.size,
      align: 1,
      tail: 0,
      unsized_: false,
      leaves: spec.is_constrained() as usize,
      uniform: match spec.valid {
        Valid::Uninit => Uniform::Pad,
        Valid::Any => Uniform::Any,
        Valid::In(_) => Uniform::No,
      },
      has_rules: false,
    }
  }

  /// Same scalar, that only accepts the values of `ranges` (see [`Spec::ranges`]), which must
  /// all be valid for the scalar.
  ///
  /// # Panics
  /// If `self` is not an initialized scalar, or if `ranges` are not valid values of it (see
  /// [`Spec::with_ranges`]).
  #[must_use]
  pub const fn restricted(&self, ranges: &'static [(u128, u128)]) -> Self {
    let Kind::Scalar(spec) = self.kind else { panic!("only scalars can be restricted") };
    assert!(!matches!(spec.valid, Valid::Uninit), "only initialized scalars can be restricted");
    // The restriction is not something that the type of the field enforces.
    let new = Spec { known: spec.known, ..spec.with_ranges(ranges) };
    let mut i = 0;
    while i < ranges.len() {
      assert!(
        spec.contains(ranges[i].0) && spec.contains(ranges[i].1),
        "restricted to values that are not valid for the type"
      );
      if let Valid::In(old) = spec.valid {
        assert!(
          matches!(crate::analysis::relate_ranges(ranges, old), crate::analysis::Rel::Subset),
          "restricted to values that are not valid for the type"
        );
      }
      i += 1;
    }
    Self {
      kind: Kind::Scalar(new),
      leaves: new.is_constrained() as usize,
      uniform: if new.is_constrained() { Uniform::No } else { self.uniform },
      ..*self
    }
  }

  /// Layout of an unsized type made of bytes that `dfa` accepts, such as `str`.
  ///
  /// # Safety
  /// `probe` must be a pointer to a type whose pointers have a slice length, which is
  /// `PROBE_LEN` (see `Pun::PROBE`).
  ///
  /// # Panics
  /// If `probe` is not a pointer to bytes, or if `dfa` is not well formed.
  pub const unsafe fn pattern<T: ?Sized>(probe: *const T, dfa: &'static Dfa) -> Self {
    // SAFETY: the caller gives the probe of a type whose pointers have a slice length.
    unsafe { ptr::verify::<T>(probe) };
    // SAFETY: the metadata of `probe` is a small slice length.
    assert!(unsafe { mem::size_of_val_raw(probe) } == ptr::PROBE_LEN, "not a byte string");
    dfa.check();
    Self {
      kind: Kind::Pattern(dfa),
      size: 0,
      align: 1,
      tail: 1,
      unsized_: true,
      leaves: 1,
      uniform: Uniform::No,
      has_rules: false,
    }
  }

  /// Layout of a type the size and alignment of `T` whose bytes are all padding.
  #[must_use]
  pub const fn padding<T>() -> Self {
    Self {
      kind: Kind::Struct(&[], &[]),
      size: size_of::<T>(),
      align: align_of::<T>(),
      tail: 0,
      unsized_: false,
      leaves: 0,
      uniform: Uniform::Pad,
      has_rules: false,
    }
  }

  /// Layout of the self-relative pointer `P`, which is the offset of `pointer` and nothing else,
  /// to a `T` (an element of a sequence, `()` for text), for [`self_relative!`](crate::self_relative).
  ///
  /// # Panics
  /// If `P` does not have the size of the offset, or if `pointer` was not made for a `T`.
  #[doc(hidden)]
  #[must_use]
  pub const fn pointer<P, T>(pointer: Pointer) -> Self {
    assert!(size_of::<P>() == pointer.width, "a self-relative pointer is its offset");
    // Whatever the type has no value of zeros: `Option` of it has its size, `None` being zeros (a
    // `NonZero`). A niche elsewhere would tell of values of the offset that are no pointer, which
    // nothing here can name: refused.
    let nonzero = excludes_zero::<P>();
    assert!(
      !(nonzero && pointer.nullable),
      "a self-relative pointer that may be null has a value of zeros: its offset is no `NonZero`"
    );
    let pointer = Pointer { nonzero, ..pointer };
    assert!(
      size_of::<T>() == pointer.size && align_of::<T>() == pointer.align,
      "the `POINTER` of a `SelfRelative` type is made for its `Target`"
    );
    Self {
      kind: Kind::Pointer(pointer),
      size: pointer.width,
      align: align_of::<P>(),
      tail: 0,
      unsized_: false,
      // A leaf: what validation may have to check of it is that it is not null.
      leaves: 1,
      uniform: Uniform::No,
      has_rules: false,
    }
  }

  /// The layout of `Option<P>`, where `P` is a self-relative pointer whose `POINTER` is `pointer`
  /// and which aims at `T`: the same pointer, that may be null. [`self_relative!`](crate::self_relative)
  /// gives it as `Pun::OPTION`, by which `Option<P>` is that pointer.
  ///
  /// `Option<P>` is that when it has the size of `P` and `None` is all zero bytes, which is
  /// checked here. Then `rustc` puts `None` in a niche of the bytes of `P` (all of them are of
  /// its offset, initialized: S1 of [`SelfRelative`](crate::SelfRelative)), which is zeros, and
  /// leaves the bytes of `Some(p)` the ones of `p` (R1 of `discover.rs`), which are not all zero:
  /// a value of all zero bytes aims at nothing, any other is a pointer as `P` is (S5).
  ///
  /// # Panics
  /// If `pointer` may be null already, if `Option<P>` is larger than `P` (an offset without a
  /// niche at zero, such as a byte array, has a tag of its own in `Option`; a `NonZero` has
  /// one), or if `None` is not zeros.
  #[must_use]
  pub const fn nullable<P, T>(pointer: Pointer) -> Self {
    assert!(!pointer.is_nullable(), "`Option` of a pointer that may be null is not a pointer");
    assert!(
      size_of::<Option<P>>() == size_of::<P>(),
      "`Option` of this self-relative pointer is larger than it: its offset has no niche at zero \
       (a `NonZero` has one), so `None` is not the pointer of zeros"
    );
    let none = mem::MaybeUninit::new(None::<P>);
    let mut i = 0;
    while i < size_of::<P>() {
      // SAFETY: in bounds. A byte of `None` that is not initialized fails the build (R2).
      let byte = unsafe { *none.as_ptr().cast::<u8>().add(i) };
      assert!(byte == 0, "`None` of this self-relative pointer is not zeros");
      i += 1;
    }
    Self::pointer::<Option<P>, T>(pointer.or_null())
  }

  /// The pointer that a value of the layout is, if it is a self-relative pointer (see
  /// [`Pointer`]); `None` for anything else, a struct that holds one included.
  #[must_use]
  pub const fn as_pointer(&self) -> Option<Pointer> {
    match self.kind {
      Kind::Pointer(pointer) => Some(pointer),
      _ => None,
    }
  }

  /// Whatever validation has nothing to check of a value of the layout: every bit pattern of its
  /// bytes (initialized) is a value, it holds no pointer, no enum and no rule. A value of it is
  /// made of any initialized bytes; [`validate_in`](crate::validate_in) reads nothing of it.
  #[must_use]
  pub const fn is_free(&self) -> bool {
    self.leaves == 0
  }

  /// Whatever a value of the layout is all its bytes: it holds no padding (no byte that may be
  /// uninitialized), and no pointer. Such a value can be copied as bytes, and its bytes are
  /// initialized. An enum with fields is never one (which bytes are padding depends on its tag),
  /// nor a slice or a pattern (unsized).
  #[must_use]
  pub const fn is_plain(&self) -> bool {
    match self.kind {
      Kind::Scalar(spec) => !spec.may_be_uninit(),
      Kind::Array(elem, _) => elem.is_plain(),
      Kind::Struct(fields, _) => {
        let (mut i, mut covered) = (0, 0);
        while i < fields.len() {
          // Sorted by offset: a field after the bytes covered so far leaves a hole.
          if fields[i].offset != covered || !fields[i].layout.is_plain() {
            return false;
          }
          covered += fields[i].layout.size;
          i += 1;
        }
        !self.unsized_ && covered == self.size
      }
      Kind::Slice(_) | Kind::Pattern(_) | Kind::Enum(_) | Kind::Pointer(_) => false,
    }
  }

  /// Whatever values of the layout hold a self-relative pointer (see [`Pointer`]), not counting
  /// what the pointers aim at.
  #[must_use]
  pub const fn has_pointers(&self) -> bool {
    // A pointer is a leaf.
    if self.leaves == 0 {
      return false;
    }
    match self.kind {
      Kind::Pointer(_) => true,
      Kind::Scalar(_) | Kind::Pattern(_) => false,
      Kind::Array(elem, _) | Kind::Slice(elem) => elem.has_pointers(),
      Kind::Struct(fields, _) => {
        let mut i = 0;
        while i < fields.len() {
          if fields[i].layout.has_pointers() {
            return true;
          }
          i += 1;
        }
        false
      }
      Kind::Enum(layout) => {
        let mut i = 0;
        while i < layout.variants.len() {
          if layout.variants[i].layout.has_pointers() {
            return true;
          }
          i += 1;
        }
        false
      }
    }
  }

  /// Layout of `[T; len]`.
  ///
  /// # Panics
  /// If `elem` is unsized, or if the array is larger than `usize::MAX` bytes.
  #[must_use]
  pub const fn array(elem: &'static Layout, len: usize) -> Self {
    assert!(!elem.unsized_);
    Self {
      kind: Kind::Array(elem, len),
      size: match elem.size.checked_mul(len) {
        Some(size) => size,
        None => panic!("array is too large"),
      },
      align: elem.align,
      tail: 0,
      unsized_: false,
      leaves: elem.leaves,
      uniform: elem.uniform,
      has_rules: elem.has_rules,
    }
  }

  /// Layout of `[T]`.
  ///
  /// # Safety
  /// The length of `probe` must be `PROBE_LEN` (see `Pun::PROBE`).
  ///
  /// # Panics
  /// If `elem` is unsized or does not have the size of `T`.
  pub const unsafe fn slice<T>(probe: *const [T], elem: &'static Layout) -> Self {
    assert!(!elem.unsized_ && elem.size == size_of::<T>());
    // SAFETY: `[T]` is a slice.
    unsafe { ptr::verify::<[T]>(probe) };
    Self {
      kind: Kind::Slice(elem),
      size: 0,
      align: elem.align,
      tail: elem.size,
      unsized_: true,
      leaves: elem.leaves,
      uniform: elem.uniform,
      has_rules: elem.has_rules,
    }
  }

  /// Layout of the struct `T`.
  ///
  /// - `probe` is `T::PROBE`,
  /// - `fields` are the fields of `T` as returned by [`Layout::sort_fields`],
  /// - `rules` are what values of `T` must satisfy beyond the validity of their fields.
  ///
  /// The size and alignment of `T` come from the compiler. If the last field is unsized, so is
  /// `T`, and the way the compiler sizes `T` is checked against the model used by this crate.
  ///
  /// # Safety
  /// If `T` is unsized, its tail must be a slice, and the slice length of `probe` must be
  /// `PROBE_LEN` (see `Pun::PROBE`).
  ///
  /// # Panics
  /// If the fields overlap, are not sorted or are out of the bounds of `T`, if a rule is
  /// malformed, or if `T` is not laid out as this crate models it.
  pub const unsafe fn structure<T: ?Sized>(
    probe: *const T,
    fields: &'static [Field],
    rules: &'static [Rule],
  ) -> Self {
    let (mut i, mut end, mut leaves) = (0, 0, (!rules.is_empty()) as usize);
    let mut has_rules = !rules.is_empty();
    let (mut all_any, mut all_pad) = (true, true);
    let mut tail: Option<&Field> = None;
    while i < fields.len() {
      let field = &fields[i];
      assert!(tail.is_none(), "only the last field of a struct can be unsized");
      leaves += field.layout.leaves;
      has_rules |= field.layout.has_rules;
      if field.layout.unsized_ {
        assert!(field.offset >= end, "fields must not overlap");
        tail = Some(field);
      } else if field.layout.size > 0 {
        assert!(field.offset >= end, "fields must be sorted and must not overlap");
        all_any &= field.offset == end && matches!(field.layout.uniform, Uniform::Any);
        all_pad &= matches!(field.layout.uniform, Uniform::Pad);
        end = field.offset + field.layout.size;
      }
      i += 1;
    }
    match tail {
      None => {
        // SAFETY: `T` is `Sized` (none of its fields is unsized): `probe` is not even looked at.
        let (size, align) = unsafe { (mem::size_of_val_raw(probe), mem::align_of_val_raw(probe)) };
        assert!(end <= size, "field out of bounds");
        let uniform = match (all_any && end == size, all_pad) {
          _ if size == 0 => Uniform::Any,
          (true, _) => Uniform::Any,
          (_, true) => Uniform::Pad,
          _ => Uniform::No,
        };
        check_rules(rules, end);
        let this = Self {
          kind: Kind::Struct(fields, rules),
          size,
          align,
          tail: 0,
          unsized_: false,
          leaves,
          uniform,
          has_rules,
        };
        let mut i = 0;
        while i < rules.len() {
          assert!(
            !matches!(rules[i], Rule::Len(_)) || this.tail_shape().array,
            "`len` is about a last field that is a slice, or an array"
          );
          i += 1;
        }
        this
      }
      Some(field) => {
        // SAFETY: the tail of `T` is a slice (the tail of its last field is).
        unsafe { ptr::verify::<T>(probe) };
        // SAFETY: the metadata of `probe` is a small slice length.
        let align = unsafe { mem::align_of_val_raw(probe) };
        check_rules(rules, end);
        let this = Self {
          kind: Kind::Struct(fields, rules),
          size: field.offset + field.layout.size,
          align,
          tail: field.layout.tail,
          unsized_: true,
          leaves,
          uniform: Uniform::No,
          has_rules,
        };
        // Check the size model against the compiler.
        let mut len = 0;
        while len <= 8 {
          // SAFETY: `T` has a slice tail of `len` elements, and its size is tiny.
          let size = unsafe { mem::size_of_val_raw(ptr::raw_with_len(probe, len)) };
          assert!(
            matches!(this.size_of_val(len), Some(s) if s == size),
            "unsupported layout: the trailing padding of this unsized struct is not the one of \
             its unsized field (packed struct with an aligned unsized field?)",
          );
          len += 1;
        }
        this
      }
    }
  }

  /// Layout of a variant of the enum `T`: its `fields`, as returned by [`Layout::sort_fields`],
  /// with their offsets from the start of the enum.
  #[must_use]
  pub const fn variant<T>(fields: &'static [Field]) -> Self {
    let (mut i, mut leaves, mut has_rules) = (0, 0, false);
    while i < fields.len() {
      leaves += fields[i].layout.leaves;
      has_rules |= fields[i].layout.has_rules;
      i += 1;
    }
    Self {
      kind: Kind::Struct(fields, &[]),
      size: size_of::<T>(),
      align: align_of::<T>(),
      tail: 0,
      unsized_: false,
      leaves,
      uniform: Uniform::No,
      has_rules,
    }
  }

  /// Layout of the enum with fields `T`, whose representation must be one that Rust specifies
  /// (`repr(C)`, `repr(u8)`...): a tag at `tag.offset`, then the fields of a variant.
  ///
  /// What is known of such an enum is its tag, and which of its bytes are initialized; what it
  /// requires is checked as a whole, at run time, unless the source is an enum whose variants
  /// each prove the variant of the same tag.
  ///
  /// # Panics
  /// If `T` is not laid out as its representation specifies, or if the variants do not fit it:
  /// a tag that is not a value of the tag or that two variants share, a field out of bounds.
  #[must_use]
  pub const fn enumeration<T>(layout: &'static Enum) -> Self {
    let (size, tag) = (size_of::<T>(), &layout.tag);
    let Kind::Scalar(spec) = tag.layout.kind else { panic!("the tag of an enum is a scalar") };
    // An `align(N)` on the enum is the only thing that the shadows do not tell.
    assert!(
      align_of::<T>() >= layout.specified.1
        && size == tail_offset(layout.specified.0, align_of::<T>(), usize::MAX),
      "the enum is not laid out as its representation specifies"
    );
    assert!(spec.is_integer() && tag.offset + spec.size <= size, "tag out of bounds");
    let (mut i, mut holders) = (0, 0);
    while i < layout.variants.len() {
      let variant = &layout.variants[i];
      let tags = variant.tags();
      let mut j = 0;
      while j < tags.len() {
        let (lo, hi) = tags.get(j);
        assert!(lo <= hi && spec.contains(lo) && spec.contains(hi), "not a value of the tag");
        assert!(j == 0 || tags.get(j - 1).1 < lo, "the tags of a variant are not sorted");
        j += 1;
      }
      holders += variant.holds_tag as usize;
      let (fields, mut j, mut end) = (variant.fields(), 0, 0);
      assert!(variant.layout.size == size, "not a variant of this enum");
      while j < fields.len() {
        let field = &fields[j];
        assert!(!field.layout.unsized_, "the fields of an enum are sized");
        if field.layout.size > 0 {
          let field_end = field.offset + field.layout.size;
          assert!(field.offset >= end && field_end <= size, "fields overlap or are out of bounds");
          // Only the variant of a niche holds the tag.
          assert!(
            variant.holds_tag || field_end <= tag.offset || field.offset >= tag.offset + spec.size,
            "a field overlaps the tag"
          );
          end = field_end;
        }
        j += 1;
      }
      let mut j = 0;
      while j < i {
        assert!(!layout.variants[j].tags().meets(&tags), "two variants have the same tag");
        j += 1;
      }
      i += 1;
    }
    assert!(holders <= 1, "the tag is in the fields of two variants");
    Self {
      kind: Kind::Enum(layout),
      size,
      align: align_of::<T>(),
      tail: 0,
      unsized_: false,
      leaves: 1,
      uniform: Uniform::No,
      // What the fields of the variants require is a matter of the enum as a whole.
      has_rules: false,
    }
  }

  /// The expression of the `rule`th rule, if it has one (else none).
  pub(crate) const fn rule_ops(&self, rule: usize) -> &'static [Op] {
    let rules = self.rules();
    if rule >= rules.len() {
      return &[];
    }
    match rules[rule].ops() {
      Some(ops) => ops,
      None => &[],
    }
  }

  /// The `index`th rule, if any.
  pub(crate) const fn rule(&self, index: usize) -> Option<Rule> {
    let rules = self.rules();
    if index < rules.len() { Some(rules[index]) } else { None }
  }

  /// The tag of the `variant`th variant of an enum.
  #[doc(hidden)]
  #[must_use]
  pub const fn variant_tag(&self, variant: usize) -> u128 {
    let Kind::Enum(layout) = self.kind else { panic!("not an enum") };
    layout.variants[variant].tag
  }

  /// The values of the tag of the `variant`th variant of an enum, if they are not
  /// [`Layout::variant_tag`] alone.
  #[doc(hidden)]
  #[must_use]
  pub const fn variant_ranges(&self, variant: usize) -> Option<&'static [(u128, u128)]> {
    let Kind::Enum(layout) = self.kind else { panic!("not an enum") };
    layout.variants[variant].ranges
  }

  /// Layout of a type that has no value, such as `Infallible` or an enum without variants.
  #[must_use]
  pub const fn uninhabited<T>() -> Self {
    Self {
      kind: Kind::Enum(&Enum::NEVER),
      size: size_of::<T>(),
      align: align_of::<T>(),
      tail: 0,
      unsized_: false,
      leaves: 1,
      uniform: Uniform::No,
      has_rules: false,
    }
  }

  /// Whatever a value of the layout can exist: one of a type without values is not. (A slice
  /// can be empty, an array of no element too.)
  #[must_use]
  pub const fn is_inhabited(&self) -> bool {
    match self.kind {
      Kind::Scalar(_) | Kind::Pattern(_) | Kind::Pointer(_) | Kind::Slice(_) => true,
      Kind::Array(elem, len) => len == 0 || elem.is_inhabited(),
      Kind::Struct(fields, _) => {
        let mut i = 0;
        while i < fields.len() {
          if !fields[i].layout.unsized_ && !fields[i].layout.is_inhabited() {
            return false;
          }
          i += 1;
        }
        true
      }
      Kind::Enum(layout) => {
        let mut i = 0;
        while i < layout.variants.len() {
          if layout.variants[i].is_present() {
            return true;
          }
          i += 1;
        }
        false
      }
    }
  }

  /// Layout of the `repr(C)` union `T`, whose fields are the variants of `layout` (see
  /// [`Enum::union`]).
  ///
  /// The validity of unions is not settled by the language. What is taken here is what every rule
  /// that is considered allows: as a target, bytes are a union if they are one of its fields
  /// (where no rule requires less, this requires more); as a source, nothing is known of them, not
  /// even that they are initialized (a field may be written over another, and some rules let a
  /// union hold any bytes).
  ///
  /// # Panics
  /// If a field is not at offset 0 or out of the union, or holds a self-relative pointer.
  #[must_use]
  pub const fn union<T>(layout: &'static Enum) -> Self {
    assert!(layout.union, "not a union");
    let (size, mut i) = (size_of::<T>(), 0);
    while i < layout.variants.len() {
      let variant = &layout.variants[i];
      let fields = variant.fields();
      assert!(variant.layout.size == size, "not a field of this union");
      assert!(fields.len() <= 1, "a field of a union is one field");
      if fields.len() == 1 {
        let field = &fields[0];
        assert!(field.offset == 0 && field.layout.size <= size, "the fields of a union are at 0");
        assert!(!field.layout.unsized_, "the fields of a union are sized");
      }
      assert!(!variant.layout.has_pointers(), "a union holds no self-relative pointer");
      i += 1;
    }
    Self {
      kind: Kind::Enum(layout),
      size,
      align: align_of::<T>(),
      tail: 0,
      unsized_: false,
      leaves: 1,
      uniform: Uniform::No,
      has_rules: false,
    }
  }

  /// The `index`th field, as declared, of the `variant`th variant of an enum.
  #[doc(hidden)]
  #[must_use]
  pub const fn variant_field(&self, variant: usize, index: usize) -> &Field {
    let Kind::Enum(layout) = self.kind else { panic!("not an enum") };
    let (fields, mut i) = (layout.variants[variant].fields(), 0);
    while i < fields.len() {
      if fields[i].index == index {
        return &fields[i];
      }
      i += 1;
    }
    panic!("no such field")
  }

  /// Whatever the bytes from `offset` are initialized, and where bytes that may be otherwise
  /// start: in every value of the layout (`always`), or in some.
  ///
  /// This does not look into arrays whose elements are not all one or the other: their bytes
  /// are not known to be initialized (`always`), or are all required to be (not `always`).
  ///
  /// With `checked` (and not `always`), the padding of a struct whose rules read the bytes of its
  /// value is required to be initialized, as these rules may be checked.
  pub(crate) const fn segment(&self, offset: usize, always: bool, checked: bool) -> (bool, usize) {
    if checked && !always && self.rules_read_bytes() {
      return (true, self.size);
    }
    match self.uniform {
      Uniform::Any => return (true, self.size),
      Uniform::Pad => return (false, self.size),
      Uniform::No => {}
    }
    match self.kind {
      // What a value holds (`always`) is what is known of it; what is required of one, what is
      // valid.
      Kind::Scalar(spec) => {
        (!matches!(if always { spec.known } else { spec.valid }, Valid::Uninit), self.size)
      }
      // The bytes of a pointer are initialized.
      Kind::Pointer(_) => (true, self.size),
      Kind::Pattern(_) | Kind::Slice(_) => panic!("not sized"),
      Kind::Array(..) => (!always, self.size),
      Kind::Struct(fields, _) => {
        let mut i = 0;
        while i < fields.len() {
          let field = &fields[i];
          if offset < field.offset {
            // Padding before the field.
            return (false, field.offset);
          }
          if offset < field.offset + field.layout.size {
            let (init, end) = field.layout.segment(offset - field.offset, always, checked);
            return (init, field.offset + end);
          }
          i += 1;
        }
        (false, self.size)
      }
      // Nothing is known of what a union holds; a target may need what any field needs.
      Kind::Enum(layout) if layout.union && always => (false, self.size),
      Kind::Enum(layout) => {
        let (start, end) = (layout.tag.offset, layout.tag.offset + layout.tag.layout.size);
        if offset >= start && offset < end {
          return (true, end);
        }
        // The same in every variant up to `stop`.
        let (mut i, mut every, mut some) = (0, true, false);
        let mut stop = if offset < start { start } else { self.size };
        while i < layout.variants.len() {
          // A variant that no value can be has nothing to tell.
          if !layout.variants[i].is_present() {
            i += 1;
            continue;
          }
          let (init, end) = layout.variants[i].layout.segment(offset, always, checked);
          (every, some) = (every && init, some || init);
          if end < stop {
            stop = end;
          }
          i += 1;
        }
        (if always { every } else { some }, stop)
      }
    }
  }

  /// Number the fields of a struct (given in declaration order) and sort them by offset.
  #[must_use]
  pub const fn sort_fields<const N: usize>(mut fields: [Field; N]) -> [Field; N] {
    let (mut i, mut leaf) = (0, 0);
    while i < N {
      (fields[i].index, fields[i].leaf) = (i, leaf);
      leaf += fields[i].layout.leaves;
      i += 1;
    }
    // Stable insertion sort.
    let mut i = 1;
    while i < N {
      let mut j = i;
      while j > 0 && fields[j - 1].offset > fields[j].offset {
        fields.swap(j - 1, j);
        j -= 1;
      }
      i += 1;
    }
    fields
  }

  /// Size and alignment of the type.
  #[inline]
  #[must_use]
  pub const fn align(&self) -> usize {
    self.align
  }

  /// Size of the type if it is `Sized`, otherwise the offset of its tail slice.
  #[inline]
  #[must_use]
  pub const fn size(&self) -> usize {
    self.size
  }

  #[inline]
  pub(crate) const fn is_unsized(&self) -> bool {
    self.unsized_
  }

  /// Whatever the size of values of the type depends on a slice length.
  #[inline]
  pub(crate) const fn is_dynamic(&self) -> bool {
    self.unsized_ && self.tail > 0
  }

  /// Size of an element of the tail slice if the type [is dynamic](Self::is_dynamic), else `0`.
  #[inline]
  pub(crate) const fn stride(&self) -> usize {
    if self.unsized_ { self.tail } else { 0 }
  }

  /// Size of a value whose tail slice has `len` elements, trailing padding included.
  #[inline]
  pub(crate) const fn size_of_val(&self, len: usize) -> Option<usize> {
    if !self.unsized_ {
      return Some(self.size);
    }
    match self.tail.checked_mul(len) {
      Some(tail) => match self.size.checked_add(tail) {
        Some(extent) => round_up(extent, self.align),
        None => None,
      },
      None => None,
    }
  }

  /// Offset of the field declared at `index`.
  #[doc(hidden)]
  #[must_use]
  pub const fn field(&self, index: usize) -> &Field {
    let Kind::Struct(fields, _) = self.kind else { panic!("not a struct") };
    let mut i = 0;
    while i < fields.len() {
      if fields[i].index == index {
        return &fields[i];
      }
      i += 1;
    }
    panic!("no such field")
  }

  #[doc(hidden)]
  #[must_use]
  pub const fn field_offset(&self, index: usize) -> usize {
    self.field(index).offset
  }

  #[doc(hidden)]
  #[must_use]
  pub const fn field_leaf(&self, index: usize) -> usize {
    self.field(index).leaf
  }

  #[inline]
  pub(crate) const fn shape(&self) -> Shape {
    match self.kind {
      Kind::Scalar(spec) => Shape::Scalar(spec),
      Kind::Array(_, len) => Shape::Array(len),
      Kind::Slice(_) => Shape::Slice,
      Kind::Struct(..) => Shape::Struct,
      Kind::Pattern(dfa) => Shape::Pattern(dfa),
      Kind::Enum(layout) => Shape::Enum(layout),
      Kind::Pointer(pointer) => Shape::Pointer(pointer),
    }
  }

  /// Whatever a rule of the struct reads bytes of its value other than its fields (`le`, `be`,
  /// `crc32`, `sum16`): its padding, if it has any, is then read when the rule is checked.
  pub(crate) const fn rules_read_bytes(&self) -> bool {
    let (rules, mut i) = (self.rules(), 0);
    while i < rules.len() {
      if let Some(ops) = rules[i].ops()
        && ops_read_bytes(ops)
      {
        return true;
      }
      i += 1;
    }
    false
  }

  /// The first scalar of the sized part of a value of this layout that only some values are
  /// valid for (the EtherType of a frame, the version of a header), by offset: its offset and
  /// what it requires. Whatever bytes a value of this layout is made from, it is checked there.
  pub(crate) const fn key(&self) -> Option<(usize, Spec)> {
    match self.kind {
      Kind::Scalar(spec) if spec.is_constrained() => Some((0, spec)),
      Kind::Struct(fields, _) => {
        let (mut best, mut i): (Option<(usize, Spec)>, usize) = (None, 0);
        while i < fields.len() {
          if let Some((offset, spec)) = fields[i].layout.key() {
            let offset = fields[i].offset + offset;
            if !matches!(best, Some((first, _)) if first <= offset) {
              best = Some((offset, spec));
            }
          }
          i += 1;
        }
        best
      }
      Kind::Array(elem, len) if len > 0 => elem.key(),
      _ => None,
    }
  }

  /// How many elements of this layout the validation of a slice checks at a time, without
  /// stopping between them (see `validate::validate_elems`): about 64 bytes of them, if checking
  /// one is a few comparisons, else 1.
  pub(crate) const fn block(&self) -> usize {
    if self.size == 0 || self.size > 16 || self.unsized_ || !self.is_cheap() {
      return 1;
    }
    64 / self.size
  }

  /// Whatever checking a value of this layout is a few comparisons: scalars, and arrays and
  /// structs of them, whose rules only compute on their fields (no loop, no division).
  const fn is_cheap(&self) -> bool {
    match self.kind {
      Kind::Scalar(_) => true,
      Kind::Array(elem, len) => len <= 16 && elem.is_cheap(),
      Kind::Struct(fields, rules) => {
        let mut i = 0;
        while i < fields.len() {
          if !fields[i].layout.is_cheap() {
            return false;
          }
          i += 1;
        }
        let mut i = 0;
        while i < rules.len() {
          if let Some(ops) = rules[i].ops()
            && !cheap_ops(ops)
          {
            return false;
          }
          i += 1;
        }
        true
      }
      Kind::Slice(_) | Kind::Pattern(_) | Kind::Enum(_) | Kind::Pointer(_) => false,
    }
  }

  /// Rules of a struct.
  #[inline]
  pub(crate) const fn rules(&self) -> &'static [Rule] {
    match self.kind {
      Kind::Struct(_, rules) => rules,
      _ => &[],
    }
  }

  /// Fields of a struct, by offset (none if it is not one).
  pub(crate) const fn struct_fields(&self) -> &'static [Field] {
    match self.kind {
      Kind::Struct(fields, _) => fields,
      _ => &[],
    }
  }

  /// Whatever the last rule of a struct is a region without an expression, which takes what
  /// remains of the tail.
  pub(crate) const fn has_last_region(&self) -> bool {
    matches!(self.rules().last(), Some(Rule::Region(None)))
  }

  /// The rule that tells how long a value is from its first bytes, if any: the first `Size` or
  /// `Len` rule of the type, or else of its last field, and so on.
  pub(crate) const fn length_rule(&self) -> Option<LengthRule> {
    let (mut layout, mut base) = (self, 0);
    loop {
      let Kind::Struct(fields, rules) = layout.kind else { return None };
      let mut i = 0;
      while i < rules.len() {
        match rules[i] {
          Rule::Size(ops) => return Some(LengthRule { base, elements: false, ops }),
          Rule::Len(ops) => return Some(LengthRule { base, elements: true, ops }),
          _ => {}
        }
        i += 1;
      }
      if fields.is_empty() || !layout.unsized_ {
        return None;
      }
      let last = &fields[fields.len() - 1];
      (layout, base) = (last.layout, base + last.offset);
    }
  }

  /// The specification of the unsigned integer that the type is, if it is one: an initialized
  /// scalar of 1 to 16 bytes whose bits are its value, or a struct whose only field (but for the
  /// ones without size) is one.
  #[doc(hidden)]
  #[must_use]
  pub const fn as_integer(&self) -> Option<Spec> {
    match self.kind {
      Kind::Scalar(spec) if spec.is_integer() && spec.number && spec.size >= 1 => Some(spec),
      Kind::Struct(fields, _) if !self.unsized_ => {
        let (mut inner, mut i): (Option<&Layout>, usize) = (None, 0);
        while i < fields.len() {
          let field = &fields[i];
          if field.layout.size > 0 {
            if inner.is_some() || field.offset != 0 || field.layout.size != self.size {
              return None;
            }
            inner = Some(field.layout);
          }
          i += 1;
        }
        match inner {
          Some(inner) => inner.as_integer(),
          None => None,
        }
      }
      _ => None,
    }
  }

  /// The tail of a struct, as rules see it: its slice if it is unsized, else its last field.
  pub(crate) const fn tail_shape(&self) -> TailShape {
    let mut shape = TailShape { start: self.size, bytes: 0, len: 0, array: false };
    let Kind::Struct(fields, _) = self.kind else { return shape };
    if self.unsized_ || fields.is_empty() {
      return shape;
    }
    // The last field, as declared.
    let (mut last, mut i) = (&fields[0], 1);
    while i < fields.len() {
      if fields[i].index > last.index {
        last = &fields[i];
      }
      i += 1;
    }
    (shape.start, shape.bytes) = (last.offset, last.layout.size);
    // The array it ends with, if any.
    let mut layout = last.layout;
    loop {
      match layout.kind {
        Kind::Array(_, len) => {
          (shape.len, shape.array) = (len, true);
          return shape;
        }
        Kind::Struct(fields, _) if !fields.is_empty() => layout = fields[fields.len() - 1].layout,
        _ => return shape,
      }
    }
  }

  /// Offset of the first byte of the tail slice (the size of a `Sized` type).
  #[inline]
  pub(crate) const fn head(&self) -> usize {
    self.size
  }

  /// Whatever the all-zeros bit pattern is valid.
  pub(crate) const fn zeroable(&self) -> bool {
    match self.kind {
      Kind::Scalar(spec) => spec.contains(0),
      Kind::Array(elem, len) => len == 0 || elem.zeroable(),
      Kind::Slice(elem) => elem.zeroable(),
      Kind::Struct(fields, _) => {
        let mut i = 0;
        while i < fields.len() {
          if !fields[i].layout.zeroable() {
            return false;
          }
          i += 1;
        }
        true
      }
      Kind::Pattern(dfa) => dfa.states[0].accept,
      // No pointer.
      Kind::Pointer(pointer) => pointer.nullable,
      // A field of a union that zeros are.
      Kind::Enum(layout) if layout.union => {
        let mut i = 0;
        while i < layout.variants.len() {
          if layout.variants[i].is_present() && layout.variants[i].layout.zeroable() {
            return true;
          }
          i += 1;
        }
        false
      }
      // The variant whose tag is 0, if any.
      Kind::Enum(layout) => {
        let mut i = 0;
        while i < layout.variants.len() {
          if layout.variants[i].tags().has(0) && layout.variants[i].is_present() {
            let (fields, mut j) = (layout.variants[i].fields(), 0);
            while j < fields.len() {
              if !fields[j].layout.zeroable() {
                return false;
              }
              j += 1;
            }
            return true;
          }
          i += 1;
        }
        false
      }
    }
  }

  /// Whatever two structs have rules that are the same function of the bytes of each: the same
  /// rules, in the same order ([`Rule::equals`]), over a struct of the same extent. A rule is a
  /// function of the bytes of its struct, of its size (`size_of_val(self)`, a `size` rule), of
  /// the number of elements of its tail (a `len` rule) and of the bytes of its tail (regions):
  /// so the same sized part, and the same tail (where it starts, the size of an element, and for
  /// a sized struct its bytes and the elements of the array it ends with), whatever the fields.
  pub(crate) const fn same_rules(&self, other: &Layout) -> bool {
    let (Kind::Struct(_, mine), Kind::Struct(_, theirs)) = (self.kind, other.kind) else {
      return false;
    };
    let (tail, other_tail) = (self.tail_shape(), other.tail_shape());
    if mine.is_empty()
      || mine.len() != theirs.len()
      || self.size != other.size
      || self.tail != other.tail
      || self.unsized_ != other.unsized_
      || tail.start != other_tail.start
      || tail.bytes != other_tail.bytes
      || tail.len != other_tail.len
      || tail.array != other_tail.array
    {
      return false;
    }
    let mut i = 0;
    while i < mine.len() {
      if !mine[i].equals(&theirs[i]) {
        return false;
      }
      i += 1;
    }
    true
  }

  /// Whatever two layouts describe the same thing.
  #[expect(
    clippy::many_single_char_names,
    reason = "`a` and `x` are parts of `self`, `b` and `y` the same parts of `other`"
  )]
  pub(crate) const fn equals(&self, other: &Layout) -> bool {
    if self.size != other.size
      || self.align != other.align
      || self.tail != other.tail
      || self.unsized_ != other.unsized_
    {
      return false;
    }
    match (self.kind, other.kind) {
      // What a scalar requires, and what it is known to be: a layout that only requires what
      // another guarantees (`valid(..)` on a `u8`, and a `bool`) is not the same.
      //
      // A scalar that may be uninitialized is equal to none: what it is lies between what it
      // requires and what it is known to be, which two types can have the same of while one has
      // padding where the other does not.
      (Kind::Scalar(a), Kind::Scalar(b)) => {
        !a.may_be_uninit()
          && a.size == b.size
          && same_valid(a.valid, b.valid, a.endian, b.endian)
          && same_valid(a.known, b.known, a.endian, b.endian)
      }
      (Kind::Array(a, n), Kind::Array(b, m)) => n == m && a.equals(b),
      (Kind::Slice(a), Kind::Slice(b)) => a.equals(b),
      (Kind::Struct(a, ra), Kind::Struct(b, rb)) => {
        if a.len() != b.len() || ra.len() != rb.len() {
          return false;
        }
        let mut i = 0;
        while i < a.len() {
          if a[i].offset != b[i].offset || !a[i].layout.equals(b[i].layout) {
            return false;
          }
          i += 1;
        }
        let mut i = 0;
        while i < ra.len() {
          if !ra[i].equals(&rb[i]) {
            return false;
          }
          i += 1;
        }
        true
      }
      (Kind::Pattern(a), Kind::Pattern(b)) => a.id == b.id,
      // As far as the pointers themselves go: what they aim at is related through the types.
      (Kind::Pointer(a), Kind::Pointer(b)) => a.same(&b),
      (Kind::Enum(a), Kind::Enum(b)) => {
        if a.tag.offset != b.tag.offset
          || a.union != b.union
          || !a.tag.layout.equals(b.tag.layout)
          || a.variants.len() != b.variants.len()
        {
          return false;
        }
        let mut i = 0;
        while i < a.variants.len() {
          let (x, y) = (&a.variants[i], &b.variants[i]);
          if !x.tags().equals(&y.tags()) || x.holds_tag != y.holds_tag || !x.layout.equals(y.layout)
          {
            return false;
          }
          i += 1;
        }
        true
      }
      _ => false,
    }
  }
}

/// Whatever two sets of valid values of scalars of the same size are the same.
#[expect(
  clippy::many_single_char_names,
  reason = "`a` and `x` are of one scalar, `b` and `y` the same of the other"
)]
const fn same_valid(a: Valid, b: Valid, endian_a: Endian, endian_b: Endian) -> bool {
  match (a, b) {
    (Valid::Uninit, Valid::Uninit) | (Valid::Any, Valid::Any) => true,
    (Valid::In(x), Valid::In(y)) => {
      let mut i = 0;
      while i < x.len() && i < y.len() && x[i].0 == y[i].0 && x[i].1 == y[i].1 {
        i += 1;
      }
      endian_a.equals(endian_b) && x.len() == y.len() && i == x.len()
    }
    _ => false,
  }
}

// -------------------------------------------------------------------------------------------------
// `Dfa`

/// A deterministic finite automaton over bytes: the language of an unsized type like `str`.
///
/// The first state is the initial one. At most 64 states.
#[derive(Debug, PartialEq, Eq)]
pub struct Dfa {
  /// Two automata with the same `id` must be the same.
  pub id: u32,
  pub states: &'static [DfaState],
}

#[derive(Debug, PartialEq, Eq)]
pub struct DfaState {
  /// Whatever a value can end in this state.
  pub accept: bool,
  /// `(first byte, last byte, next state)`, sorted and disjoint.
  pub edges: &'static [(u8, u8, u8)],
}

impl Dfa {
  /// UTF-8, as of the Unicode standard (table 3-7, "well-formed UTF-8 byte sequences").
  pub const UTF8: &'static Dfa = &Dfa {
    id: 1,
    states: &[
      // 0: between two characters.
      DfaState {
        accept: true,
        edges: &[
          (0x00, 0x7f, 0),
          (0xc2, 0xdf, 1),
          (0xe0, 0xe0, 4),
          (0xe1, 0xec, 2),
          (0xed, 0xed, 5),
          (0xee, 0xef, 2),
          (0xf0, 0xf0, 6),
          (0xf1, 0xf3, 3),
          (0xf4, 0xf4, 7),
        ],
      },
      // 1, 2, 3: that many continuation bytes to go.
      DfaState { accept: false, edges: &[(0x80, 0xbf, 0)] },
      DfaState { accept: false, edges: &[(0x80, 0xbf, 1)] },
      DfaState { accept: false, edges: &[(0x80, 0xbf, 2)] },
      // 4: after `E0`, no overlong encoding.
      DfaState { accept: false, edges: &[(0xa0, 0xbf, 1)] },
      // 5: after `ED`, no surrogate.
      DfaState { accept: false, edges: &[(0x80, 0x9f, 1)] },
      // 6: after `F0`, no overlong encoding.
      DfaState { accept: false, edges: &[(0x90, 0xbf, 2)] },
      // 7: after `F4`, nothing above `U+10FFFF`.
      DfaState { accept: false, edges: &[(0x80, 0x8f, 2)] },
    ],
  };

  /// ASCII: bytes below `0x80`.
  pub const ASCII: &'static Dfa =
    &Dfa { id: 2, states: &[DfaState { accept: true, edges: &[(0x00, 0x7f, 0)] }] };

  /// Decimal digits, `0` to `9`.
  pub const DIGITS: &'static Dfa =
    &Dfa { id: 3, states: &[DfaState { accept: true, edges: &[(b'0', b'9', 0)] }] };

  /// Octal digits, `0` to `7`.
  pub const OCTAL: &'static Dfa =
    &Dfa { id: 4, states: &[DfaState { accept: true, edges: &[(b'0', b'7', 0)] }] };

  /// Hexadecimal digits, of either case.
  pub const HEX: &'static Dfa = &Dfa {
    id: 5,
    states: &[DfaState {
      accept: true,
      edges: &[(b'0', b'9', 0), (b'A', b'F', 0), (b'a', b'f', 0)],
    }],
  };

  /// UTF-8 without `U+0000` (the strings of MQTT).
  pub const UTF8_NO_NUL: &'static Dfa = &Dfa {
    id: 6,
    states: &[
      DfaState {
        accept: true,
        edges: &[
          (0x01, 0x7f, 0),
          (0xc2, 0xdf, 1),
          (0xe0, 0xe0, 4),
          (0xe1, 0xec, 2),
          (0xed, 0xed, 5),
          (0xee, 0xef, 2),
          (0xf0, 0xf0, 6),
          (0xf1, 0xf3, 3),
          (0xf4, 0xf4, 7),
        ],
      },
      DfaState { accept: false, edges: &[(0x80, 0xbf, 0)] },
      DfaState { accept: false, edges: &[(0x80, 0xbf, 1)] },
      DfaState { accept: false, edges: &[(0x80, 0xbf, 2)] },
      DfaState { accept: false, edges: &[(0xa0, 0xbf, 1)] },
      DfaState { accept: false, edges: &[(0x80, 0x9f, 1)] },
      DfaState { accept: false, edges: &[(0x90, 0xbf, 2)] },
      DfaState { accept: false, edges: &[(0x80, 0x8f, 2)] },
    ],
  };

  /// Whatever two automata have the same states and edges (what their `id`s do not prove).
  pub(crate) const fn same(&self, other: &Dfa) -> bool {
    if self.states.len() != other.states.len() {
      return false;
    }
    let mut state = 0;
    while state < self.states.len() {
      let (ours, theirs) = (&self.states[state], &other.states[state]);
      if ours.accept != theirs.accept || ours.edges.len() != theirs.edges.len() {
        return false;
      }
      let mut edge = 0;
      while edge < ours.edges.len() {
        let (first, second) = (ours.edges[edge], theirs.edges[edge]);
        if first.0 != second.0 || first.1 != second.1 || first.2 != second.2 {
          return false;
        }
        edge += 1;
      }
      state += 1;
    }
    true
  }

  /// Whatever the automaton accepts `bytes`, by its states and edges alone.
  pub(crate) const fn accepts_bytes(&self, bytes: &[u8]) -> bool {
    let (mut state, mut i) = (0, 0);
    'bytes: while i < bytes.len() {
      let (edges, mut j) = (self.states[state].edges, 0);
      while j < edges.len() {
        if edges[j].0 <= bytes[i] && bytes[i] <= edges[j].1 {
          state = edges[j].2 as usize;
          i += 1;
          continue 'bytes;
        }
        j += 1;
      }
      return false;
    }
    self.states[state].accept
  }

  const fn check(&self) {
    assert!(!self.states.is_empty() && self.states.len() <= 64, "a DFA has 1 to 64 states");
    let mut i = 0;
    while i < self.states.len() {
      let (edges, mut j) = (self.states[i].edges, 0);
      while j < edges.len() {
        assert!(edges[j].0 <= edges[j].1 && (edges[j].2 as usize) < self.states.len());
        assert!(j == 0 || edges[j - 1].1 < edges[j].0, "DFA edges must be sorted and disjoint");
        j += 1;
      }
      i += 1;
    }
  }

  /// States reached from the states of `set` (a bit for each) with a byte of `lo..=hi`, and
  /// whatever every such byte leads somewhere from every state of `set`.
  pub(crate) const fn step(&self, set: u64, lo: u8, hi: u8) -> (u64, bool) {
    let (mut next, mut total, mut state) = (0, true, 0);
    while state < self.states.len() {
      if set & (1 << state) != 0 {
        // `need` is the first byte of `lo..=hi` that no edge covers so far.
        let (edges, mut need, mut i) = (self.states[state].edges, lo as u16, 0);
        while i < edges.len() && need <= hi as u16 {
          let (first, last, to) = edges[i];
          if last as u16 >= need && first <= hi {
            total &= first as u16 <= need;
            next |= 1 << to;
            need = last as u16 + 1;
          }
          i += 1;
        }
        total &= need > hi as u16;
      }
      state += 1;
    }
    (next, total)
  }

  /// Whatever a value can end in every state of `set`.
  pub(crate) const fn accepts(&self, set: u64) -> bool {
    let mut state = 0;
    while state < self.states.len() {
      if set & (1 << state) != 0 && !self.states[state].accept {
        return false;
      }
      state += 1;
    }
    true
  }

  /// Every state that can be reached from the initial one.
  pub(crate) const fn reachable(&self) -> u64 {
    let mut set = 1;
    loop {
      let next = set | self.step(set, 0, 0xff).0;
      if next == set {
        return set;
      }
      set = next;
    }
  }

  /// Whatever the automaton accepts the `len` bytes at `ptr`.
  ///
  /// # Safety
  /// `ptr` must be valid for reads of `len` initialized bytes.
  #[inline]
  pub(crate) const unsafe fn matches(&self, ptr: *const u8, len: usize) -> bool {
    // SAFETY: guaranteed by the caller.
    let bytes = unsafe { core::slice::from_raw_parts(ptr, len) };
    if self.id == Self::UTF8.id {
      return core::str::from_utf8(bytes).is_ok();
    }
    self.accepts_bytes(bytes)
  }
}

// -------------------------------------------------------------------------------------------------
// `Rule`

/// An operation of a postfix expression over the scalar fields of a struct, see [`Rule`].
///
/// Expressions are evaluated on `u128`. What cannot be computed (an overflow, an underflow, a
/// division by zero, a shift by 128 or more, a read out of the value) makes the rule that holds
/// the expression fail. What Rust does not compute is not computed: the right side of a `&&` or
/// of a `||` that the left side decides, the branch of an `if` that is not taken
/// ([`Op::Shortcut`], [`Op::Then`], [`Op::Else`]). So `kind != 7 || be(40, 2) == 0` holds for a
/// value that is not of kind 7, however short, and `be(40, 2) == 0 || kind != 7` fails for one
/// that is too short. As nothing that is computed can then be left out of the result, the first
/// failure is the one of the expression, and its evaluation stops there. `<<` drops the bits that
/// it shifts out, as in Rust.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Op {
  /// Push a constant.
  Lit(u128),
  /// Push the unsigned integer of `size` bytes at `offset` in the struct.
  Load {
    offset: usize,
    size: u8,
    big: bool,
  },
  /// Push the size in bytes of the struct (not in `size` and `len` rules, which define it).
  Size,
  /// Replace an offset by the unsigned integer of `size` bytes that is there in the value.
  At {
    size: u8,
    big: bool,
  },
  /// Replace an offset by the variable-length integer of the `form` that is there in the value, or
  /// with `length`, by its number of bytes.
  Varint {
    form: Varint,
    length: bool,
  },
  /// Replace two offsets by the CRC-32 (the one of Ethernet) of the bytes between them.
  Crc32,
  /// Replace two offsets by the ones' complement sum of the big-endian 16 bits words between
  /// them (the checksum of IP, before it is complemented).
  Sum16,
  /// Replace two offsets by the CRC of the bytes between them.
  Crc(&'static Crc),
  /// Replace four offsets by the CRC of the bytes between the first two, the ones between the
  /// last two taken as zeros (a CRC that covers the field that holds it).
  CrcZeros(&'static Crc),
  /// Replace two offsets by a checksum of the bytes between them.
  Checksum(Checksum),
  /// Replace two offsets by whatever the automaton accepts the bytes between them: a pattern
  /// that a struct requires of them (which proves nothing of them, as any rule).
  Matches(&'static Dfa),
  Add,
  Sub,
  Mul,
  Div,
  Rem,
  And,
  Or,
  Xor,
  Shl,
  Shr,
  Eq,
  Ne,
  Lt,
  Le,
  Gt,
  Ge,
  /// `&&`, after its [`Op::Shortcut`] and its right side: replace the left side, which did not
  /// decide, and the right side by the right side.
  Both,
  /// `||`, as [`Op::Both`].
  Either,
  /// After the left side of a `&&` (`either: false`) or of a `||` (`either: true`): if that side
  /// decides (it is `either`), it is the result, and the `len` operations that follow, which are
  /// the right side and the [`Op::Both`] or [`Op::Either`], are not computed.
  Shortcut {
    either: bool,
    len: u16,
  },
  /// `!`, on a boolean.
  Not,
  /// After the condition of an `if`: if it is false, the `len` operations that follow, which are
  /// the branch of the `if` and its [`Op::Else`], are not computed (the value of the branch is
  /// then a placeholder, which [`Op::Select`] does not take).
  Then(u16),
  /// After the branch of an `if` that is taken when its condition is true: the `len` operations
  /// that follow, which are the `else` branch, are not computed (its value is a placeholder).
  Else(u16),
  /// After an [`Op::Then`] and an [`Op::Else`], and their branches: replace a condition and two
  /// values by the first value if the condition is true, by the second if not. Only that one
  /// was computed.
  Select,
  /// Take a value off the stack: it is the `index`th variable. What a `let` binds is computed,
  /// as in Rust, whether it is used or not.
  Set(u8),
  /// Push the `index`th variable.
  Get(u8),
  /// Replace a value by what it becomes when, for as long as the expression `cond` is true of
  /// it, it is replaced by the expression `next`. In both, the value is the variable `var`.
  ///
  /// This is how what is at an offset that depends on what is before is reached: options, a
  /// chain of headers. A walk takes at most `8 * size + 128` steps, where `size` is the size of
  /// the value in bytes: a step for each bit of the value, and for each bit of a `u128`.
  Walk {
    var: u8,
    cond: &'static [Op],
    next: &'static [Op],
  },
}

/// The types of the constants of rules: unsigned integers, which they compute on.
#[doc(hidden)]
#[diagnostic::on_unimplemented(
  message = "a constant of `#[pun(..)]` is an unsigned integer: `{Self}` is not one",
  label = "`#[pun(..)]` computes on unsigned integers",
  note = "an expression of `#[pun(..)]` means what it means in Rust, where every integer is a `u128`"
)]
pub trait Unsigned: Copy + seal::Unsigned {}

/// Implemented by this crate only.
mod seal {
  pub trait Unsigned {}
}

impl seal::Unsigned for u8 {}
impl seal::Unsigned for u16 {}
impl seal::Unsigned for u32 {}
impl seal::Unsigned for u64 {}
impl seal::Unsigned for u128 {}
impl seal::Unsigned for usize {}
impl Unsigned for u8 {}
impl Unsigned for u16 {}
impl Unsigned for u32 {}
impl Unsigned for u64 {}
impl Unsigned for u128 {}
impl Unsigned for usize {}

impl Op {
  /// [`Op::Matches`], of an automaton that is checked to be one.
  #[doc(hidden)]
  #[must_use]
  pub const fn matches(dfa: &'static Dfa) -> Self {
    dfa.check();
    Op::Matches(dfa)
  }

  /// What `as usize` keeps of a value.
  #[doc(hidden)]
  pub const USIZE_MAX: u128 = usize::MAX as u128;

  /// The value of a constant of a rule.
  #[doc(hidden)]
  #[must_use]
  pub const fn constant<T: Unsigned>(value: &T) -> u128 {
    // SAFETY: an unsigned integer is initialized, and has no padding.
    unsafe { load(core::ptr::from_ref::<T>(value).cast(), size_of::<T>(), Endian::NATIVE) }
  }

  /// Push the value of the field of type `T` at `offset`: an integer, or a struct that only
  /// holds one.
  ///
  /// # Panics
  /// If `T` is not an integer.
  #[must_use]
  pub const fn load<T: crate::Pun>(offset: usize) -> Self {
    let Some(spec) = T::LAYOUT.as_integer() else {
      panic!(
        "a rule reads the unsigned integer fields of its struct, and this field is not one (a \
         signed or floating-point number, a `bool`, a `char` or an enum is not): read its bytes \
         with `le(offset, size)` or `be(offset, size)`"
      )
    };
    Op::Load { offset, size: spec.size as u8, big: !spec.endian.is_little() }
  }

  #[expect(
    clippy::many_single_char_names,
    reason = "`a`, `b`, `c` of one side are compared with `x`, `y`, `z` of the other"
  )]
  const fn equals(&self, other: &Op) -> bool {
    match (*self, *other) {
      (Op::Lit(a), Op::Lit(b)) => a == b,
      (Op::Load { offset: a, size: b, big: c }, Op::Load { offset: x, size: y, big: z }) => {
        a == x && b == y && c == z
      }
      (Op::At { size: a, big: b }, Op::At { size: x, big: y }) => a == x && b == y,
      (Op::Varint { form: a, length: b }, Op::Varint { form: x, length: y }) => {
        a as u8 == x as u8 && b == y
      }
      (Op::Crc(a), Op::Crc(b)) | (Op::CrcZeros(a), Op::CrcZeros(b)) => a.equals(b),
      (Op::Checksum(a), Op::Checksum(b)) => a as u8 == b as u8,
      (Op::Matches(a), Op::Matches(b)) => a.same(b),
      (Op::Set(a), Op::Set(b)) | (Op::Get(a), Op::Get(b)) => a == b,
      (Op::Shortcut { either: a, len: b }, Op::Shortcut { either: x, len: y }) => a == x && b == y,
      (Op::Then(a), Op::Then(b)) | (Op::Else(a), Op::Else(b)) => a == b,
      (Op::Walk { var: a, cond: b, next: c }, Op::Walk { var: x, cond: y, next: z }) => {
        a == x && same_ops(b, y) && same_ops(c, z)
      }
      (
        Op::Lit(_)
        | Op::Load { .. }
        | Op::At { .. }
        | Op::Varint { .. }
        | Op::Crc(_)
        | Op::CrcZeros(_)
        | Op::Checksum(_)
        | Op::Matches(_)
        | Op::Set(_)
        | Op::Get(_)
        | Op::Shortcut { .. }
        | Op::Then(_)
        | Op::Else(_)
        | Op::Walk { .. },
        _,
      )
      | (
        _,
        Op::Lit(_)
        | Op::Load { .. }
        | Op::At { .. }
        | Op::Varint { .. }
        | Op::Crc(_)
        | Op::CrcZeros(_)
        | Op::Checksum(_)
        | Op::Matches(_)
        | Op::Set(_)
        | Op::Get(_)
        | Op::Shortcut { .. }
        | Op::Then(_)
        | Op::Else(_)
        | Op::Walk { .. },
      ) => false,
      // Other operations hold no data.
      (a, b) => a.code() == b.code(),
    }
  }

  const fn code(self) -> u8 {
    // SAFETY-free: the discriminant of a variant without data, by exhaustion.
    match self {
      Op::Lit(_) => 0,
      Op::Load { .. } => 1,
      Op::Add => 2,
      Op::Sub => 3,
      Op::Mul => 4,
      Op::Div => 5,
      Op::Rem => 6,
      Op::And => 7,
      Op::Or => 8,
      Op::Xor => 9,
      Op::Shl => 10,
      Op::Shr => 11,
      Op::Eq => 12,
      Op::Ne => 13,
      Op::Lt => 14,
      Op::Le => 15,
      Op::Gt => 16,
      Op::Ge => 17,
      Op::Both => 18,
      Op::Either => 19,
      Op::Not => 20,
      Op::Size => 21,
      Op::At { .. } => 22,
      Op::Crc32 => 23,
      Op::Sum16 => 24,
      Op::Select => 25,
      Op::Set(_) => 26,
      Op::Get(_) => 27,
      Op::Walk { .. } => 28,
      Op::Varint { .. } => 30,
      Op::Crc(_) => 31,
      Op::CrcZeros(_) => 32,
      Op::Checksum(_) => 33,
      Op::Matches(_) => 34,
      Op::Shortcut { .. } => 29,
      Op::Then(_) => 35,
      Op::Else(_) => 36,
    }
  }
}

/// A CRC of at most 64 bits, by the parameters of the catalogue of CRCs.
///
/// They are its `width`, its polynomial `poly` (without its high bit), the value `init` that the
/// register starts with, whatever its bits are `reflect`ed (the bytes taken least significant bit
/// first, and the result read so), and `xorout`, what the result is combined with by XOR.
///
/// ```
/// use puns::layout::Crc;
///
/// // CRC-32C (Castagnoli, iSCSI, SCTP), CRC-16/MODBUS, CRC-16/XMODEM, CRC-15/CAN.
/// static CASTAGNOLI: Crc = Crc::new(32, 0x1edc_6f41, 0xffff_ffff, true, 0xffff_ffff);
/// assert_eq!(CASTAGNOLI.check(b"123456789"), 0xe306_9283);
/// assert_eq!(Crc::new(16, 0x8005, 0xffff, true, 0).check(b"123456789"), 0x4b37);
/// assert_eq!(Crc::new(16, 0x1021, 0, false, 0).check(b"123456789"), 0x31c3);
/// assert_eq!(Crc::new(15, 0x4599, 0, false, 0).check(b"123456789"), 0x059e);
/// ```
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Crc {
  width: u32,
  poly: u64,
  init: u64,
  reflect: bool,
  xorout: u64,
  /// `tables[k][b]`: the register after the byte `b` and `k` zero bytes, from a register of
  /// zeros: in its low bits if reflected, else in its high bits. Sixteen bytes are then sixteen
  /// lookups that do not wait for each other (slicing by 16), rather than a chain of sixteen.
  tables: Tables,
}

/// The tables of a [`Crc`] ([`CRC_TABLES`] of them): of `u32`s for a CRC of at most 32 bits (16
/// KiB, half of what the cache holds of them otherwise), else of `u64`s.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[expect(
  clippy::large_enum_variant,
  reason = "a `Crc` is a constant of 32 KiB, as it was with tables of `u64`s only: nothing to box"
)]
enum Tables {
  /// The register in the low bits of a `u32` if reflected, else in its high bits.
  Narrow([[u32; 256]; CRC_TABLES]),
  /// The register in the low bits of a `u64` if reflected, else in its high bits.
  Wide([[u64; 256]; CRC_TABLES]),
}

/// The number of tables of a [`Crc`]: 16 (32 KiB, sixteen bytes at a time), but 1 (2 KiB, a byte
/// at a time) on a 16-bit target, where no value is larger than `isize::MAX` (32 KiB minus 1)
/// bytes. The CRC is the same either way.
pub(crate) const CRC_TABLES: usize = if cfg!(target_pointer_width = "16") { 1 } else { 16 };

// The tables of a `Crc` are a value, which is at most `isize::MAX` bytes.
const _: () = assert!(size_of::<[[u64; 256]; CRC_TABLES]>() <= isize::MAX as usize);

impl Crc {
  /// The CRC-32 of Ethernet, zlib, PNG: what `crc32(..)` is.
  pub const CRC32: Self = Self::new(32, 0x04c1_1db7, 0xffff_ffff, true, 0xffff_ffff);

  /// Its tables are 32 KiB (16 KiB of them are used for a CRC of up to 32 bits): a `Crc` is
  /// meant to be a constant, made in const evaluation.
  ///
  /// # Panics
  /// If `width` is not 1 to 64, or a parameter has more bits than that.
  #[must_use]
  #[expect(clippy::large_stack_arrays, reason = "made in const evaluation, as a constant")]
  pub const fn new(width: u32, poly: u64, init: u64, reflect: bool, xorout: u64) -> Self {
    assert!(width >= 1 && width <= 64, "a CRC has 1 to 64 bits");
    let mask = u64::MAX >> (64 - width);
    assert!(
      poly <= mask && init <= mask && xorout <= mask,
      "a parameter has more bits than the CRC"
    );
    let (mut tables, mut i) = ([[0; 256]; CRC_TABLES], 0);
    while i < 256 {
      let mut register = if reflect { i as u64 } else { (i as u64) << 56 };
      let mut bit = 0;
      while bit < 8 {
        register = match (reflect, register & 1, register >> 63) {
          (true, 1, _) => (register >> 1) ^ (poly.reverse_bits() >> (64 - width)),
          (true, ..) => register >> 1,
          (false, _, 1) => (register << 1) ^ (poly << (64 - width)),
          (false, ..) => register << 1,
        };
        bit += 1;
      }
      tables[0][i] = register;
      i += 1;
    }
    // A zero byte more.
    let mut k = 1;
    while k < CRC_TABLES {
      let mut i = 0;
      while i < 256 {
        let register = tables[k - 1][i];
        tables[k][i] = if reflect {
          tables[0][(register & 0xff) as usize] ^ (register >> 8)
        } else {
          tables[0][(register >> 56) as usize] ^ (register << 8)
        };
        i += 1;
      }
      k += 1;
    }
    let tables = if width > 32 {
      Tables::Wide(tables)
    } else {
      // The register of the `u64`s, in a `u32`: its other bits are zeros.
      let (mut narrow, mut k) = ([[0; 256]; CRC_TABLES], 0);
      while k < CRC_TABLES {
        let mut i = 0;
        while i < 256 {
          narrow[k][i] = if reflect { tables[k][i] as u32 } else { (tables[k][i] >> 32) as u32 };
          i += 1;
        }
        k += 1;
      }
      Tables::Narrow(narrow)
    };
    Self { width, poly, init, reflect, xorout, tables }
  }

  /// The CRC of `bytes`.
  #[must_use]
  pub const fn check(&self, bytes: &[u8]) -> u64 {
    self.compute(bytes, (0, 0))
  }

  /// The CRC of `bytes`, the ones from `zeros.0` to `zeros.1` taken as zeros.
  #[inline]
  pub(crate) const fn compute(&self, bytes: &[u8], zeros: (usize, usize)) -> u64 {
    let mut register = if self.reflect {
      self.init.reverse_bits() >> (64 - self.width)
    } else {
      self.init << (64 - self.width)
    };
    let to = if zeros.1 < bytes.len() { zeros.1 } else { bytes.len() };
    let from = if zeros.0 < to { zeros.0 } else { to };
    let (before, rest) = bytes.split_at(from);
    let (zeros, after) = rest.split_at(to - from);
    register = self.update(register, before);
    // As many zeros, a block at a time.
    let mut zeros = zeros.len();
    while zeros > 0 {
      let count = if zeros < 64 { zeros } else { 64 };
      register = self.update(register, [0; 64].split_at(count).0);
      zeros -= count;
    }
    register = self.update(register, after);
    if !self.reflect {
      register >>= 64 - self.width;
    }
    register ^ self.xorout
  }

  /// The register after `bytes`.
  #[inline]
  const fn update(&self, register: u64, bytes: &[u8]) -> u64 {
    match &self.tables {
      // In a `u32`: the low bits of the register if reflected, else its high bits.
      Tables::Narrow(t) => {
        let narrow = if self.reflect { register as u32 } else { (register >> 32) as u32 };
        let narrow = Self::update_narrow(t, self.reflect, narrow, bytes);
        if self.reflect { narrow as u64 } else { (narrow as u64) << 32 }
      }
      Tables::Wide(t) => Self::update_wide(t, self.reflect, register, bytes),
    }
  }

  /// [`Crc::update`] with tables of `u32`s: as the CRC-32 of Ethernet ([`crc32`]).
  #[inline]
  const fn update_narrow(
    t: &[[u32; 256]; CRC_TABLES],
    reflect: bool,
    mut register: u32,
    bytes: &[u8],
  ) -> u32 {
    // With one table, every byte goes through the loop below.
    let (blocks, rest): (&[[u8; 16]], &[u8]) =
      if CRC_TABLES == 16 { bytes.as_chunks::<16>() } else { (&[], bytes) };
    // A slice: with one table, the indices of the sixteen are never reached (no block), which
    // the compiler cannot tell of an array.
    #[cfg(target_pointer_width = "16")]
    let t: &[[u32; 256]] = t;
    let mut i = 0;
    while i < blocks.len() {
      let b = &blocks[i];
      // The register is the first 4 bytes of a block: what it holds after them, and after the 12
      // that follow, is a lookup for each byte.
      let head = [b[0], b[1], b[2], b[3]];
      let x = register ^ if reflect { u32::from_le_bytes(head) } else { u32::from_be_bytes(head) };
      let x = if reflect { x.to_le_bytes() } else { x.to_be_bytes() };
      register = t[15][x[0] as usize]
        ^ t[14][x[1] as usize]
        ^ t[13][x[2] as usize]
        ^ t[12][x[3] as usize]
        ^ t[11][b[4] as usize]
        ^ t[10][b[5] as usize]
        ^ t[9][b[6] as usize]
        ^ t[8][b[7] as usize]
        ^ t[7][b[8] as usize]
        ^ t[6][b[9] as usize]
        ^ t[5][b[10] as usize]
        ^ t[4][b[11] as usize]
        ^ t[3][b[12] as usize]
        ^ t[2][b[13] as usize]
        ^ t[1][b[14] as usize]
        ^ t[0][b[15] as usize];
      i += 1;
    }
    let mut i = 0;
    while i < rest.len() {
      register = if reflect {
        t[0][((register ^ rest[i] as u32) & 0xff) as usize] ^ (register >> 8)
      } else {
        t[0][((register >> 24) ^ rest[i] as u32) as usize] ^ (register << 8)
      };
      i += 1;
    }
    register
  }

  /// [`Crc::update`] with tables of `u64`s.
  #[inline]
  const fn update_wide(
    t: &[[u64; 256]; CRC_TABLES],
    reflect: bool,
    mut register: u64,
    bytes: &[u8],
  ) -> u64 {
    // With one table, every byte goes through the loop below.
    let (blocks, rest): (&[[u8; 16]], &[u8]) =
      if CRC_TABLES == 16 { bytes.as_chunks::<16>() } else { (&[], bytes) };
    // As above.
    #[cfg(target_pointer_width = "16")]
    let t: &[[u64; 256]] = t;
    let mut i = 0;
    while i < blocks.len() {
      let b = &blocks[i];
      // The register is the first 8 bytes of a block, as many as a `u64` holds (its bits that
      // are not those of the CRC are zeros): what it holds after them, and after the 8 bytes
      // that follow, is a lookup for each byte.
      let head = [b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]];
      let x = register ^ if reflect { u64::from_le_bytes(head) } else { u64::from_be_bytes(head) };
      let x = if reflect { x.to_le_bytes() } else { x.to_be_bytes() };
      register = t[15][x[0] as usize]
        ^ t[14][x[1] as usize]
        ^ t[13][x[2] as usize]
        ^ t[12][x[3] as usize]
        ^ t[11][x[4] as usize]
        ^ t[10][x[5] as usize]
        ^ t[9][x[6] as usize]
        ^ t[8][x[7] as usize]
        ^ t[7][b[8] as usize]
        ^ t[6][b[9] as usize]
        ^ t[5][b[10] as usize]
        ^ t[4][b[11] as usize]
        ^ t[3][b[12] as usize]
        ^ t[2][b[13] as usize]
        ^ t[1][b[14] as usize]
        ^ t[0][b[15] as usize];
      i += 1;
    }
    let mut i = 0;
    while i < rest.len() {
      register = if reflect {
        t[0][((register ^ rest[i] as u64) & 0xff) as usize] ^ (register >> 8)
      } else {
        t[0][((register >> 56) ^ rest[i] as u64) as usize] ^ (register << 8)
      };
      i += 1;
    }
    register
  }

  /// The CRC of `bytes` with the first table only, a byte at a time: what a target of one table
  /// computes, for the tests.
  #[cfg(test)]
  pub(crate) fn bytewise(&self, bytes: &[u8]) -> u64 {
    let mut register = if self.reflect {
      self.init.reverse_bits() >> (64 - self.width)
    } else {
      self.init << (64 - self.width)
    };
    // The first table, as the register of a `u64`.
    let t = |i: u64| match &self.tables {
      Tables::Narrow(t) if self.reflect => u64::from(t[0][i as usize]),
      Tables::Narrow(t) => u64::from(t[0][i as usize]) << 32,
      Tables::Wide(t) => t[0][i as usize],
    };
    for &byte in bytes {
      register = if self.reflect {
        t((register ^ u64::from(byte)) & 0xff) ^ (register >> 8)
      } else {
        t((register >> 56) ^ u64::from(byte)) ^ (register << 8)
      };
    }
    if !self.reflect {
      register >>= 64 - self.width;
    }
    register ^ self.xorout
  }

  const fn equals(&self, other: &Crc) -> bool {
    self.width == other.width
      && self.poly == other.poly
      && self.init == other.init
      && self.reflect == other.reflect
      && self.xorout == other.xorout
  }
}

/// A checksum of bytes, see [`Op::Checksum`].
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Checksum {
  /// The sum of the bytes (not reduced: `% 256` makes it the one of a byte).
  Sum,
  /// Fletcher-16: two sums modulo 255, the second of the first, as `second << 8 | first`. It is
  /// `0` over data that holds its ISO 8473 checksum (OSPF, IS-IS).
  Fletcher16,
  /// Adler-32 (zlib): two sums modulo 65521, the first starting at 1, as `second << 16 | first`.
  Adler32,
}

impl Checksum {
  /// The checksum of `bytes`.
  const fn compute(self, bytes: &[u8]) -> u128 {
    let (modulo, mut first, mut second, mut i) = match self {
      Checksum::Sum => (0, 0u128, 0u128, 0),
      Checksum::Fletcher16 => (255, 0, 0, 0),
      Checksum::Adler32 => (65521, 1, 0, 0),
    };
    while i < bytes.len() {
      first += bytes[i] as u128;
      if modulo > 0 {
        first %= modulo;
        second = (second + first) % modulo;
      }
      i += 1;
    }
    match self {
      Checksum::Sum => first,
      Checksum::Fletcher16 => second << 8 | first,
      Checksum::Adler32 => second << 16 | first,
    }
  }
}

/// A form of variable-length integer, see [`Op::Varint`].
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Varint {
  /// Unsigned LEB128 (Protocol Buffers, WebAssembly, MQTT, DWARF): 7 bits a byte, the least
  /// significant first, each byte but the last with its high bit set.
  Leb128,
  /// The variable-length integer of QUIC (RFC 9000): the two high bits of the first byte tell
  /// its length (1, 2, 4 or 8 bytes), the rest is the value, big-endian.
  Quic,
  /// 7 bits a byte, the most significant first, each byte but the last with its high bit set
  /// (the tag numbers and the arcs of object identifiers of ASN.1, MIDI).
  Vlq,
}

impl Varint {
  /// The value of the integer at `ptr`, and its number of bytes, of at most `size`: `None` if
  /// it is not over by then, or does not fit in a `u128`.
  ///
  /// # Safety
  /// `ptr` must be valid for reads of `size` initialized bytes.
  #[inline]
  const unsafe fn read(self, ptr: *const u8, size: usize) -> Option<(u128, usize)> {
    // SAFETY: guaranteed by the caller.
    let bytes = unsafe { core::slice::from_raw_parts(ptr, size) };
    if matches!(self, Varint::Quic) {
      if bytes.is_empty() {
        return None;
      }
      let length = 1 << (bytes[0] >> 6);
      if bytes.len() < length {
        return None;
      }
      let (mut value, mut i) = ((bytes[0] & 0x3f) as u128, 1);
      while i < length {
        value = value << 8 | bytes[i] as u128;
        i += 1;
      }
      return Some((value, length));
    }
    let (mut value, mut i) = (0u128, 0);
    while i < bytes.len() {
      let bits = (bytes[i] & 0x7f) as u128;
      match self {
        // 18 bytes are 126 bits: the 19th can only add 2, and there is no 20th.
        Varint::Leb128 if i < 18 || (i == 18 && bits < 4) => value |= bits << (7 * i),
        Varint::Vlq if value >> 121 == 0 => value = value << 7 | bits,
        _ => return None,
      }
      i += 1;
      if bytes[i - 1] & 0x80 == 0 {
        return Some((value, i));
      }
    }
    None
  }
}

/// Whatever an expression only computes on fields with a few instructions: it does not read the
/// value at an offset that it computes, pass over its bytes, walk, or divide.
const fn cheap_ops(ops: &[Op]) -> bool {
  let mut i = 0;
  while i < ops.len() {
    if matches!(
      ops[i],
      Op::At { .. }
        | Op::Varint { .. }
        | Op::Crc32
        | Op::Sum16
        | Op::Crc(_)
        | Op::CrcZeros(_)
        | Op::Checksum(_)
        | Op::Matches(_)
        | Op::Walk { .. }
        | Op::Div
        | Op::Rem
    ) {
      return false;
    }
    i += 1;
  }
  true
}

/// Whatever two expressions are the same.
pub(crate) const fn same_ops(a: &[Op], b: &[Op]) -> bool {
  let mut i = 0;
  while i < a.len() && i < b.len() && a[i].equals(&b[i]) {
    i += 1;
  }
  a.len() == b.len() && i == a.len()
}

/// Something that values of a struct must satisfy, beyond the validity of their fields.
///
/// Rules are checked when a value is made from something else (`try_pun*`, `from_head`), and
/// tell `from_head` how long a value is. They are not invariants that unsafe code may rely on: safe
/// code can write to the fields of a struct.
#[derive(Copy, Clone, Debug)]
pub enum Rule {
  /// The size in bytes of the value is the result of the expression.
  Size(&'static [Op]),
  /// The number of elements of the tail slice of the value is the result of the expression.
  Len(&'static [Op]),
  /// The expression is true.
  Assert(&'static [Op]),
  /// The tail of the value starts with a region of as many bytes as the result of the
  /// expression (`None`: what remains). Regions follow each other, in the order of the rules.
  Region(Option<&'static [Op]>),
}

/// Whatever an expression reads bytes of the value that are not those of its integer fields.
const fn ops_read_bytes(ops: &[Op]) -> bool {
  let mut i = 0;
  while i < ops.len() {
    match ops[i] {
      Op::At { .. }
      | Op::Varint { .. }
      | Op::Crc32
      | Op::Sum16
      | Op::Crc(_)
      | Op::CrcZeros(_)
      | Op::Checksum(_)
      | Op::Matches(_) => return true,
      Op::Walk { cond, next, .. } if ops_read_bytes(cond) || ops_read_bytes(next) => return true,
      _ => {}
    }
    i += 1;
  }
  false
}

impl Rule {
  /// Its expression, if it has one.
  pub(crate) const fn ops(&self) -> Option<&'static [Op]> {
    match *self {
      Rule::Size(ops) | Rule::Len(ops) | Rule::Assert(ops) | Rule::Region(Some(ops)) => Some(ops),
      Rule::Region(None) => None,
    }
  }

  pub(crate) const fn equals(&self, other: &Rule) -> bool {
    let same_kind = matches!(
      (self, other),
      (Rule::Size(_), Rule::Size(_))
        | (Rule::Len(_), Rule::Len(_))
        | (Rule::Assert(_), Rule::Assert(_))
        | (Rule::Region(_), Rule::Region(_))
    );
    match (self.ops(), other.ops()) {
      (None, None) => same_kind,
      (Some(a), Some(b)) => same_kind && same_ops(a, b),
      _ => false,
    }
  }
}

/// See [`Layout::tail_shape`].
#[derive(Copy, Clone)]
pub(crate) struct TailShape {
  /// Offset of the tail.
  pub(crate) start: usize,
  /// Size of the tail, if the struct is `Sized`.
  pub(crate) bytes: usize,
  /// Number of elements of the array that the tail ends with, if the struct is `Sized`.
  pub(crate) len: usize,
  pub(crate) array: bool,
}

/// See [`Layout::length_rule`].
#[derive(Copy, Clone)]
pub(crate) struct LengthRule {
  /// Offset of the struct that has the rule.
  pub(crate) base: usize,
  /// The expression is a number of elements of the tail slice, rather than the size in bytes of
  /// the struct.
  pub(crate) elements: bool,
  pub(crate) ops: &'static [Op],
}

/// Depth of the stack of the evaluator ([`Machine`]).
const STACK: usize = 16;
/// Number of variables of an expression.
const VARIABLES: usize = 8;
/// The most operations of an expression, and the most walks that it nests (a walk in a walk is
/// two): the validation applies each operation in an instance of a function of its own, reached
/// by recursions over a tree of the operations (`validate::steps`), which these keep short.
/// `#[derive(Pun)]` has the same limits, and says where an expression goes beyond them.
pub(crate) const MAX_OPS: usize = 256;
pub(crate) const MAX_WALKS: usize = 4;

/// Check that the expressions of `rules` are well formed, and only read the first `head` bytes.
const fn check_rules(rules: &[Rule], head: usize) {
  let mut i = 0;
  while i < rules.len() {
    if let Some(ops) = rules[i].ops() {
      let sizes = matches!(rules[i], Rule::Size(_) | Rule::Len(_));
      check_ops(ops, head, sizes, 0, 0);
    }
    i += 1;
  }
}

/// The `index`th operation of `ops`, if any.
pub(crate) const fn op_at(ops: &[Op], index: usize) -> Option<Op> {
  if index < ops.len() { Some(ops[index]) } else { None }
}

/// Check an expression, in which the variables of `defined` are (a bit for each), and which is in
/// `walks` walks. With `sizes`, it is a size or a length, which cannot be computed from the size
/// of the value.
///
/// What an [`Op::Shortcut`], an [`Op::Then`] or an [`Op::Else`] does not compute is a *part*: the
/// right side of a `&&` or `||`, a branch of an `if`. A part must end with what it is a part of
/// (the `Both`, `Either`, `Else` or `Select`), be within the part that holds it, and push one
/// value without taking any of what is below: then the stack is as deep after a part, whether it
/// was computed or not (the evaluator pushes a placeholder for a branch that is not). What a part
/// defines is not defined after it. Every `&&`, `||` and `if` has its parts: what is computed is
/// what Rust computes, and no failure can be left out of the value of the expression.
const fn check_ops(ops: &[Op], head: usize, sizes: bool, mut defined: u8, walks: usize) {
  assert!(ops.len() <= MAX_OPS, "an expression is too long");
  let (mut depth, mut i) = (0usize, 0);
  // The parts that are open, innermost last: the index of the operation that ends each, the depth
  // of the stack before it, the variables defined before it, and the operation that opened it.
  let (mut parts, mut open) = ([(0usize, 0usize, 0u8, Op::Size); STACK], 0);
  while i < ops.len() {
    let op = ops[i];
    assert!(
      !(sizes && matches!(op, Op::Size)),
      "a size or a length cannot be computed from `size_of_val(self)`, which it defines"
    );
    // The part that this operation ends, if any, and what it must be.
    let ends = open > 0 && parts[open - 1].0 == i;
    if ends {
      let (_, floor, before, opener) = parts[open - 1];
      assert!(depth == floor + 1, "malformed expression");
      let fits = match (opener, op) {
        (Op::Shortcut { either, .. }, Op::Both) => !either,
        (Op::Shortcut { either, .. }, Op::Either) => either,
        (Op::Then(_), Op::Else(_)) | (Op::Else(_), Op::Select) => true,
        _ => false,
      };
      assert!(fits, "malformed expression");
      (open, defined) = (open - 1, before);
    } else {
      assert!(
        !matches!(op, Op::Both | Op::Either | Op::Else(_) | Op::Select),
        "malformed expression"
      );
    }
    let (pops, pushes) = effect(op);
    assert!(depth >= pops, "malformed expression");
    assert!(open == 0 || depth - pops >= parts[open - 1].1, "malformed expression");
    // A part that starts after this operation, and the operation that ends it: the one after it,
    // but for the part of an `Else`, which is followed by the `Select` (which it does not skip).
    if let Op::Shortcut { len, .. } | Op::Then(len) | Op::Else(len) = op {
      let end = i + len as usize + matches!(op, Op::Else(_)) as usize;
      assert!(depth >= 1 && len >= 1 && end < ops.len(), "malformed expression");
      assert!(open == 0 || end < parts[open - 1].0, "malformed expression");
      assert!(open < STACK, "expression is too deep");
      parts[open] = (end, depth, defined, op);
      open += 1;
    }
    match op {
      Op::At { size, .. } => assert!(size >= 1 && size <= 16, "malformed expression"),
      Op::Load { offset, size, .. } => {
        assert!(size >= 1 && size <= 16 && offset + size as usize <= head, "field out of bounds");
      }
      Op::Set(index) => {
        assert!((index as usize) < VARIABLES, "too many variables in an expression");
        defined |= 1 << index;
      }
      Op::Get(index) => {
        assert!((index as usize) < VARIABLES && defined & (1 << index) != 0, "no such variable");
      }
      Op::Walk { var, cond, next } => {
        assert!(walks < MAX_WALKS, "walks are nested too deeply");
        assert!((var as usize) < VARIABLES, "too many variables in an expression");
        check_ops(cond, head, sizes, defined | 1 << var, walks + 1);
        check_ops(next, head, sizes, defined | 1 << var, walks + 1);
      }
      _ => {}
    }
    depth = depth - pops + pushes;
    assert!(depth <= STACK, "expression is too deep");
    i += 1;
  }
  assert!(depth == 1 && open == 0, "malformed expression");
}

/// What an operation takes off the stack, and what it puts on it, where it is computed (see
/// [`check_ops`] for the parts that are not).
const fn effect(op: Op) -> (usize, usize) {
  match op {
    Op::Lit(_) | Op::Load { .. } | Op::Size | Op::Get(_) => (0, 1),
    Op::At { .. }
    | Op::Varint { .. }
    | Op::Not
    | Op::Walk { .. }
    | Op::Shortcut { .. }
    | Op::Then(_) => (1, 1),
    // After the branch that is taken when the condition is true, which it does not take off.
    Op::Else(_) => (0, 0),
    Op::Set(_) => (1, 0),
    Op::Select => (3, 1),
    Op::CrcZeros(_) => (4, 1),
    Op::Crc32
    | Op::Sum16
    | Op::Crc(_)
    | Op::Checksum(_)
    | Op::Matches(_)
    | Op::Add
    | Op::Sub
    | Op::Mul
    | Op::Div
    | Op::Rem
    | Op::And
    | Op::Or
    | Op::Xor
    | Op::Shl
    | Op::Shr
    | Op::Eq
    | Op::Ne
    | Op::Lt
    | Op::Le
    | Op::Gt
    | Op::Ge
    | Op::Both
    | Op::Either => (2, 1),
  }
}

/// What evaluates an expression: a stack, and variables.
///
/// It is given one operation at a time, each a constant of the instance of the function that
/// applies it (`validate::steps`): there is no interpreter. This is written so that what remains
/// once it is inlined there is the arithmetic of the expression, without relying on much of the
/// optimizer: the stack is not an array but a variable for each of its slots, what is not
/// computed is told by an index (`skip`), which is a constant on each path through the code, and
/// a failure, which is the one of the expression (nothing that is computed is left out of its
/// value), returns at once.
#[derive(Copy, Clone)]
pub(crate) struct Machine {
  stack: Stack,
  variables: Variables,
  depth: usize,
  /// The operations before this index are not computed: the part that an [`Op::Shortcut`], an
  /// [`Op::Then`] or an [`Op::Else`] skips.
  skip: usize,
}

/// `slots!(Name: [0 1 2 ..] last)`: values in fields, one for each index, rather than in an array: so
/// that the optimizer makes a variable of each, whatever the index asked for (see [`Machine`]).
macro_rules! slots {
  ($($name:ident: [$($index:tt)+] $last:tt)+) => {$(
    #[derive(Copy, Clone)]
    struct $name($(slots!(@u128 $index),)+ u128);

    impl $name {
      const NEW: Self = Self($(slots!(@zero $index),)+ 0);

      // The last one is the one of any other index, which is not asked for: no other case.
      #[inline(always)]
      const fn get(&self, index: usize) -> u128 {
        match index {
          $($index => self.$index,)+
          _ => self.$last,
        }
      }

      #[inline(always)]
      const fn set(&mut self, index: usize, value: u128) {
        match index {
          $($index => self.$index = value,)+
          _ => self.$last = value,
        }
      }
    }
  )+};
  (@u128 $index:tt) => { u128 };
  (@zero $index:tt) => { 0 };
}

slots! {
  Stack: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] 15
  Variables: [0 1 2 3 4 5 6] 7
}

impl Machine {
  pub(crate) const NEW: Self =
    Self { stack: Stack::NEW, variables: Variables::NEW, depth: 0, skip: 0 };

  #[inline(always)]
  const fn get(&self, slot: usize) -> u128 {
    self.stack.get(slot)
  }

  #[inline(always)]
  const fn set(&mut self, slot: usize, value: u128) {
    self.stack.set(slot, value);
  }

  #[inline(always)]
  const fn push(&mut self, value: u128) {
    self.stack.set(self.depth, value);
    self.depth += 1;
  }

  /// Whatever the operation at the index `at` of the expression is computed.
  #[inline(always)]
  pub(crate) const fn computes(&self, at: usize) -> bool {
    at >= self.skip
  }

  /// The value of the expression, once all of its operations are applied without a failure.
  #[inline(always)]
  pub(crate) const fn result(&self) -> u128 {
    self.get(0)
  }

  /// The value at the top of the stack.
  #[inline(always)]
  pub(crate) const fn top(&self) -> u128 {
    self.get(self.depth - 1)
  }

  /// Replace the value at the top of the stack: whatever there is one (else the expression
  /// fails).
  #[must_use]
  #[inline(always)]
  pub(crate) const fn replace(&mut self, value: Option<u128>) -> bool {
    match value {
      Some(value) => {
        self.set(self.depth - 1, value);
        true
      }
      None => false,
    }
  }

  /// A machine to evaluate another expression, in which the variable `index` is `value` and the
  /// other ones are what they are here.
  #[inline(always)]
  pub(crate) const fn enter(&self, index: u8, value: u128) -> Self {
    let mut inner = Self { depth: 0, skip: 0, ..*self };
    inner.variables.set(index as usize, value);
    inner
  }

  /// Apply the operation `op`, at the index `at` of an expression, over the struct at `base`,
  /// whose size is `size`: `false` if it fails, which is then the failure of the expression.
  ///
  /// # Safety
  /// `op` must be an operation of an expression of a rule of the layout of a struct, and `base`
  /// must be valid for reads of the sized fields of such a struct, and of `size` bytes,
  /// initialized. `size` is the size of the struct, or for a `size` or `len` rule, which tells it,
  /// what can be read.
  // Not in debug builds, where this would only make every struct that has rules slow to build.
  #[must_use]
  #[cfg_attr(not(debug_assertions), inline(always))]
  pub(crate) const unsafe fn apply(
    &mut self,
    op: Op,
    at: usize,
    base: *const u8,
    size: usize,
  ) -> bool {
    if !self.computes(at) {
      return true;
    }
    let depth = self.depth;
    match op {
      Op::Lit(value) => self.push(value),
      Op::Load { offset, size, big } => {
        let endian = if big { Endian::Big } else { Endian::Little };
        // SAFETY: `check_rules` made sure that the field is in the sized part of the struct.
        self.push(unsafe { load(base.add(offset), size as usize, endian) });
      }
      Op::Size => self.push(size as u128),
      Op::Get(index) => self.push(self.variables.get(index as usize)),
      Op::Set(index) => {
        self.variables.set(index as usize, self.get(depth - 1));
        self.depth -= 1;
      }
      Op::Not => self.set(depth - 1, (self.get(depth - 1) == 0) as u128),
      Op::Varint { form, length } => {
        let offset = self.get(depth - 1);
        let value = if offset <= size as u128 {
          // SAFETY: in the bounds of the value.
          match unsafe { form.read(base.add(offset as usize), size - offset as usize) } {
            Some((value, bytes)) => Some(if length { bytes as u128 } else { value }),
            None => None,
          }
        } else {
          None
        };
        return self.replace(value);
      }
      Op::At { size: width, big } => {
        let (offset, endian) =
          (self.get(depth - 1), if big { Endian::Big } else { Endian::Little });
        if offset <= size as u128 && size - offset as usize >= width as usize {
          // SAFETY: in the bounds of the value.
          self.set(depth - 1, unsafe { load(base.add(offset as usize), width as usize, endian) });
        } else {
          return false;
        }
      }
      op @ (Op::Crc32
      | Op::Sum16
      | Op::Crc(_)
      | Op::CrcZeros(_)
      | Op::Checksum(_)
      | Op::Matches(_)) => {
        // The range, and the one taken as zeros (none but for `CrcZeros`).
        let operands = if matches!(op, Op::CrcZeros(_)) { 4 } else { 2 };
        let (start, end) = (self.get(depth - operands), self.get(depth - operands + 1));
        let (zeros, to) =
          if operands == 4 { (self.get(depth - 2), self.get(depth - 1)) } else { (0, 0) };
        self.depth -= operands - 1;
        if start <= end && end <= size as u128 && zeros <= to && to <= size as u128 {
          // SAFETY: in the bounds of the value.
          let bytes = unsafe {
            core::slice::from_raw_parts(base.add(start as usize), (end - start) as usize)
          };
          // Where the zeros are in `bytes` (in the value: they fit a `usize`).
          let zeros = (zeros.saturating_sub(start) as usize, to.saturating_sub(start) as usize);
          let value = match op {
            Op::Crc32 => crc32_inline(bytes) as u128,
            Op::Sum16 => sum16_inline(bytes) as u128,
            Op::Crc(crc) | Op::CrcZeros(crc) => crc.compute(bytes, zeros) as u128,
            Op::Checksum(checksum) => checksum.compute(bytes),
            Op::Matches(dfa) => dfa.accepts_bytes(bytes) as u128,
            _ => 0,
          };
          self.set(depth - operands, value);
        } else {
          return false;
        }
      }
      // The left side decides: it is the value, and the right side is not computed.
      Op::Shortcut { either, len } => {
        if (self.get(depth - 1) != 0) == either {
          self.set(depth - 1, either as u128);
          self.skip = at + 1 + len as usize;
        }
      }
      // The condition is false: the branch is not computed, a placeholder is its value.
      Op::Then(len) => {
        if self.get(depth - 1) == 0 {
          self.push(0);
          self.skip = at + 1 + len as usize;
        }
      }
      // The branch was computed: the `else` branch is not, a placeholder is its value.
      Op::Else(len) => {
        self.push(0);
        self.skip = at + 1 + len as usize;
      }
      // The left side did not decide (see `Op::Shortcut`): the right side is the value.
      Op::Both | Op::Either => {
        self.set(depth - 2, (self.get(depth - 1) != 0) as u128);
        self.depth -= 1;
      }
      // One of the values is a placeholder, and not the one that the condition selects.
      Op::Select => {
        let value =
          if self.get(depth - 3) != 0 { self.get(depth - 2) } else { self.get(depth - 1) };
        self.set(depth - 3, value);
        self.depth -= 2;
      }
      // `validate::walk` applies a walk, with the instances of the functions that apply its
      // expressions: the validation never gives one to this function (`validate::covers`).
      Op::Walk { .. } => return false,
      op => {
        match binary(op, self.get(depth - 2), self.get(depth - 1)) {
          Some(value) => self.set(depth - 2, value),
          None => return false,
        }
        self.depth -= 1;
      }
    }
    true
  }
}

// `walk_limit` adds 16 to the size of a value, at most `isize::MAX`: it does not overflow.
const _: () = assert!(isize::MAX as usize <= usize::MAX - 16);

/// So that a walk ends: a step for each bit of a value of `size` bytes, and for each bit of a
/// `u128`. (`size` is at most `isize::MAX`: adding 16 does not overflow.)
#[inline(always)]
pub(crate) const fn walk_limit(size: usize) -> usize {
  (size + 16).saturating_mul(8)
}

#[inline(always)]
const fn binary(op: Op, a: u128, b: u128) -> Option<u128> {
  match op {
    Op::Add => a.checked_add(b),
    Op::Sub => a.checked_sub(b),
    Op::Mul => a.checked_mul(b),
    Op::Div => a.checked_div(b),
    Op::Rem => a.checked_rem(b),
    Op::And => Some(a & b),
    Op::Or => Some(a | b),
    Op::Xor => Some(a ^ b),
    Op::Shl => {
      if b < 128 {
        Some(a << b)
      } else {
        None
      }
    }
    Op::Shr => {
      if b < 128 {
        Some(a >> b)
      } else {
        None
      }
    }
    Op::Eq => Some((a == b) as u128),
    Op::Ne => Some((a != b) as u128),
    Op::Lt => Some((a < b) as u128),
    Op::Le => Some((a <= b) as u128),
    Op::Gt => Some((a > b) as u128),
    Op::Ge => Some((a >= b) as u128),
    Op::Lit(_)
    | Op::Load { .. }
    | Op::Size
    | Op::Not
    | Op::At { .. }
    | Op::Varint { .. }
    | Op::Crc32
    | Op::Sum16
    | Op::Crc(_)
    | Op::CrcZeros(_)
    | Op::Checksum(_)
    | Op::Matches(_)
    | Op::Both
    | Op::Either
    | Op::Select
    | Op::Set(_)
    | Op::Get(_)
    | Op::Shortcut { .. }
    | Op::Then(_)
    | Op::Else(_)
    | Op::Walk { .. } => None,
  }
}

/// The CRC-32 of Ethernet (and zlib, PNG...): the frame check sequence of a frame is the CRC of
/// what is before it, least significant byte first.
///
/// ```
/// assert_eq!(puns::crc32(b"123456789"), 0xcbf43926);
/// ```
#[must_use]
pub const fn crc32(bytes: &[u8]) -> u32 {
  crc32_inline(bytes)
}

/// [`crc32`], inlined where a rule computes it.
#[inline(always)]
const fn crc32_inline(bytes: &[u8]) -> u32 {
  // Slicing by 16, as `Crc`, with tables of `u32`s (16 KiB, built at compile time). Without
  // carry-less multiplication, which a `const fn` cannot reach on stable Rust, it is the fastest.
  const TABLES: [[u32; 256]; 16] = {
    let mut tables = [[0u32; 256]; 16];
    let mut i = 0;
    while i < 256 {
      let (mut value, mut bit) = (i as u32, 0);
      while bit < 8 {
        value = if value & 1 != 0 { (value >> 1) ^ 0xedb8_8320 } else { value >> 1 };
        bit += 1;
      }
      tables[0][i] = value;
      i += 1;
    }
    let mut k = 1;
    while k < 16 {
      let mut i = 0;
      while i < 256 {
        let value = tables[k - 1][i];
        tables[k][i] = tables[0][(value & 0xff) as usize] ^ (value >> 8);
        i += 1;
      }
      k += 1;
    }
    tables
  };
  let (blocks, rest) = bytes.as_chunks::<16>();
  let (t, mut crc, mut i) = (&TABLES, !0u32, 0);
  while i < blocks.len() {
    let b = &blocks[i];
    let x = (crc ^ u32::from_le_bytes([b[0], b[1], b[2], b[3]])).to_le_bytes();
    crc = t[15][x[0] as usize]
      ^ t[14][x[1] as usize]
      ^ t[13][x[2] as usize]
      ^ t[12][x[3] as usize]
      ^ t[11][b[4] as usize]
      ^ t[10][b[5] as usize]
      ^ t[9][b[6] as usize]
      ^ t[8][b[7] as usize]
      ^ t[7][b[8] as usize]
      ^ t[6][b[9] as usize]
      ^ t[5][b[10] as usize]
      ^ t[4][b[11] as usize]
      ^ t[3][b[12] as usize]
      ^ t[2][b[13] as usize]
      ^ t[1][b[14] as usize]
      ^ t[0][b[15] as usize];
    i += 1;
  }
  let mut i = 0;
  while i < rest.len() {
    crc = t[0][((crc ^ rest[i] as u32) & 0xff) as usize] ^ (crc >> 8);
    i += 1;
  }
  !crc
}

/// The ones' complement sum of the big-endian 16 bits words of `bytes`.
///
/// A last byte alone is the high byte of a word. The checksums of IP, UDP and TCP are the
/// complement of this sum: over data that holds its own checksum, it is `0xffff`.
///
/// ```
/// assert_eq!(puns::sum16(&[0x00, 0x01, 0xf2, 0x03, 0xf4, 0xf5, 0xf6, 0xf7]), 0xddf2);
/// ```
#[must_use]
pub const fn sum16(bytes: &[u8]) -> u16 {
  sum16_inline(bytes)
}

/// The blocks of 8 bytes that `sum16` adds before it folds: 2^20, or all of them where a `usize`
/// does not count that far (a 16-bit target: at most 2^12 blocks, far from a lost carry).
const SUM16_FOLD: usize = if usize::BITS > 20 { 1 << (20 % usize::BITS) } else { usize::MAX };

// A block adds less than 2^33 to the sum: `SUM16_FOLD` of them never carry out of a `u64`.
const _: () = assert!((SUM16_FOLD as u128) << 33 < 1 << 64);

/// [`sum16`], inlined where a rule computes it.
#[inline(always)]
const fn sum16_inline(bytes: &[u8]) -> u16 {
  // Eight bytes at a time, in the byte order of the target, as two halves of 32 bits added into a
  // `u64` (RFC 1071): words of the other byte order sum to the sum with its bytes swapped, which
  // is swapped back. Folded every 2^20 blocks, long before a carry could be lost (2^31). As the
  // sum word by word folded at each step: `0` for no or only zero bytes, else the value of
  // `1..=0xffff` that is congruent to the sum modulo `0xffff`.
  let (mut blocks, rest) = bytes.as_chunks::<8>();
  let mut sum = 0u64;
  while !blocks.is_empty() {
    let (now, later) =
      blocks.split_at(if blocks.len() > SUM16_FOLD { SUM16_FOLD } else { blocks.len() });
    let (mut wide, mut i) = (0u64, 0);
    while i < now.len() {
      let block = u64::from_ne_bytes(now[i]);
      wide += (block & 0xffff_ffff) + (block >> 32);
      i += 1;
    }
    sum = fold(sum + fold(wide) as u64) as u64;
    blocks = later;
  }
  if cfg!(target_endian = "little") {
    sum = (sum as u16).swap_bytes() as u64;
  }
  // What remains, big-endian: fewer than 8 bytes, from an even offset.
  let mut i = 0;
  while i < rest.len() {
    let low = if i + 1 < rest.len() { rest[i + 1] } else { 0 };
    sum += ((rest[i] as u64) << 8) | low as u64;
    i += 2;
  }
  fold(sum)
}

/// A sum of 16 bits words, folded into 16 bits with end-around carries.
#[inline(always)]
const fn fold(mut sum: u64) -> u16 {
  while sum > 0xffff {
    sum = (sum & 0xffff) + (sum >> 16);
  }
  sum as u16
}

/// Bounds of a range of valid values of an integer of the size of `T`, for `#[pun(valid(..))]`.
#[doc(hidden)]
#[must_use]
pub const fn range<T>(start: Option<u128>, end: Option<u128>, inclusive: bool) -> (u128, u128) {
  let start = match start {
    Some(start) => start,
    None => 0,
  };
  let end = match end {
    Some(end) if inclusive => end,
    Some(end) => {
      assert!(end > start, "empty range");
      end - 1
    }
    None => max_value(size_of::<T>()),
  };
  assert!(start <= end, "empty range");
  (start, end)
}

/// Sort and merge inclusive ranges; returns the ranges and their count.
#[doc(hidden)]
#[must_use]
pub const fn merge_ranges<const N: usize>(
  mut ranges: [(u128, u128); N],
) -> ([(u128, u128); N], usize) {
  let mut i = 1;
  while i < N {
    let mut j = i;
    while j > 0 && ranges[j - 1].0 > ranges[j].0 {
      ranges.swap(j - 1, j);
      j -= 1;
    }
    i += 1;
  }
  let (mut merged, mut len, mut i) = ([(0, 0); N], 0, 0);
  while i < N {
    if len > 0 && (ranges[i].0 <= merged[len - 1].1 || ranges[i].0 - merged[len - 1].1 == 1) {
      if ranges[i].1 > merged[len - 1].1 {
        merged[len - 1].1 = ranges[i].1;
      }
    } else {
      merged[len] = ranges[i];
      len += 1;
    }
    i += 1;
  }
  (merged, len)
}

/// The bits of a value of at most 16 bytes, as a native-endian unsigned integer.
///
/// # Safety
/// Every byte of `value` must be initialized.
#[doc(hidden)]
pub const unsafe fn bits_of<T>(value: &T) -> u128 {
  assert!(size_of::<T>() <= 16, "a fieldless enum cannot be larger than 16 bytes");
  // SAFETY: guaranteed by the caller.
  unsafe { load(core::ptr::from_ref::<T>(value).cast(), size_of::<T>(), Endian::NATIVE) }
}