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
// SPDX-License-Identifier: MPL-2.0
//
// Part of Auguth Labs open-source softwares.
// Built for the Substrate framework.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2026 Auguth Labs (OPC) Pvt Ltd, India
// ===============================================================================
// ```````````````````````````````` VIRTUALS SUITE ```````````````````````````````
// ===============================================================================
//! Virtual struct system mapping discriminants (ZSTs) to behavior via
//! trait-driven schemas.
//!
//! Traditional Rust data structures require committing upfront to:
//! - a fixed shape (`Option<T>`, `Vec<T>`, `[T; N]`, struct fields)
//! - a fixed cardinality (single vs multiple)
//! - a fixed storage layout
//!
//! This makes evolution difficult:
//! - structure and storage are tightly coupled
//! - generic abstractions often hit coherence limits
//! - external composition (plugins, schemas) is hard
//! - changing a field (`None -> Some -> Many`) requires redesign
//!
//! This module introduces a system where **structure, interpretation,
//! and storage are decoupled**, and resolved through types.
//!
//! ## Mental Model: Virtual Structs
//!
//! Think in terms of a **virtual struct**, whose fields are not stored
//! directly, but defined through traits and discriminants:
//!
//! ```ignore
//! struct <T as Trait>::Struct<K> {
//! FieldTag: T::Field, // virtual field (identified by discriminant)
//! ExtTag: K, // virtual extension (identified by discriminant)
//! }
//! ```
//!
//! This is not a concrete struct. Instead:
//!
//! - fields are accessed via trait implementations
//! - each field is identified by a **discriminant (type-level key)**
//! - storage is abstracted or external
//!
//! ## Discriminants (Field Identifiers)
//!
//! ```ignore
//! struct FieldTag;
//! struct ExtTag;
//! ```
//!
//! Discriminants are zero-sized types used as **type-level field keys**.
//!
//! They act as:
//! - field identifiers in the virtual struct
//! - selectors for behavior and interpretation
//! - disambiguators for generic implementations
//!
//! This ensures:
//! - multiple fields can coexist without ambiguity
//! - overlapping generic impls remain coherence-safe
//!
//! ## Trait as Schema
//!
//! ```ignore
//! pub trait Trait {
//! type Struct: VirtualDynField<FieldTag, Some = Self::Field>
//! + VirtualDynExtension<ExtTag>;
//!
//! type Field;
//! }
//! ```
//!
//! - `Struct` is the container (virtual struct)
//! - `FieldTag` identifies a logical field
//! - `ExtTag` identifies an extension slot
//! - `Field` defines the logical type of the field
//!
//! Traits define the **schema**, not the storage.
//!
//! ## Field Behavior (Cardinality Abstraction)
//!
//! A virtual field supports:
//!
//! - `None` - no value
//! - `Some` - one value
//! - `Many` - multiple values
//!
//! This replaces fixed representations like:
//!
//! ```ignore
//! Option<T> / Vec<T>
//! ```
//!
//! with a single abstraction that can evolve without redesign.
//!
//! ## Key Insight
//!
//! A virtual struct is not a fixed layout, but a **composition of
//! discriminant-keyed behaviors**, where:
//!
//! - **discriminants** -> field identifiers
//! - **traits** -> schema (what exists)
//! - **implementations** -> storage (how/where it exists)
//!
//! Structure is resolved by types, not encoded directly.
//!
//! ## Core Primitives
//!
//! The system is built from:
//!
//! ### Virtual Fields
//! - [`VirtualDynField`] - dynamic, vector-like semantics
//! - [`VirtualStaticField`] - static, array-like semantics
//!
//! ### Virtual Extensions
//! - [`VirtualDynExtension`] / [`VirtualStaticExtension`]
//! - externally defined fields via schemas
//!
//! ### Schemas & Bounds
//! - [`VirtualDynBound`] / [`VirtualStaticBound`]
//! - constraints defined independently of storage
//!
//! ### Concrete Representations
//! - [`SumDynType`] - bounded vector semantics (`None | Some | Many`)
//! - [`SumStaticType`] - fixed array semantics
//!
//! ## Design Principles
//!
//! ### Discriminant-Keyed Design
//!
//! All components are keyed by a [`DiscriminantTag`].
//!
//! This:
//! - avoids ambiguity in generic implementations
//! - enables multiple independent fields on the same container
//! - ensures coherence-safe extensibility
//!
//! ### Tagged Conversions
//!
//! Instead of `From` / `Into`, the system uses:
//!
//! - [`FromTag`], [`IntoTag`]
//! - [`TryFromTag`], [`TryIntoTag`]
//!
//! Conversions are disambiguated by discriminants:
//! - [`NoneTag`] - absence
//! - [`SomeTag`] - single value
//! - [`ManyTag`] - multiple values
//!
//! This avoids overlapping implementations in generic contexts.
//!
//! ### Layered Model
//!
//! The system separates:
//!
//! - **Type-level layer**
//! - defines structure, schema, and behavior
//!
//! - **Value-level layer**
//! - represents `None`, `Some`, `Many`
//!
//! This enables abstract structure with concrete representations.
//!
//! ### Dynamic vs Static
//!
//! #### Dynamic (Runtime Flexible)
//! - vector-like semantics
//! - runtime bounds (`Get<u32>`)
//! - growable/shrinkable collections
//!
//! #### Static (Compile-Time Fixed)
//! - array-like semantics
//! - compile-time bounds (`const`)
//! - zero-overhead representations
//!
//! ## Helpers
//!
//! Ergonomic helpers are provided for working with virtual components:
//!
//! - [`DynFieldHelpers`] / [`StaticFieldHelpers`]
//! - [`DynExtHelpers`] / [`StaticExtHelpers`]
//!
//! ## Summary
//!
//! This system enables:
//!
//! - evolving data shapes without redesign
//! - discriminant-keyed field composition
//! - external composition via schemas and extensions
//! - reuse of storage across multiple logical fields
//! - coherence-safe generic abstractions
//!
//! It provides a **type-level virtualization layer** where:
//!
//! > A virtual struct is a mapping from **discriminants -> behaviors**,
//! > resolved through traits and implemented via storage.
// ===============================================================================
// ``````````````````````````````````` IMPORTS ```````````````````````````````````
// ===============================================================================
// --- Local crate imports ---
use crate;
// --- Core (Rust std replacement) ---
use ;
// --- Scale-codec crates ---
use ;
use TypeInfo;
// --- FRAME Support ---
use ;
// --- Substrate primitives ---
use ;
use ;
use vec;
// ===============================================================================
// ```````````````````````````````` DISCRIMINANTS ````````````````````````````````
// ===============================================================================
/// Marker trait for type-level discriminants.
///
/// A `Discriminant` is a zero-sized type used to uniquely identify behavior,
/// structure, or interpretation at the type level.
///
/// ## Guidelines
///
/// Implementors should:
/// - be zero-sized types (ZST)
/// - carry no runtime data
/// - act purely as type-level identifiers
///
/// ## Motivation
///
/// In Rust, trait implementations involving generic or associated types can
/// become ambiguous under the coherence rules:
///
/// - generic parameters may unify in unexpected ways
/// - multiple impls may overlap when types are not fully concrete
/// - the compiler must conservatively reject such cases
///
/// To avoid this, a **concrete type-level key** is introduced as a discriminant.
///
/// By adding a `Discriminant`:
/// - each implementation becomes uniquely identifiable
/// - ambiguity between generic impls is disposed
/// - coherence is preserved without restricting expressiveness
///
/// ## Role in the System
///
/// Discriminants are used in some cases like
/// - keys for [`VirtualDynField`]
/// - identifiers for [`plugin`](crate::plugins) operations
/// - selectors for tagged conversions ([`FromTag`], [`IntoTag`], etc.)
///
/// They allow multiple interpretations over the same underlying types
/// without conflict.
/// Default Discriminant implementation if no
/// multiple interpretations are required.
/// Defines one or more public zero-sized discriminant types.
///
/// A *discriminant* is a concrete type-level key used to uniquely identify
/// behavior, structure, or interpretation in generic systems.
///
/// ## Why Discriminants?
///
/// In Rust, trait implementations over generic or associated types can become
/// ambiguous under coherence rules:
/// - generic types may unify in multiple ways
/// - implementations may overlap when types are not fully concrete
/// - the compiler must reject such cases conservatively
///
/// Discriminants solve this by introducing a **concrete, unique type**
/// that disambiguates otherwise overlapping implementations.
///
/// This enables:
/// - multiple interpretations over the same types
/// - safe composition of generic abstractions
/// - coherence-safe extensibility
///
/// ## Syntax
///
/// ```ignore
/// discriminants!(
/// /// Optional docs or attributes
/// A,
///
/// B,
///
/// #[cfg(feature = "x")]
/// C,
/// );
/// ```
///
/// ## Expansion
///
/// For each identifier, this macro generates:
/// - a `pub` zero-sized struct
/// - an implementation of [`DiscriminantTag`]
///
/// ## Properties
///
/// - zero runtime cost (ZSTs)
/// - purely type-level identifiers
/// - stable and unambiguous across generic contexts
/// - reusable across virtual fields, extensions, and
/// [`plugin`](crate::plugins) systems
///
/// ## Why Always Public
///
/// Discriminants appear in type signatures and generic bounds,
/// making them part of the public type-level API.
///
/// Restricting visibility would:
/// - prevent use in external generic constraints
/// - break composability across modules or crates
/// - force duplication of identical identifiers
///
/// In practice, discriminants are **type-level contracts**, not internal details.
)*
};
}
/// Implements [`DiscriminantTag`] for one or more existing types.
///
/// This macro allows pre-existing types to act as discriminants without
/// redefining them.
///
/// ## Why Discriminants?
///
/// Discriminants provide a **concrete type-level key** that avoids ambiguity
/// in generic trait resolution under Rust's coherence rules.
///
/// By associating behavior with a discriminant instead of relying solely
/// on generic types, implementations become:
/// - uniquely identifiable
/// - non-overlapping
/// - composable across abstraction boundaries
///
/// ## When to Use
///
/// Use this macro when:
/// - a type already exists and should act as a discriminant
/// - redefining it as a new ZST would be redundant or impossible
///
/// ## Syntax
///
/// ```ignore
/// struct MyTag;
/// struct OtherTag;
///
/// impl_discriminants!(MyTag, OtherTag);
/// ```
///
/// ## Behavior
///
/// - Implements [`DiscriminantTag`] for each provided type
/// - Preserves the original type definition
///
/// ## Notes
///
/// - Types are expected to behave like discriminants (typically ZSTs)
/// - No runtime guarantees are enforced; this is a semantic contract
///
/// ## Summary
///
/// This macro extends the discriminant system to existing types,
/// enabling reuse and integration without redefining identifiers.
;
}
// ===============================================================================
// ````````````````````` FROM/INTO DISCRIMINANTED CONVERSIONS ````````````````````
// ===============================================================================
/// Converts a value `T` into `Self` under a given discriminant.
///
/// Unlike [`From`], this trait introduces an additional type parameter
/// (`Discriminant` implementing [`DiscriminantTag`] via
/// [`discriminants`] or [`impl_discriminants`])
/// to distinguish between conversions that would otherwise overlap under
/// Rust's coherence rules.
///
/// This is necessary because:
/// - `T` and `Self` may be generic or associated types (i.e., not fully concrete)
/// - such types may unify in the future
/// - the compiler must conservatively reject potentially overlapping impls
///
/// By adding a concrete discriminant (tag), each conversion becomes uniquely
/// identifiable at the type level.
///
/// ## Type Parameters
/// - `T`: Source type (may be generic or non-concrete).
/// - `Discriminant`: A concrete marker type used to disambiguate conversions.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: uses the unit type as a default tag,
/// meaning no additional disambiguation when a single interpretation exists.
/// Fallible version of [`FromTag`].
///
/// Allows conversions that may fail, while still being disambiguated by a
/// concrete discriminant.
///
/// ## Type Parameters
/// - `T`: Source type (may be generic or non-concrete).
/// - `Discriminant`: A concrete marker type used to disambiguate conversions.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: uses the unit type as a default tag,
/// meaning no additional disambiguation when a single interpretation exists.
/// Converts `self` into another representation under a given discriminant.
///
/// This is the method-based counterpart to [`FromTag`]. It exists for ergonomic
/// use, similar to how [`Into`] complements [`From`].
///
/// The additional discriminant ensures that conversions remain unambiguous even
/// when source and target types are not fully concrete.
///
/// ## Type Parameters
/// - `R`: Target type (may be generic or non-concrete).
/// - `Discriminant`: A concrete marker type used to disambiguate conversions.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: uses the unit type as a default tag,
/// meaning no additional disambiguation when a single interpretation exists.
/// Fallible version of [`IntoTag`].
///
/// Attempts to convert `self` into another representation under a given
/// discriminant, returning an error if the conversion fails.
///
/// ## Type Parameters
/// - `R`: Target type (may be generic or non-concrete).
/// - `Discriminant`: A concrete marker type used to disambiguate conversions.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: uses the unit type as a default tag,
/// meaning no additional disambiguation when a single interpretation exists.
/// Blanket implementation of [`IntoTag`] for any type that implements [`FromTag`].
///
/// This mirrors the relationship between [`Into`] and [`From`], while preserving
/// disambiguation via the discriminant.
///
/// The discriminant ensures that even when `T` and `U` are not fully concrete,
/// the conversion remains uniquely identifiable and does not violate coherence.
// ===============================================================================
// `````````````````````````````````` SUM TYPES ``````````````````````````````````
// ===============================================================================
discriminants!
/// A concrete representation of field cardinality: zero, one, or many values.
///
/// `SumDynType` unifies the possible shapes of a field into a single type:
/// - absence (`None`)
/// - a single value (`Some`)
/// - multiple values (`Many`)
///
/// ## Context
///
/// In the virtual field system:
/// - [`VirtualDynField`] defines field behavior abstractly (type-level)
/// - `SumDynType` provides a concrete, value-level representation
///
/// It is commonly used as the backing representation (`Repr`) for
/// dynamically shaped fields.
///
/// ## Representation Model
///
/// The `Many` variant is backed by a [`BoundedVec`], giving it
/// **vector-like semantics**:
/// - dynamically sized (up to a bound)
/// - growable and shrinkable
/// - capacity enforced via a type-level limit
///
/// This makes `SumDynType` suitable for:
/// - flexible schemas
/// - deferred structure
/// - abstraction across boundaries where size is not fixed
///
/// ## Variants
/// - `None`: no value
/// - `Some(Type)`: exactly one value
/// - `Many(BoundedVec<Type, S>)`: multiple values with vector semantics
///
/// ## Type Parameters
/// - `Type`: element type
/// - `S`: type-level capacity bound
///
/// ## Key Property
///
/// This is a **concrete (non-virtual) representation** using
/// **bounded vector semantics**, allowing flexible cardinality
/// within a constrained capacity.
///
/// ## Default Generics
///
/// - `Type = ()`: no meaningful value (unit type)
/// - `S = ConstU32<0>`: zero capacity, `Many` cannot store elements
///
/// Together, this yields a **no-op, zero-capacity representation**,
/// useful as a placeholder in generic contexts.
/// A statically shaped representation of field cardinality.
///
/// `SumStaticType` encodes the possible shapes of a field:
/// - absence (`None`)
/// - a single value (`Some`)
/// - multiple values (`Many`)
///
/// ## Context
///
/// In the virtual field system:
/// - [`VirtualStaticField`] defines fields whose structure is
/// fully determined at compile time
/// - `SumStaticType` provides a matching concrete representation
///
/// ## Representation Model
///
/// The `Many` variant is backed by a fixed-size array (`[Type; N]`),
/// giving it **array-like semantics**:
/// - size is fixed at compile time
/// - no resizing or allocation
/// - capacity is encoded directly in the type
///
/// This makes `SumStaticType` suitable for:
/// - compile-time enforced layouts
/// - fixed schemas
/// - zero-overhead representations
///
/// ## Variants
/// - `None`: no value
/// - `Some(Type)`: exactly one value
/// - `Many([Type; N])`: fixed-size collection with array semantics
///
/// ## Type Parameters
/// - `Type`: element type
/// - `N`: compile-time capacity
///
/// ## Key Property
///
/// This is a **fully determined representation** using
/// **array semantics**, where both cardinality and capacity
/// are encoded directly in the type.
///
/// ## Default Generics
///
/// - `Type = ()`: no meaningful value (unit type)
/// - `N = 0`: zero-sized array, `Many` holds no elements
///
/// Together, this yields a **no-op, zero-sized representation**,
/// useful as a placeholder in generic contexts.
// ===============================================================================
// ```````````````````````````````` VIRTUAL FIELDS ```````````````````````````````
// ===============================================================================
/// A discriminant-keyed virtual field abstraction with flexible cardinality
/// (`None`, `Some`, or `Many`).
///
/// This trait models a field whose structure is **deferred** and resolved
/// through types rather than fixed upfront.
///
/// ## Model
///
/// A `VirtualDynField` behaves like a field in a logical record, without
/// committing to:
///
/// - a concrete container (`Option`, `Vec`, etc.)
/// - a fixed cardinality
/// - or a fixed storage layout
///
/// Instead:
/// - the **implementor** defines the backing representation (`Repr`)
/// - the **caller** selects the shape (`None`, `Some`, or `Many`)
///
/// ## Representation Semantics
///
/// The `Many` form is expected to have **vector-like semantics**:
/// - dynamically sized (within bounds)
/// - growable and shrinkable
/// - capacity enforced but not encoded in the type shape
///
/// This makes `VirtualDynField` suitable for:
/// - flexible schemas
/// - deferred structure
/// - abstraction across boundaries
///
/// ## Discriminant
///
/// The `Discriminant` acts as a type-level key, allowing multiple independent
/// fields to coexist without ambiguity.
///
/// ## Design
///
/// Responsibilities are separated:
///
/// - **Implementor (Storage Layer)**
/// - defines representation (`Repr`)
/// - **Caller (Shape Layer)**
/// - selects cardinality and interpretation
///
/// This decouples logical meaning from physical storage.
///
/// ## When to Use
///
/// Use this trait when:
/// - structure must remain flexible
/// - cardinality is not known upfront
/// - storage must be abstract and composable
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: uses the unit type as a single field key,
/// meaning only one virtual field exists (no disambiguation needed).
/// Trivial `None` conversion for unit.
/// Trivial `Some` conversion for unit.
/// Trivial `Many` conversion for unit (always succeeds).
/// Trivial extraction of `None` from unit.
/// Trivial extraction of `Some` from unit.
/// Converts unit into an empty `Many` representation.
/// No-op `VirtualDynField` implementation for unit.
///
/// Represents an allocation with no storage and zero capacity.
// ===============================================================================
// ```````````````````````` VIRTUAL FIELD DEFAULT-HELPERS ````````````````````````
// ===============================================================================
/// A discriminant-keyed virtual field abstraction with statically
/// determined cardinality.
///
/// This trait models a field whose structure is **fully determined
/// at compile time**, rather than deferred.
///
/// ## Model
///
/// A `VirtualStaticField` behaves like a field in a logical record,
/// where:
///
/// - cardinality is fixed or constrained at compile time
/// - storage shape is predetermined
/// - no dynamic resizing or growth is expected
///
/// Similar to [`VirtualDynField`] (but not dynamically sized):
/// - the **implementor** defines the backing representation (`Repr`)
/// - the **caller** selects the interpretation (`None`, `Some`, `Many`)
///
/// ## Representation Semantics
///
/// The `Many` form is expected to have **array-like semantics**:
/// - fixed size
/// - no resizing or allocation
/// - capacity encoded directly in the type
///
/// This makes `VirtualStaticField` suitable for:
/// - compile-time enforced layouts
/// - fixed schemas
/// - zero-overhead representations
///
/// ## Discriminant
///
/// The `Discriminant` acts as a type-level key, allowing multiple independent
/// fields to coexist without ambiguity.
///
/// ## Design
///
/// As with dynamic fields, responsibilities are separated:
///
/// - **Implementor (Storage Layer)**
/// - defines representation (`Repr`)
/// - **Caller (Shape Layer)**
/// - selects cardinality and interpretation
///
/// The key difference is that structure is **not deferred**, but
/// fully determined at compile time.
///
/// ## When to Use
///
/// Use this trait when:
/// - structure is known and fixed upfront
/// - dynamic resizing is unnecessary or undesirable
/// - compile-time guarantees are preferred over flexibility
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: uses the unit type as a single field key,
/// meaning only one virtual field exists (no disambiguation needed).
/// Trivial `Many` conversion for unit using a zero-sized array.
/// Trivial extraction of `Many` as a zero-sized array from unit.
/// No-op `VirtualStaticField` implementation for unit.
///
/// Represents an allocation with no storage and zero capacity.
/// Helper methods for accessing and mutating values in a [`VirtualDynField`].
///
/// These helpers operate on **dynamically shaped fields** with
/// **vector-like semantics**:
/// - collections may grow or shrink (within bounds)
/// - indexing and iteration are supported
/// - mutations may reallocate or fail due to bounds
///
/// All operations are performed via tagged conversions.
///
/// ## Default Discriminant
///
/// - `K = ()`: operates on a single default field,
/// meaning one virtual field is assumed.
/// Blanket impl for all [`VirtualDynField`] types.
///
/// This trait is not intended to be implemented manually.
/// It exists as an ergonomic replacement for free helper functions.
///
/// All methods have default implementations, making this
/// forward-compatible: new helpers can be added without
/// breaking existing code.
/// Helper methods for accessing and mutating values in a [`VirtualStaticField`].
///
/// These helpers operate on **statically shaped fields** with
/// **array-like semantics**:
/// - collection size is fixed at compile time
/// - no resizing or extension is performed
/// - operations replace or read the entire structure
///
/// All operations are performed via tagged conversions.
///
/// ## Default Discriminant
///
/// - `K = ()`: operates on a single default field,
/// meaning one virtual static field is assumed.
/// Blanket impl for all [`VirtualStaticField`] types.
///
/// This trait is not intended to be implemented manually.
/// It exists as an ergonomic replacement for free helper functions.
///
/// All methods have default implementations, making this
/// forward-compatible: new helpers can be added without
/// breaking existing code.
// ===============================================================================
// ````````````````````````````` VIRTUAL FIELD-BOUNDS ````````````````````````````
// ===============================================================================
/// Provides the bounds associated with a [`VirtualDynField<Discriminant>`].
///
/// `VirtualDynBound` defines constraints (such as capacity limits)
/// without requiring the field itself to hardcode them.
///
/// ## Representation
///
/// The bound is provided as a type implementing [`Get<u32>`],
/// meaning the value is **resolved at runtime (or via type-level indirection)**.
///
/// This enables flexible, dynamically bounded behavior while still
/// enforcing limits.
///
/// ## Discriminant
///
/// The `Discriminant` links a field to its corresponding bound,
/// allowing multiple independent fields to coexist without ambiguity.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: provides a single default bound,
/// meaning only one dynamically bounded field is assumed.
/// No-op bound with zero capacity.
/// Provides the bounds associated with a [`VirtualStaticField<Discriminant>`].
///
/// `VirtualStaticBound` defines constraints (such as capacity limits)
/// that are **fully determined at compile time**.
///
/// ## Representation
///
/// The bound is provided as a `const`, meaning:
/// - it is a **compile-time constant**
/// - no runtime resolution or indirection is involved
///
/// This enables fully static, zero-overhead representations.
///
/// ## Discriminant
///
/// The `Discriminant` links a field to its corresponding bound,
/// allowing multiple independent fields to coexist without ambiguity.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: provides a single default bound,
/// meaning only one dynamically bounded field is assumed.
/// No-op bound provided with zero capacity.
// ===============================================================================
// ```````````````````````````````` VIRTUAL ERRORS ```````````````````````````````
// ===============================================================================
/// Defines the error type associated with a virtual component
/// identified by a `Discriminant`.
///
/// `VirtualError` provides a way to associate a specific error type
/// with a [`VirtualDynField`] or related abstraction, without hardcoding
/// the error into the implementation.
///
/// ## Discriminant
///
/// The `Discriminant` acts as a key linking a virtual field (or related
/// abstraction) to its corresponding error type.
///
/// This ensures multiple independent virtual components can coexist
/// without ambiguity.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: associates a single default error type,
/// meaning only one virtual component is assumed.
// ===============================================================================
// ``````````````````````````` DELEGATED VIRTUAL BOUNDS ``````````````````````````
// ===============================================================================
/// Delegates bound resolution to an external [`VirtualDynBound`] provider.
///
/// This trait is used when a type participates in a [`VirtualDynField`]
/// but does not define or own its bounds.
///
/// Instead, bounds are supplied externally via `Provider`,
/// allowing constraints (such as capacity) to be defined independently
/// of the field or its storage.
///
/// ## Representation
///
/// The delegated bound is a **runtime-resolved value** (via [`Get<u32>`]),
/// enabling flexible, dynamically bounded behavior.
///
/// ## Roles
///
/// - **Container (`Self`)**
/// - provides storage for the field
///
/// - **Bounds (`Provider`)**
/// - supplies constraints for that field
///
/// This separation enables:
/// - composability
/// - reuse across contexts
/// - decoupling of storage and constraints
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: delegates a single default bound,
/// meaning one dynamically bounded field is assumed.
/// Blanket implementation enabling all types to delegate
/// dynamic bound resolution to a [`VirtualDynBound`] provider.
/// Delegates bound resolution to an external [`VirtualStaticBound`] provider.
///
/// This trait is used when a type participates in a [`VirtualStaticField`]
/// but does not define or own its bounds.
///
/// Instead, bounds are supplied externally via `Provider`,
/// allowing constraints (such as capacity) to be defined independently
/// of the field or its storage.
///
/// ## Representation
///
/// The delegated bound is a **compile-time constant** (`usize`),
/// enabling fully static, zero-overhead representations.
///
/// ## Roles
///
/// - **Container (`Self`)**
/// - provides storage for the field
///
/// - **Bounds (`Provider`)**
/// - supplies compile-time constraints for that field
///
/// This separation enables:
/// - composability
/// - reuse across contexts
/// - decoupling of storage and constraints
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: delegates a single default bound,
/// meaning one statically bounded field is assumed.
/// Blanket implementation enabling all types to delegate
/// static bound resolution to a [`VirtualStaticBound`] provider.
// ===============================================================================
// ````````````````````` DELEGATED VIRTUAL FIELDS AND BOUNDS `````````````````````
// ===============================================================================
/// Constraint describing a virtual field whose bounds are delegated
/// to an external [`VirtualDynBound`] provider.
///
/// This composes:
/// - [`VirtualDynField`] - defines storage and representation
/// - [`DelegateVirtualDynBound`] - supplies bounds externally
///
/// ## Semantics
///
/// - **Container (`Self`)**
/// - provides storage for values of type `T`
///
/// - **Bounds (`Provider`)**
/// - supplies capacity constraints
///
/// - **Caller**
/// - selects field shape (`None`, `Some`, `Many`)
///
/// ## Representation
///
/// Bounds are resolved dynamically:
/// - provided via [`Get<u32>`]
/// - enable flexible, vector-like behavior within limits
///
/// This allows storage and constraints to remain decoupled.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: applies a single default bounded field,
/// meaning one dynamically bounded field is assumed.
/// Blanket implementation for any compatible container.
/// Constraint describing a virtual field whose bounds are delegated
/// to an external [`VirtualStaticBound`] provider.
///
/// This composes:
/// - [`VirtualStaticField`] - defines storage and representation
/// - [`DelegateVirtualStaticBound`] - supplies bounds externally
///
/// ## Semantics
///
/// - **Container (`Self`)**
/// - provides storage for values of type `T`
///
/// - **Bounds (`Provider`)**
/// - supplies compile-time capacity constraints
///
/// - **Caller**
/// - selects field shape (`None`, `Some`, `Many`)
///
/// ## Representation
///
/// Bounds are compile-time constants:
/// - encoded directly in the type system (`usize`)
/// - enable fixed-size, array-like behavior
///
/// This ensures fully static, zero-overhead representations.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: applies a single default bounded field,
/// meaning one statically bounded field is assumed.
/// Blanket implementation for any compatible container.
// ===============================================================================
// `````````````````````````````` VIRTUAL EXTENSIONS `````````````````````````````
// ===============================================================================
/// Defines the schema for a [`VirtualDynExtension`] identified by a `Discriminant`.
///
/// A `VirtualDynExtensionSchema` describes the **structure and representation**
/// of an extension field whose type is supplied externally.
///
/// Unlike [`VirtualDynField`], the element type and layout are not defined
/// by the container, but provided through this schema.
///
/// ## Context
///
/// In the virtual system:
/// - [`VirtualDynField`] defines fields with internally known types
/// - `VirtualDynExtensionSchema` defines fields with externally supplied types
/// - [`VirtualDynExtension`] stores values using this schema
///
/// This allows containers to support fields whose types are:
/// - not known at implementation time
/// - injected via type-level composition
///
/// ## Representation
///
/// The `Many` form is expected to have **vector-like semantics**:
/// - dynamically sized (within bounds)
/// - supports buffering and indexed access
///
/// The schema itself is purely type-level:
/// - it does not store data
/// - it defines how data is represented and interpreted
///
/// ## Discriminant
///
/// The `Discriminant` links:
/// - the extension storage
/// - to its schema
///
/// allowing multiple independent extensions to coexist safely.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: defines a single default extension schema,
/// meaning only one extension is assumed.
/// Defines the schema for a [`VirtualStaticExtension`] identified by a `Discriminant`.
///
/// A `VirtualStaticExtensionSchema` describes the **structure and representation**
/// of an extension field whose type is supplied externally and fully
/// determined at compile time.
///
/// ## Context
///
/// In the static field model:
/// - [`VirtualStaticField`] defines fields with fixed structure
/// - `VirtualStaticExtensionSchema` defines externally supplied types and layout
/// - [`VirtualStaticExtension`] stores values using this schema
///
/// This allows containers to support externally defined fields
/// with compile-time determined structure.
///
/// ## Representation
///
/// The `Many` form is expected to have **array-like semantics**:
/// - fixed size
/// - no dynamic resizing or allocation
/// - capacity encoded in the type
///
/// The schema is purely type-level:
/// - it does not store data
/// - it defines how data is represented and interpreted
///
/// ## Discriminant
///
/// The `Discriminant` links:
/// - the extension storage
/// - to its schema
///
/// allowing multiple independent extensions to coexist safely.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: defines a single default extension schema,
/// meaning only one static extension is assumed.
/// Allocation interface for virtual extensions whose type and schema
/// are defined externally.
///
/// This is a second-order abstraction over [`VirtualDynField`]:
/// instead of defining its own element type, the field delegates both
/// type and representation to an external schema.
///
/// ## Context
///
/// In the virtual system:
/// - [`VirtualDynField`] defines fields with internally known types
/// - [`VirtualDynExtensionSchema`] defines externally supplied types and layout
/// - `VirtualDynExtension` stores values using that schema
///
/// This allows a container (`Self`) to host fields whose types are:
/// - not known at implementation time
/// - supplied later via type-level composition
///
/// ## Representation
///
/// All operations are performed on the schema-defined representation:
/// - `TypesVia` defines `None`, `Some`, `Many`, and `Repr`
/// - `Many` is expected to have **vector-like semantics**
/// - size and bounds are resolved dynamically (within constraints)
///
/// ## Semantics
///
/// - **Container (`Self`)**
/// - owns storage
///
/// - **Schema (`TypesVia`)**
/// - defines element type and representation
///
/// This enables fields to remain fully generic over externally
/// defined and deferred types.
///
/// ## Key Property
///
/// Type and structure are **not fixed in the container**, but injected
/// externally and resolved at compile time.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: defines a single default extension,
/// meaning only one dynamic extension is assumed.
/// Allocation interface for virtual extensions whose type and schema
/// are defined externally and fully determined at compile time.
///
/// This is the static counterpart to [`VirtualDynExtension`], where
/// both type and structure are fixed via the schema.
///
/// ## Context
///
/// In the static field model:
/// - [`VirtualStaticField`] defines fields with fixed structure
/// - [`VirtualStaticExtensionSchema`] defines externally supplied types
/// and compile-time layout
/// - `VirtualStaticExtension` stores values using that schema
///
/// This allows a container (`Self`) to host externally defined fields
/// with statically determined structure.
///
/// ## Representation
///
/// All operations are performed on the schema-defined representation:
/// - `TypesVia` defines `None`, `Some`, `Many`, and `Repr`
/// - `Many` is expected to have **array-like semantics**
/// - size and capacity are encoded at compile time
///
/// ## Semantics
///
/// - **Container (`Self`)**
/// - owns storage
///
/// - **Schema (`TypesVia`)**
/// - defines element type and representation
///
/// ## Key Property
///
/// Both type and structure are **fully determined at compile time**,
/// enabling zero-overhead representations without dynamic checks.
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: defines a single default extension,
/// meaning only one static extension is assumed.
// ===============================================================================
// `````````````````````` VIRTUAL EXTENSION DEFAULT-HELPERS ``````````````````````
// ===============================================================================
/// Helper methods for accessing and mutating values in a [`VirtualDynExtension`].
///
/// These helpers operate on **dynamically shaped extensions** with
/// **vector-like semantics**:
/// - collections may grow or shrink (within bounds)
/// - indexing and iteration are supported
/// - mutations may fail due to constraints
///
/// Structure and types are defined externally via [`VirtualDynExtensionSchema`],
/// while storage is handled by the container.
///
/// All operations are performed via tagged conversions.
///
/// ## Default Discriminant
///
/// - `K = ()`: operates on a single default extension,
/// meaning one dynamic extension is assumed.
/// Blanket impl for all [`VirtualDynExtension`] types.
///
/// This trait is not intended to be implemented manually.
/// It exists as an ergonomic replacement for free helper functions.
///
/// All methods have default implementations, making this
/// forward-compatible: new helpers can be added without
/// breaking existing code.
/// Helper methods for accessing and mutating values in a [`VirtualStaticExtension`].
///
/// These helpers operate on **statically shaped extensions** with
/// **array-like semantics**:
/// - collection size is fixed at compile time
/// - no resizing or extension is performed
/// - operations act on the entire structure
///
/// Structure and types are defined externally via [`VirtualStaticExtensionSchema`],
/// while storage is handled by the container.
///
/// All operations are performed via tagged conversions.
///
/// ## Default Discriminant
///
/// - `K = ()`: operates on a single default extension,
/// meaning one static extension is assumed.
/// Blanket impl for all [`VirtualStaticExtension`] types.
///
/// This trait is not intended to be implemented manually.
/// It exists as an ergonomic replacement for free helper functions.
///
/// All methods have default implementations, making this
/// forward-compatible: new helpers can be added without
/// breaking existing code.
/// Sets the value at a given index in a collection.
///
/// If the index is out of bounds, the collection is automatically extended
/// with default values (`T::default()`) up to the required index.
///
/// ## Behavior
/// - If `index < current length`, the value is simply updated.
/// - If `index >= current length`, the collection is resized by filling
/// missing positions with default values, then the value is set.
///
/// ## Type Parameters
/// - `C`: A collection that supports indexing and extension.
/// - `T`: The element type, which must implement `Default`.
///
/// ## Example
/// If the collection has length 3 and you set index 5:
/// - Elements at index 3 and 4 will be filled with `T::default()`.
/// - Index 5 will be assigned the given `value`.
/// Implements an empty virtual extension schema for a given extension.
///
/// This macro defines a no-op schema where the extension has **no storage**
/// and behaves as absent.
///
/// It supports two modes:
///
/// - **Dynamic (default)** -> implements [`VirtualDynExtensionSchema`]
/// - uses vector-like semantics (`Vec<()>`)
/// - size-related operations (`len`, `min`, `max`) return `0`
///
/// - **Static (`static` keyword)** -> implements [`VirtualStaticExtensionSchema`]
/// - uses array-like semantics (`[(); 0]`)
/// - fully determined at compile time
/// - no size-related operations are required
///
/// ## Semantics
///
/// In both modes:
/// - `None`, `Some`, and `Repr` are represented as `()`
/// - no data is stored
/// - the extension is effectively non-existent
///
/// This is useful when:
/// - a container participates in the extension system
/// - but a particular extension is unsupported or intentionally omitted
///
/// ## Syntax
///
/// ### Dynamic (default)
/// ```ignore
/// empty_virtual_extension!(
/// target: MyContainer,
/// tag: MyExtension,
/// schema: MySchema,
/// generics: [T, U],
/// bounds: [T: Clone, U: Default],
/// );
/// ```
///
/// ### Static
/// ```ignore
/// empty_virtual_extension!(
/// target: MyContainer,
/// tag: MyExtension,
/// schema: static MySchema,
/// );
/// ```
///
/// ## Parameters
///
/// - `target`: container type (conceptual owner of the extension)
/// - `tag`: discriminant identifying the extension
/// - `schema`: schema type to implement
/// - `generics` *(optional)*: generics for the impl
/// - `bounds` *(optional)*: additional `where` constraints
///
/// ## Behavior
///
/// - The extension is treated as non-existent
/// - Dynamic mode:
/// - uses `Vec<()>` (always empty)
/// - returns `0` for all size queries
/// - Static mode:
/// - uses `[(); 0]` (zero-length array)
/// - fully resolved at compile time
};
// DYNAMIC VERSION (default)
=> ;
}
// ===============================================================================
// ````````````````````````` DELEGATED VIRTUAL EXTENSIONS ````````````````````````
// ===============================================================================
/// Constraint for delegating a virtual extension to an external schema.
///
/// This trait ties a container to an externally provided
/// [`VirtualDynExtensionSchema`], without requiring the container
/// to define the extension's type or representation itself.
///
/// ## Roles
///
/// - **Container (`Self`)**
/// - stores the extension representation
///
/// - **Schema (`Provider`)**
/// - defines the element type and representation
///
/// - **Caller**
/// - selects the extension via the `Discriminant`
///
/// ## Representation
///
/// The delegated schema is dynamic:
/// - `Many` has vector-like semantics
/// - size and bounds are resolved at runtime (within limits)
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: delegates a single default extension,
/// meaning one dynamic extension is assumed.
/// Blanket implementation enabling all types to delegate
/// extension schema resolution.
/// Constraint for delegating a virtual extension to an external
/// [`VirtualStaticExtensionSchema`].
///
/// This is the static counterpart to [`DelegateVirtualDynExtension`],
/// where the schema defines a fully determined, compile-time structure.
///
/// ## Roles
///
/// - **Container (`Self`)**
/// - stores the extension representation
///
/// - **Schema (`Provider`)**
/// - defines the element type and representation
///
/// - **Caller**
/// - selects the extension via the `Discriminant`
///
/// ## Representation
///
/// The delegated schema is static:
/// - `Many` has array-like semantics
/// - size and capacity are fixed at compile time
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: delegates a single default extension,
/// meaning one static extension is assumed.
/// Blanket implementation enabling all types to delegate
/// static extension schema resolution.
// ===============================================================================
// `````````````````````````````` VIRTUAL COLLECTOR ``````````````````````````````
// ===============================================================================
/// A virtual collector for values of type `T` under a discriminant.
///
/// A `VirtualCollector` represents a type that can:
/// - collect a value `T` into itself (via [`FromTag`])
/// - attempt to extract a value `T` back (via [`TryIntoTag`])
///
/// ## Virtual Field Context
///
/// In the virtual system:
/// - values may be interpreted differently depending on their role
/// - tagged conversions (`FromTag`, `TryIntoTag`) define those interpretations
/// - this trait groups types that support bidirectional interaction with `T`
///
/// ## Semantics
///
/// A `VirtualCollector` acts as a *tagged carrier* of `T`:
///
/// - `T -> Self`
/// - always succeeds (collection)
///
/// - `Self -> T`
/// - may fail depending on structure (extraction)
///
/// This is commonly implemented by enums where a specific variant
/// represents `T`.
///
/// ## Example Pattern
///
/// ```ignore
/// enum Value {
/// Number(u32),
/// Text(String),
/// }
///
/// // `Value` can act as a VirtualCollector<u32>
/// ```
///
/// ## Discriminant
///
/// The `Discriminant` ensures conversions remain unambiguous,
/// even when `T` or `Self` are generic or not fully concrete.
///
/// ## When to Use
///
/// Use this trait when:
/// - a type can *collect* values of `T`
/// - extraction may depend on internal structure
/// - tagged semantics are required for disambiguation
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: defines a single default interpretation,
/// meaning one collection/extraction behavior is assumed.
/// Blanket implementation for all types supporting bidirectional tagged
/// conversion with `T`.
///
/// Any type that:
/// - can be constructed from `T` via [`FromTag`]
/// - can attempt to extract `T` via [`TryIntoTag`]
///
/// automatically implements [`VirtualCollector`].
///
/// This allows enums and similar container types to act as collectors
/// without requiring explicit implementations.
// ===============================================================================
// ````````````````````````````` VIRTUAL STORAGE MAPS ````````````````````````````
// ===============================================================================
/// A storage-backed virtual n-map owned by a container in the virtual structure system.
///
/// This trait defines a **map-like virtual component** that is logically owned
/// by a container (`For`), while its storage is delegated to an external
/// implementation (e.g. [`StorageNMap`]).
///
/// ## Virtual Structure Context
///
/// In the virtual system, a container (virtual struct) composes behavior through
/// independent, type-driven components:
///
/// - [`VirtualDynField`] / [`VirtualStaticField`] - field-level abstraction
/// over values and cardinality
/// - [`VirtualDynExtension`] / [`VirtualStaticExtension`] - externally defined
/// field schemas
/// - `VirtualNMap` - container-level map storage
///
/// These components are:
/// - **logically part of the container**
/// - but **not required to share a single physical representation**
///
/// ## Ownership and Delegation
///
/// The container (`For`) acts as the **owner** of the map:
/// - it defines the type context (e.g. key/value via associated types)
/// - it determines how the map is used
/// - but it does not store the map directly
///
/// Instead, storage is delegated to a native map implementation.
///
/// This separation allows:
/// - lightweight container representations
/// - efficient handling of large or frequently mutated data
/// - independent evolution of storage and structure
///
/// ## Type-Level Association
///
/// - `For`: the owning container (virtual struct)
/// - `Discriminant`: a type-level key identifying this map
///
/// This enables:
/// - multiple independent maps per container
/// - map definitions derived from container-level abstractions
/// - coherence-safe composition via distinct discriminants
///
/// ## Storage Model
///
/// - storage is provided by the implementor via [`StorageNMap`]
/// - keys are encoded using [`KeyGenerator`]
/// - iteration and prefix-based access are supported
///
/// The map is external in storage, but internal in ownership and usage.
///
/// ## When to Use
///
/// Use this trait when:
/// - a container logically owns map-like data
/// - key/value types depend on container-level abstractions
/// - data is large, dynamic, or frequently mutated
/// - embedding the map in a virtual field or representation is inefficient
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: defines a single default map,
/// meaning one virtual map is assumed per container.
/// A storage-backed virtual map owned by a container in the virtual structure system.
///
/// This trait defines a **map-like virtual component** that is logically owned
/// by a container (`For`), while its storage is delegated to an external
/// implementation (e.g. [`StorageMap`]).
///
/// ## Virtual Structure Context
///
/// In the virtual system, a container (virtual struct) composes behavior through
/// independent, type-driven components:
///
/// - [`VirtualDynField`] / [`VirtualStaticField`] - field-level abstraction
/// over values and cardinality
/// - [`VirtualDynExtension`] / [`VirtualStaticExtension`] - externally defined
/// field schemas
/// - `VirtualMap` - container-level map storage
///
/// These components are logically part of the container, but are not required
/// to share a single physical representation.
///
/// ## Ownership and Delegation
///
/// The container (`For`) acts as the **owner** of the map:
/// - it provides the type context for the map (key/value via associated types)
/// - it determines how the map is used
/// - but it does not store the map directly
///
/// Instead, storage is delegated to a native map implementation.
///
/// This allows:
/// - avoiding encode/decode overhead from embedding maps in representations
/// - efficient handling of large or frequently mutated data
/// - separation of structure (types) from storage (runtime)
///
/// ## Type-Level Association
///
/// - `For`: the owning container (virtual struct)
/// - `Discriminant`: a type-level key identifying this map
///
/// This enables:
/// - multiple independent maps per container
/// - map definitions derived from container-level abstractions
/// - coherence-safe composition via distinct discriminants
///
/// ## Storage Model
///
/// - storage is provided by the implementor via [`StorageMap`]
/// - keys are encoded via [`KeyGenerator`]
/// - iteration and full traversal are supported
///
/// The map is external in storage, but internal in ownership and usage.
///
/// ## When to Use
///
/// Use this trait when:
/// - a container logically owns map-like data
/// - key/value types depend on container-level abstractions
/// - data is large, dynamic, or frequently mutated
/// - embedding the map in a virtual field or representation is inefficient
///
/// ## Default Discriminant
///
/// - `Discriminant = ()`: defines a single default map,
/// meaning one virtual map is assumed per container.