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
use crate::borrow::{AtomicRefCell, Ref, RefMap, RefMapMut, RefMut};
use crate::entity::Entity;
use crate::entity::EntityLocation;
use crate::event::EventFilterWrapper;
use crate::event::Subscriber;
use crate::event::{Event, Subscribers};
use crate::filter::ArchetypeFilterData;
use crate::filter::ChunkFilterData;
use crate::filter::ChunksetFilterData;
use crate::filter::EntityFilter;
use crate::filter::Filter;
use crate::index::ArchetypeIndex;
use crate::index::ChunkIndex;
use crate::index::ComponentIndex;
use crate::index::SetIndex;
use crate::iterator::FissileZip;
use crate::iterator::SliceVecIter;
use crate::world::TagSet;
use crate::world::WorldId;
use derivative::Derivative;
use fxhash::FxHashMap;
use smallvec::SmallVec;
use std::any::TypeId;
use std::cell::UnsafeCell;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::mem::size_of;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ops::RangeBounds;
use std::ptr::NonNull;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use tracing::trace;

static VERSION_COUNTER: AtomicU64 = AtomicU64::new(0);

fn next_version() -> u64 {
    VERSION_COUNTER
        .fetch_add(1, Ordering::Relaxed)
        .checked_add(1)
        .unwrap()
}

#[cfg(not(feature = "ffi"))]
/// A type ID identifying a component type.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct ComponentTypeId(pub TypeId);

#[cfg(not(feature = "ffi"))]
impl ComponentTypeId {
    /// Gets the component type ID that represents type `T`.
    pub fn of<T: Component>() -> Self { Self(TypeId::of::<T>()) }
}

#[cfg(feature = "ffi")]
/// A type ID identifying a component type.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct ComponentTypeId(pub TypeId, pub u32);

#[cfg(feature = "ffi")]
impl ComponentTypeId {
    /// Gets the component type ID that represents type `T`.
    pub fn of<T: Component>() -> Self { Self(TypeId::of::<T>(), 0) }
}

#[cfg(not(feature = "ffi"))]
/// A type ID identifying a tag type.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct TagTypeId(pub TypeId);

#[cfg(not(feature = "ffi"))]
impl TagTypeId {
    /// Gets the tag type ID that represents type `T`.
    pub fn of<T: Component>() -> Self { Self(TypeId::of::<T>()) }
}

#[cfg(feature = "ffi")]
/// A type ID identifying a tag type.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct TagTypeId(pub TypeId, pub u32);

#[cfg(feature = "ffi")]
impl TagTypeId {
    /// Gets the tag type ID that represents type `T`.
    pub fn of<T: Component>() -> Self { Self(TypeId::of::<T>(), 0) }
}

/// A `Component` is per-entity data that can be attached to a single entity.
pub trait Component: Send + Sync + 'static {}

/// A `Tag` is shared data that can be attached to multiple entities at once.
pub trait Tag: Clone + Send + Sync + PartialEq + 'static {}

impl<T: Send + Sync + 'static> Component for T {}
impl<T: Clone + Send + Sync + PartialEq + 'static> Tag for T {}

/// Stores slices of `ComponentTypeId`, each of which identifies the type of components
/// contained within the archetype of the same index.
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct ComponentTypes(SliceVec<ComponentTypeId>);

/// Stores slices of `TagTypeId`, each of which identifies the type of tags
/// contained within the archetype of the same index.
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct TagTypes(SliceVec<TagTypeId>);

impl ComponentTypes {
    /// Gets an iterator over all type ID slices.
    pub fn iter(&self) -> SliceVecIter<ComponentTypeId> { self.0.iter() }

    /// Gets the number of slices stored within the set.
    pub fn len(&self) -> usize { self.0.len() }

    /// Determines if the set is empty.
    pub fn is_empty(&self) -> bool { self.len() < 1 }
}

impl TagTypes {
    /// Gets an iterator over all type ID slices.
    pub fn iter(&self) -> SliceVecIter<TagTypeId> { self.0.iter() }

    /// Gets the number of slices stored within the set.
    pub fn len(&self) -> usize { self.0.len() }

    /// Determines if the set is empty.
    pub fn is_empty(&self) -> bool { self.len() < 1 }
}

/// A vector of slices.
///
/// Each slice is stored inline so as to be efficiently iterated through linearly.
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct SliceVec<T> {
    data: Vec<T>,
    counts: Vec<usize>,
}

impl<T> SliceVec<T> {
    /// Gets the length of the vector.
    pub fn len(&self) -> usize { self.counts.len() }

    /// Determines if the vector is empty.
    pub fn is_empty(&self) -> bool { self.len() < 1 }

    /// Pushes a new slice onto the end of the vector.
    pub fn push<I: IntoIterator<Item = T>>(&mut self, items: I) {
        let mut count = 0;
        for item in items.into_iter() {
            self.data.push(item);
            count += 1;
        }
        self.counts.push(count);
    }

    /// Gets an iterator over all slices in the vector.
    pub fn iter(&self) -> SliceVecIter<T> {
        SliceVecIter {
            data: &self.data,
            counts: &self.counts,
        }
    }
}

/// Stores all entity data for a `World`.
pub struct Storage {
    world_id: WorldId,
    component_types: ComponentTypes,
    tag_types: TagTypes,
    archetypes: Vec<ArchetypeData>,
    subscribers: Subscribers,
}

impl Storage {
    // Creates an empty `Storage`.
    pub fn new(world_id: WorldId) -> Self {
        Self {
            world_id,
            component_types: ComponentTypes::default(),
            tag_types: TagTypes::default(),
            archetypes: Vec::default(),
            subscribers: Subscribers::default(),
        }
    }

    pub(crate) fn subscribe<T: EntityFilter + Sync + 'static>(
        &mut self,
        sender: crossbeam_channel::Sender<Event>,
        filter: T,
    ) {
        let subscriber = Subscriber::new(Arc::new(EventFilterWrapper(filter.clone())), sender);
        self.subscribers.push(subscriber.clone());

        for i in filter.iter_archetype_indexes(self).collect::<Vec<_>>() {
            self.archetypes_mut()[i].subscribe(subscriber.clone());
        }
    }

    /// Creates a new archetype.
    ///
    /// Returns the index of the newly created archetype and an exclusive reference to the
    /// achetype's data.
    pub(crate) fn alloc_archetype(
        &mut self,
        desc: ArchetypeDescription,
    ) -> (ArchetypeIndex, &mut ArchetypeData) {
        let index = ArchetypeIndex(self.archetypes.len());
        let id = ArchetypeId(self.world_id, index);
        let archetype = ArchetypeData::new(id, desc);

        self.push(archetype);

        let archetype = &mut self.archetypes[index];
        (index, archetype)
    }

    pub(crate) fn push(&mut self, mut archetype: ArchetypeData) {
        let desc = archetype.description();
        self.component_types
            .0
            .push(desc.components.iter().map(|&(t, _)| t));
        self.tag_types.0.push(desc.tags.iter().map(|&(t, _)| t));

        let index = ArchetypeIndex(self.archetypes.len());
        let archetype_data = ArchetypeFilterData {
            component_types: &self.component_types,
            tag_types: &self.tag_types,
        };

        let id = archetype.id();

        trace!(
            world = id.world().index(),
            archetype = *id.index(),
            components = ?desc.component_names,
            tags = ?desc.tag_names,
            "Created Archetype"
        );

        let mut subscribers = self.subscribers.matches_archetype(archetype_data, index);
        subscribers.send(Event::ArchetypeCreated(id));
        archetype.set_subscribers(subscribers);

        self.archetypes.push(archetype);
    }

    /// Gets a vector of slices of all component types for all archetypes.
    ///
    /// Each slice contains the component types for the archetype at the corresponding index.
    pub fn component_types(&self) -> &ComponentTypes { &self.component_types }

    /// Gets a vector of slices of all tag types for all archetypes.
    ///
    /// Each slice contains the tag types for the archetype at the corresponding index.
    pub fn tag_types(&self) -> &TagTypes { &self.tag_types }

    /// Gets a slice reference to all archetypes.
    pub fn archetypes(&self) -> &[ArchetypeData] { &self.archetypes }

    /// Gets a mutable slice reference to all archetypes.
    pub fn archetypes_mut(&mut self) -> &mut [ArchetypeData] { &mut self.archetypes }

    pub(crate) fn drain<R: RangeBounds<usize>>(
        &mut self,
        range: R,
    ) -> std::vec::Drain<ArchetypeData> {
        self.archetypes.drain(range)
    }

    pub(crate) fn archetype(
        &self,
        ArchetypeIndex(index): ArchetypeIndex,
    ) -> Option<&ArchetypeData> {
        self.archetypes().get(index)
    }

    pub(crate) fn archetype_mut(
        &mut self,
        ArchetypeIndex(index): ArchetypeIndex,
    ) -> Option<&mut ArchetypeData> {
        self.archetypes_mut().get_mut(index)
    }

    pub(crate) unsafe fn archetype_unchecked(
        &self,
        ArchetypeIndex(index): ArchetypeIndex,
    ) -> &ArchetypeData {
        self.archetypes().get_unchecked(index)
    }

    pub(crate) unsafe fn archetype_unchecked_mut(
        &mut self,
        ArchetypeIndex(index): ArchetypeIndex,
    ) -> &mut ArchetypeData {
        self.archetypes_mut().get_unchecked_mut(index)
    }

    pub(crate) fn chunk(&self, loc: EntityLocation) -> Option<&ComponentStorage> {
        self.archetype(loc.archetype())
            .and_then(|atd| atd.chunkset(loc.set()))
            .and_then(|cs| cs.chunk(loc.chunk()))
    }

    pub(crate) fn chunk_mut(&mut self, loc: EntityLocation) -> Option<&mut ComponentStorage> {
        self.archetype_mut(loc.archetype())
            .and_then(|atd| atd.chunkset_mut(loc.set()))
            .and_then(|cs| cs.chunk_mut(loc.chunk()))
    }
}

/// Stores metadata decribing the type of a tag.
#[derive(Copy, Clone, PartialEq)]
pub struct TagMeta {
    size: usize,
    align: usize,
    drop_fn: Option<fn(*mut u8)>,
    eq_fn: fn(*const u8, *const u8) -> bool,
    clone_fn: fn(*const u8, *mut u8),
}

impl TagMeta {
    /// Gets the tag meta of tag type `T`.
    pub fn of<T: Tag>() -> Self {
        TagMeta {
            size: size_of::<T>(),
            align: std::mem::align_of::<T>(),
            drop_fn: if std::mem::needs_drop::<T>() {
                Some(|ptr| unsafe { std::ptr::drop_in_place(ptr as *mut T) })
            } else {
                None
            },
            eq_fn: |a, b| unsafe { *(a as *const T) == *(b as *const T) },
            clone_fn: |src, dst| unsafe {
                let clone = (&*(src as *const T)).clone();
                std::ptr::write(dst as *mut T, clone);
            },
        }
    }

    pub(crate) unsafe fn equals(&self, a: *const u8, b: *const u8) -> bool { (self.eq_fn)(a, b) }

    pub(crate) unsafe fn clone(&self, src: *const u8, dst: *mut u8) { (self.clone_fn)(src, dst) }

    pub(crate) unsafe fn drop(&self, val: *mut u8) {
        if let Some(drop_fn) = self.drop_fn {
            (drop_fn)(val);
        }
    }

    pub(crate) fn layout(&self) -> std::alloc::Layout {
        unsafe { std::alloc::Layout::from_size_align_unchecked(self.size, self.align) }
    }

    pub(crate) fn is_zero_sized(&self) -> bool { self.size == 0 }
}

/// Stores metadata describing the type of a component.
#[derive(Copy, Clone, PartialEq)]
pub struct ComponentMeta {
    size: usize,
    align: usize,
    drop_fn: Option<fn(*mut u8)>,
}

impl ComponentMeta {
    /// Gets the component meta of component type `T`.
    pub fn of<T: Component>() -> Self {
        ComponentMeta {
            size: size_of::<T>(),
            align: std::mem::align_of::<T>(),
            drop_fn: if std::mem::needs_drop::<T>() {
                Some(|ptr| unsafe { std::ptr::drop_in_place(ptr as *mut T) })
            } else {
                None
            },
        }
    }

    pub(crate) fn size(&self) -> usize { self.size }

    pub(crate) fn align(&self) -> usize { self.align }
}

/// Describes the layout of an archetype, including what components
/// and tags shall be attached to entities stored within an archetype.
#[derive(Default, Clone, PartialEq)]
pub struct ArchetypeDescription {
    tags: Vec<(TagTypeId, TagMeta)>,
    components: Vec<(ComponentTypeId, ComponentMeta)>,
    tag_names: Vec<&'static str>,
    component_names: Vec<&'static str>,
}

impl ArchetypeDescription {
    /// Gets a slice of the tags in the description.
    pub fn tags(&self) -> &[(TagTypeId, TagMeta)] { &self.tags }

    /// Gets a slice of the components in the description.
    pub fn components(&self) -> &[(ComponentTypeId, ComponentMeta)] { &self.components }

    /// Adds a tag to the description.
    pub fn register_tag_raw(&mut self, type_id: TagTypeId, type_meta: TagMeta) {
        self.tags.push((type_id, type_meta));
        self.tag_names.push("<unknown>");
    }

    /// Adds a tag to the description.
    pub fn register_tag<T: Tag>(&mut self) {
        self.tags.push((TagTypeId::of::<T>(), TagMeta::of::<T>()));
        self.tag_names.push(std::any::type_name::<T>());
    }

    /// Adds a component to the description.
    pub fn register_component_raw(&mut self, type_id: ComponentTypeId, type_meta: ComponentMeta) {
        self.components.push((type_id, type_meta));
        self.component_names.push("<unknown>");
    }

    /// Adds a component to the description.
    pub fn register_component<T: Component>(&mut self) {
        self.components
            .push((ComponentTypeId::of::<T>(), ComponentMeta::of::<T>()));
        self.component_names.push(std::any::type_name::<T>());
    }
}

impl<'a> Filter<ArchetypeFilterData<'a>> for ArchetypeDescription {
    type Iter = FissileZip<SliceVecIter<'a, TagTypeId>, SliceVecIter<'a, ComponentTypeId>>;

    fn collect(&self, source: ArchetypeFilterData<'a>) -> Self::Iter {
        FissileZip::new(source.tag_types.iter(), source.component_types.iter())
    }

    fn is_match(&self, (tags, components): &<Self::Iter as Iterator>::Item) -> Option<bool> {
        Some(
            tags.len() == self.tags.len()
                && self.tags.iter().all(|(t, _)| tags.contains(t))
                && components.len() == self.components.len()
                && self.components.iter().all(|(t, _)| components.contains(t)),
        )
    }
}

const MAX_CHUNK_SIZE: usize = 16 * 1024 * 10;
const COMPONENT_STORAGE_ALIGNMENT: usize = 64;

/// Unique ID of an archetype.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct ArchetypeId(WorldId, ArchetypeIndex);

impl ArchetypeId {
    pub(crate) fn new(world_id: WorldId, index: ArchetypeIndex) -> Self {
        ArchetypeId(world_id, index)
    }

    pub fn index(self) -> ArchetypeIndex { self.1 }

    pub fn world(self) -> WorldId { self.0 }
}

/// Contains all of the tags attached to the entities in each chunk.
pub struct Tags(pub(crate) SmallVec<[(TagTypeId, TagStorage); 3]>);

impl Tags {
    fn new(mut data: SmallVec<[(TagTypeId, TagStorage); 3]>) -> Self {
        data.sort_by_key(|&(t, _)| t);
        Self(data)
    }

    fn validate(&self, set_count: usize) {
        for (_, tags) in self.0.iter() {
            debug_assert_eq!(set_count, tags.len());
        }
    }

    /// Gets the set of tag values of the specified type attached to all chunks.
    #[inline]
    pub fn get(&self, type_id: TagTypeId) -> Option<&TagStorage> {
        self.0
            .binary_search_by_key(&type_id, |&(t, _)| t)
            .ok()
            .map(|i| unsafe { &self.0.get_unchecked(i).1 })
    }

    /// Mutably gets the set of all tag values of the specified type attached to all chunks.
    #[inline]
    pub fn get_mut(&mut self, type_id: TagTypeId) -> Option<&mut TagStorage> {
        self.0
            .binary_search_by_key(&type_id, |&(t, _)| t)
            .ok()
            .map(move |i| unsafe { &mut self.0.get_unchecked_mut(i).1 })
    }

    pub(crate) fn tag_set(&self, SetIndex(index): SetIndex) -> DynamicTagSet {
        let mut tags = DynamicTagSet { tags: Vec::new() };

        unsafe {
            for &(type_id, ref storage) in self.0.iter() {
                let (ptr, element_size, count) = storage.data_raw();
                debug_assert!(index < count, "set index out of bounds");
                tags.push(
                    type_id,
                    *storage.element(),
                    NonNull::new(ptr.as_ptr().add(element_size * index)).unwrap(),
                );
            }
        }

        tags
    }
}

pub(crate) struct DynamicTagSet {
    // the pointer here is to heap allocated memory owned by the tag set
    tags: Vec<(TagTypeId, TagMeta, NonNull<u8>)>,
}

unsafe impl Send for DynamicTagSet {}

unsafe impl Sync for DynamicTagSet {}

impl DynamicTagSet {
    pub fn push(&mut self, type_id: TagTypeId, meta: TagMeta, value: NonNull<u8>) {
        // we clone the value here and take ownership of the copy
        unsafe {
            if meta.is_zero_sized() {
                self.tags
                    .push((type_id, meta, NonNull::new(meta.align as *mut u8).unwrap()));
            } else {
                let copy = std::alloc::alloc(meta.layout());
                meta.clone(value.as_ptr(), copy);
                self.tags.push((type_id, meta, NonNull::new(copy).unwrap()));
            }
        }
    }

    pub fn remove(&mut self, type_id: TagTypeId) {
        if let Some((i, _)) = self
            .tags
            .iter()
            .enumerate()
            .find(|(_, &(t, _, _))| t == type_id)
        {
            let (_, meta, ptr) = self.tags.remove(i);
            unsafe {
                // drop and dealloc the copy as we own this memory
                if let Some(drop_fn) = meta.drop_fn {
                    drop_fn(ptr.as_ptr());
                }

                if !meta.is_zero_sized() {
                    std::alloc::dealloc(ptr.as_ptr(), meta.layout());
                }
            }
        }
    }
}

impl TagSet for DynamicTagSet {
    fn write_tags(&self, tags: &mut Tags) {
        for &(type_id, ref meta, ptr) in self.tags.iter() {
            let storage = tags.get_mut(type_id).unwrap();
            unsafe {
                if meta.drop_fn.is_some() && !meta.is_zero_sized() {
                    // clone the value into temp storage then move it into the chunk
                    // we can dealloc the copy without dropping because the value
                    // is considered moved and will be dropped by the tag storage later
                    let copy = std::alloc::alloc(meta.layout());
                    meta.clone(ptr.as_ptr(), copy);
                    storage.push_raw(copy);
                    std::alloc::dealloc(copy, meta.layout());
                } else {
                    // copy the value directly into the tag storage
                    // if the value has no drop fn, then it is safe for us to make
                    // copies of the data without explicit clones
                    storage.push_raw(ptr.as_ptr())
                }
            }
        }
    }
}

impl Drop for DynamicTagSet {
    fn drop(&mut self) {
        // we own all of the vales in the set, so we need to drop and dealloc them
        for (_, meta, ptr) in self.tags.drain(..) {
            unsafe {
                let layout = std::alloc::Layout::from_size_align_unchecked(meta.size, meta.align);
                if let Some(drop_fn) = meta.drop_fn {
                    drop_fn(ptr.as_ptr());
                }
                if !meta.is_zero_sized() {
                    std::alloc::dealloc(ptr.as_ptr(), layout);
                }
            }
        }
    }
}

/// Stores entity data in chunks. All entities within an archetype have the same data layout
/// (component and tag types).
pub struct ArchetypeData {
    id: ArchetypeId,
    desc: ArchetypeDescription,
    tags: Tags,
    component_layout: ComponentStorageLayout,
    chunk_sets: Vec<Chunkset>,
    subscribers: Subscribers,
}

impl ArchetypeData {
    fn new(id: ArchetypeId, desc: ArchetypeDescription) -> Self {
        // create tag storage
        let tags = desc
            .tags
            .iter()
            .map(|&(type_id, meta)| (type_id, TagStorage::new(meta)))
            .collect();

        // create component data layout
        let max_component_size = desc
            .components
            .iter()
            .map(|(_, meta)| meta.size)
            .max()
            .unwrap_or(0);
        let entity_capacity = std::cmp::max(
            1,
            MAX_CHUNK_SIZE / std::cmp::max(max_component_size, size_of::<Entity>()),
        );
        let mut data_capacity = 0usize;
        let mut component_data_offsets = Vec::new();
        for &(type_id, meta) in desc.components.iter() {
            data_capacity = align_up(
                align_up(data_capacity, COMPONENT_STORAGE_ALIGNMENT),
                meta.align,
            );
            component_data_offsets.push((type_id, data_capacity, meta));
            data_capacity += meta.size * entity_capacity;
        }
        let data_alignment =
            std::alloc::Layout::from_size_align(data_capacity, COMPONENT_STORAGE_ALIGNMENT)
                .expect("invalid component data size/alignment");

        ArchetypeData {
            desc,
            id,
            tags: Tags::new(tags),
            component_layout: ComponentStorageLayout {
                capacity: entity_capacity,
                alloc_layout: data_alignment,
                data_layout: component_data_offsets,
            },
            chunk_sets: Vec::new(),
            subscribers: Subscribers::default(),
        }
    }

    pub(crate) fn delete_all(&mut self) {
        for set in &mut self.chunk_sets {
            // Clearing the chunk will Drop all the data
            set.chunks.clear();
        }
    }

    pub(crate) fn subscribe(&mut self, subscriber: Subscriber) {
        self.subscribers.push(subscriber.clone());

        for i in (0..self.chunk_sets.len()).map(SetIndex) {
            let filter = ChunksetFilterData {
                archetype_data: self,
            };

            if subscriber.filter.matches_chunkset(filter, i) {
                self.chunk_sets[i].subscribe(subscriber.clone());
            }
        }
    }

    pub(crate) fn set_subscribers(&mut self, subscribers: Subscribers) {
        self.subscribers = subscribers;

        for i in (0..self.chunk_sets.len()).map(SetIndex) {
            let filter = ChunksetFilterData {
                archetype_data: self,
            };

            let subscribers = self.subscribers.matches_chunkset(filter, i);
            self.chunk_sets[i].set_subscribers(subscribers);
        }
    }

    /// Gets the unique ID of this archetype.
    pub fn id(&self) -> ArchetypeId { self.id }

    fn find_chunk_set_by_tags(
        &self,
        other_tags: &Tags,
        other_set_index: SetIndex,
    ) -> Option<SetIndex> {
        // search for a matching chunk set
        let mut set_match = None;
        for self_set_index in 0..self.chunk_sets.len() {
            let self_set_index = SetIndex(self_set_index);
            let mut matches = true;
            for &(type_id, ref tags) in self.tags.0.iter() {
                unsafe {
                    let (self_tag_ptr, size, _) = tags.data_raw();
                    let (other_tag_ptr, _, _) = other_tags.get(type_id).unwrap().data_raw();

                    if !tags.element().equals(
                        self_tag_ptr.as_ptr().add(self_set_index.0 * size),
                        other_tag_ptr.as_ptr().add(other_set_index.0 * size),
                    ) {
                        matches = false;
                        break;
                    }
                }
            }

            if matches {
                set_match = Some(self_set_index);
                break;
            }
        }

        set_match
    }

    pub(crate) fn find_or_create_chunk_set_by_tags(
        &mut self,
        src_tags: &Tags,
        src_chunk_set_index: SetIndex,
    ) -> SetIndex {
        let dst_chunk_set_index = self.find_chunk_set_by_tags(src_tags, src_chunk_set_index);
        dst_chunk_set_index.unwrap_or_else(|| {
            self.alloc_chunk_set(|self_tags| {
                for (type_id, other_tags) in src_tags.0.iter() {
                    unsafe {
                        let (src, _, _) = other_tags.data_raw();
                        let dst = self_tags.get_mut(*type_id).unwrap().alloc_ptr();
                        other_tags.element().clone(src.as_ptr(), dst);
                    }
                }
            })
        })
    }

    pub(crate) fn move_from(&mut self, mut other: ArchetypeData) {
        let other_tags = &other.tags;
        for (other_index, mut set) in other.chunk_sets.drain(..).enumerate() {
            let other_index = SetIndex(other_index);
            let set_match = self.find_chunk_set_by_tags(&other_tags, other_index);

            if let Some(chunk_set) = set_match {
                // if we found a match, move the chunks into the set
                let target = &mut self.chunk_sets[chunk_set];
                for mut chunk in set.drain(..) {
                    chunk.mark_modified();
                    target.push(chunk);
                }
            } else {
                // if we did not find a match, clone the tags and move the set
                set.mark_modified();
                self.push(set, |self_tags| {
                    for &(type_id, ref other_tags) in other_tags.0.iter() {
                        unsafe {
                            let (src, _, _) = other_tags.data_raw();
                            let dst = self_tags.get_mut(type_id).unwrap().alloc_ptr();
                            other_tags.element().clone(src.as_ptr(), dst);
                        }
                    }
                });
            }
        }

        self.tags.validate(self.chunk_sets.len());
    }

    /// Given a source world and archetype, step through all of its chunks and copy the data in it
    /// into this archetype. The archetype index is provided so that we can produce EntityLocations
    /// During this process, we can replace pre-existing entities. This function assumes that any
    /// entity referenced in replace_mappings actually exists in the world. The public API in world
    /// checks this assumption and panics if it is violated.
    ///
    /// See also `clone_from_single`, which copies a specific entity
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn clone_from<
        's,
        CloneImplT: crate::world::CloneImpl,
        CloneImplResultT: crate::world::CloneImplResult,
        EntityReplacePolicyT: crate::world::EntityReplacePolicy<'s>,
    >(
        &mut self,
        src_world: &crate::world::World,
        src_archetype: &ArchetypeData,
        dst_archetype_index: ArchetypeIndex,
        dst_entity_allocator: &crate::entity::EntityAllocator,
        dst_entity_locations: &mut crate::entity::Locations,
        clone_impl: &CloneImplT,
        clone_impl_result: &mut CloneImplResultT,
        entity_replace_policy: &EntityReplacePolicyT,
    ) {
        // Iterate all the chunk sets within the source archetype
        let src_tags = &src_archetype.tags;
        for (src_chunk_set_index, src_chunk_set) in src_archetype.chunk_sets.iter().enumerate() {
            let src_chunk_set_index = SetIndex(src_chunk_set_index);
            let dst_chunk_set_index =
                self.find_or_create_chunk_set_by_tags(src_tags, src_chunk_set_index);

            // Iterate all the chunks within the source chunk set
            for (_src_chunk_idx, src_chunk) in src_chunk_set.chunks.iter().enumerate() {
                // Copy the data from source to destination. Continuously find or create chunks as
                // needed until we've copied all the data
                let mut entities_remaining = src_chunk.len();
                while entities_remaining > 0 {
                    // Get or allocate a chunk.. since we could be transforming to a larger component size, it's possible
                    // that even a brand-new, empty chunk won't be large enough to hold everything in the chunk we are copying from
                    let dst_free_chunk_index =
                        self.get_free_chunk(dst_chunk_set_index, entities_remaining);
                    let dst_chunk_set = &mut self.chunk_sets[dst_chunk_set_index];
                    let dst_chunk = &mut dst_chunk_set.chunks[dst_free_chunk_index];

                    // Determine how many entities we will write
                    let entities_to_write =
                        std::cmp::min(entities_remaining, dst_chunk.capacity() - dst_chunk.len());

                    // Prepare to write to the chunk storage
                    let mut writer = dst_chunk.writer();
                    let (dst_entities, dst_components) = writer.get();

                    // Find the region of memory we will be reading from in the source chunk
                    let src_begin_idx = ComponentIndex(src_chunk.len() - entities_remaining);
                    let src_end_idx = ComponentIndex(src_begin_idx.0 + entities_to_write);

                    let dst_begin_idx = ComponentIndex(dst_entities.len());
                    let dst_end_idx = ComponentIndex(dst_entities.len() + entities_to_write);

                    // Copy all the entities to the destination chunk. The normal case is that we simply allocate
                    // new entities.
                    //
                    // We also allow end-user to specify a HashMap<Entity, Entity>. The key is an Entity from
                    // the source chunk and the value is an Entity from the destination chunk. Rather than appending
                    // data to the destination chunk, we will *replace* the data, according to the mapping. This
                    // is specifically intended for use with hot-reloading data. When some source data is changed,
                    // we can use the mapping to respawn entities as needed using the new data.

                    // We know how many entities will be appended to this list
                    dst_entities.reserve(dst_entities.len() + entities_to_write);

                    for src_entity in &src_chunk.entities[src_begin_idx.0..src_end_idx.0] {
                        // Determine if there is an entity we will be replacing
                        let dst_entity = entity_replace_policy.get_dst_entity(*src_entity);

                        // The location of the next entity
                        let location = EntityLocation::new(
                            dst_archetype_index,
                            dst_chunk_set_index,
                            dst_free_chunk_index,
                            ComponentIndex(dst_entities.len()),
                        );

                        // Determine the Entity to use for this element
                        let dst_entity = if let Some(dst_entity) = dst_entity {
                            // We are replacing data
                            // Verify that the entity is alive.. this checks the index and version of the entity
                            // The entity should be alive because World::clone_from verifies this
                            debug_assert!(dst_entity_allocator.is_alive(dst_entity));
                            dst_entity
                        } else {
                            // We are appending data, allocate a new entity
                            dst_entity_allocator.create_entity()
                        };

                        dst_entity_locations.set(dst_entity, location);
                        dst_entities.push(dst_entity);

                        clone_impl_result.add_result(*src_entity, dst_entity);
                    }

                    ArchetypeData::clone_components(
                        clone_impl,
                        src_world,
                        src_archetype,
                        src_chunk,
                        src_begin_idx..src_end_idx,
                        &dst_entities[dst_begin_idx.0..dst_end_idx.0],
                        dst_components,
                        entities_to_write,
                    );

                    entities_remaining -= entities_to_write;
                }
            }
        }
    }

    /// Given a source world, archetype, and entity, copy it into this archetype. The archetype
    /// index is provided so that we can produce EntityLocations.
    /// During this process, we can replace a pre-existing entity. This function assumes that if
    /// replace_mapping is not none, that the entity exists. The public API in world checks this
    /// assumption and panics if it is violated.
    ///
    /// See also `clone_from`, which copies all data
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn clone_from_single<C: crate::world::CloneImpl>(
        &mut self,
        src_world: &crate::world::World,
        src_archetype: &ArchetypeData,
        src_location: &EntityLocation,
        dst_archetype_index: ArchetypeIndex,
        dst_entity_allocator: &crate::entity::EntityAllocator,
        dst_entity_locations: &mut crate::entity::Locations,
        clone_impl: &C,
        replace_mapping: Option<Entity>,
    ) -> Entity {
        // We are reading from a specific chunk set within the source archetype
        let src_tags = &src_archetype.tags;
        let src_chunk_set_index = src_location.set();
        let src_chunk_set = &src_archetype.chunk_sets[src_chunk_set_index];

        // Find or create the chunk set that matches the source chunk set
        let dst_chunk_set_index =
            self.find_or_create_chunk_set_by_tags(src_tags, src_chunk_set_index);

        // Get the source chunk
        let src_chunk_idx = src_location.chunk();
        let src_chunk = &src_chunk_set.chunks[src_chunk_idx];

        // Get or allocate a chunk.. since we could be transforming to a larger component size, it's possible
        // that even a brand-new, empty chunk won't be large enough to hold everything in the chunk we are copying from
        let dst_free_chunk_index = self.get_free_chunk(dst_chunk_set_index, 1);
        let dst_chunk_set = &mut self.chunk_sets[dst_chunk_set_index];
        let dst_chunk = &mut dst_chunk_set.chunks[dst_free_chunk_index];

        // Determine how many entities we will write
        let entities_to_write = 1;

        // Prepare to write to the chunk storage
        let mut writer = dst_chunk.writer();
        let (dst_entities, dst_components) = writer.get();

        // Find the region of memory we will be reading from in the source chunk
        let src_begin_idx = src_location.component();
        let src_end_idx = ComponentIndex(src_begin_idx.0 + 1);

        // We know how many entities will be appended to this list
        let dst_begin_idx = ComponentIndex(dst_entities.len());
        let dst_end_idx = ComponentIndex(dst_entities.len() + entities_to_write);

        // Copy the entity to the destination chunk. The normal case is that we simply allocate
        // a new entity.
        //
        // We also allow end-user to specify a Option<Entity>. The src Entity from will *replace* the
        // data of the given Entity

        // The location of the next entity
        let location = EntityLocation::new(
            dst_archetype_index,
            dst_chunk_set_index,
            dst_free_chunk_index,
            ComponentIndex(dst_entities.len()),
        );

        let dst_entity = if let Some(dst_entity) = replace_mapping {
            // We are replacing data
            // Verify that the entity is alive.. this checks the index and version of the entity
            // The entity should be alive because World::clone_from verifies this
            debug_assert!(dst_entity_allocator.is_alive(dst_entity));
            dst_entity
        } else {
            // We are appending data, allocate a new entity
            dst_entity_allocator.create_entity()
        };

        dst_entity_locations.set(dst_entity, location);
        dst_entities.push(dst_entity);

        ArchetypeData::clone_components(
            clone_impl,
            src_world,
            src_archetype,
            src_chunk,
            src_begin_idx..src_end_idx,
            &dst_entities[dst_begin_idx.0..dst_end_idx.0],
            dst_components,
            entities_to_write,
        );

        dst_entity
    }

    /// Implements shared logic between `clone_from` and `clone_from_single`. For every component type,
    /// in the given archetype,
    #[allow(clippy::too_many_arguments)]
    fn clone_components<C: crate::world::CloneImpl>(
        clone_impl: &C,
        src_world: &crate::world::World,
        src_archetype: &ArchetypeData,
        src_chunk: &ComponentStorage,
        src_range: core::ops::Range<ComponentIndex>,
        dst_entities: &[Entity],
        dst_components: &UnsafeCell<Components>,
        entities_to_write: usize,
    ) {
        for (src_type, _) in src_archetype.description().components() {
            let dst_components = unsafe { &mut *dst_components.get() };

            // Look up what type we should transform the data into (can be the same type, meaning it should be cloned)
            let (dst_type, _) = clone_impl.map_component_type(*src_type);

            // Create a writer that will insert the data into the destination chunk
            let mut dst_component_writer = dst_components
                .get_mut(dst_type)
                .expect("ComponentResourceSet missing in clone_from")
                .writer();

            // Find the data in the source chunk
            let src_component_storage = src_chunk
                .components(*src_type)
                .expect("ComponentResourceSet missing in clone_from");

            // Now copy the data
            unsafe {
                let (src_component_chunk_data, src_element_size, _) =
                    src_component_storage.data_raw();

                // offset to the first entity we want to copy from the source chunk
                let src_data = src_component_chunk_data.add(src_element_size * src_range.start.0);

                // allocate the space we need in the destination chunk
                let dst_data = dst_component_writer.reserve_raw(entities_to_write).as_ptr();

                // Delegate the clone operation to the provided CloneImpl
                clone_impl.clone_components(
                    src_world,
                    src_chunk,
                    src_range.clone(),
                    *src_type,
                    &src_chunk.entities[src_range.start.0..src_range.end.0],
                    dst_entities,
                    src_data,
                    dst_data,
                    entities_to_write,
                );
            }
        }
    }

    /// Iterate all entities in existence by iterating across archetypes, chunk sets, and chunks
    pub(crate) fn iter_entities<'a>(&'a self) -> impl Iterator<Item = Entity> + 'a {
        self.chunk_sets.iter().flat_map(move |set| {
            set.chunks
                .iter()
                .flat_map(move |chunk| chunk.entities().iter().copied())
        })
    }

    pub(crate) fn iter_entity_locations<'a>(
        &'a self,
        archetype_index: ArchetypeIndex,
    ) -> impl Iterator<Item = (Entity, EntityLocation)> + 'a {
        self.chunk_sets
            .iter()
            .enumerate()
            .flat_map(move |(set_index, set)| {
                set.chunks
                    .iter()
                    .enumerate()
                    .flat_map(move |(chunk_index, chunk)| {
                        chunk
                            .entities()
                            .iter()
                            .enumerate()
                            .map(move |(entity_index, &entity)| {
                                (
                                    entity,
                                    EntityLocation::new(
                                        archetype_index,
                                        SetIndex(set_index),
                                        ChunkIndex(chunk_index),
                                        ComponentIndex(entity_index),
                                    ),
                                )
                            })
                    })
            })
    }

    fn push<F: FnMut(&mut Tags)>(&mut self, set: Chunkset, mut initialize: F) {
        initialize(&mut self.tags);
        self.chunk_sets.push(set);

        let index = SetIndex(self.chunk_sets.len() - 1);
        let filter = ChunksetFilterData {
            archetype_data: self,
        };
        let subscribers = self.subscribers.matches_chunkset(filter, index);

        self.chunk_sets[index].set_subscribers(subscribers);
        self.tags.validate(self.chunk_sets.len());
    }

    /// Allocates a new chunk set. Returns the index of the new set.
    ///
    /// `initialize` is expected to push the new chunkset's tag values onto the tags collection.
    pub(crate) fn alloc_chunk_set<F: FnMut(&mut Tags)>(&mut self, initialize: F) -> SetIndex {
        self.push(Chunkset::default(), initialize);
        SetIndex(self.chunk_sets.len() - 1)
    }

    /// Finds a chunk with space free for at least `minimum_space` entities, creating a chunk if needed.
    pub(crate) fn get_free_chunk(
        &mut self,
        set_index: SetIndex,
        minimum_space: usize,
    ) -> ChunkIndex {
        let count = {
            let chunks = &mut self.chunk_sets[set_index];
            let len = chunks.len();
            for (i, chunk) in chunks.iter_mut().enumerate() {
                let space_left = chunk.capacity() - chunk.len();
                if space_left >= minimum_space {
                    return ChunkIndex(i);
                }
            }
            ChunkIndex(len)
        };

        let chunk = self
            .component_layout
            .alloc_storage(ChunkId(self.id, set_index, count));
        unsafe { self.chunkset_unchecked_mut(set_index).push(chunk) };

        trace!(
            world = self.id.world().index(),
            archetype = *self.id.index(),
            chunkset = *set_index,
            chunk = *count,
            components = ?self.desc.component_names,
            tags = ?self.desc.tag_names,
            "Created chunk"
        );

        count
    }

    /// Gets the number of chunk sets stored within this archetype.
    pub fn len(&self) -> usize { self.chunk_sets.len() }

    /// Determines whether this archetype has any chunks.
    pub fn is_empty(&self) -> bool { self.len() < 1 }

    /// Gets the tag storage for all chunks in the archetype.
    pub fn tags(&self) -> &Tags { &self.tags }

    /// Mutably gets the tag storage for all chunks in the archetype.
    pub fn tags_mut(&mut self) -> &mut Tags { &mut self.tags }

    /// Gets a slice of chunksets.
    pub fn chunksets(&self) -> &[Chunkset] { &self.chunk_sets }

    /// Gets a mutable slice of chunksets.
    pub fn chunksets_mut(&mut self) -> &mut [Chunkset] { &mut self.chunk_sets }

    /// Gets a description of the component types in the archetype.
    pub fn description(&self) -> &ArchetypeDescription { &self.desc }

    pub(crate) fn defrag<F: FnMut(Entity, EntityLocation)>(
        &mut self,
        budget: &mut usize,
        mut on_moved: F,
    ) -> bool {
        trace!(
            world = self.id().world().index(),
            archetype = *self.id().index(),
            "Defragmenting archetype"
        );
        let arch_index = self.id.index();
        for (i, chunkset) in self.chunk_sets.iter_mut().enumerate() {
            let complete = chunkset.defrag(budget, |e, chunk, component| {
                on_moved(
                    e,
                    EntityLocation::new(arch_index, SetIndex(i), chunk, component),
                );
            });
            if !complete {
                return false;
            }
        }

        true
    }

    pub(crate) fn chunkset(&self, SetIndex(index): SetIndex) -> Option<&Chunkset> {
        self.chunksets().get(index)
    }

    pub(crate) fn chunkset_mut(&mut self, SetIndex(index): SetIndex) -> Option<&mut Chunkset> {
        self.chunksets_mut().get_mut(index)
    }

    pub(crate) unsafe fn chunkset_unchecked(&self, SetIndex(index): SetIndex) -> &Chunkset {
        self.chunksets().get_unchecked(index)
    }

    pub(crate) unsafe fn chunkset_unchecked_mut(
        &mut self,
        SetIndex(index): SetIndex,
    ) -> &mut Chunkset {
        self.chunksets_mut().get_unchecked_mut(index)
    }

    pub(crate) fn iter_data_slice<'a, T: Component>(
        &'a self,
    ) -> impl Iterator<Item = RefMap<&[T]>> + 'a {
        self.chunk_sets.iter().flat_map(move |set| {
            set.chunks.iter().map(move |chunk| {
                let c = chunk.components(ComponentTypeId::of::<T>()).unwrap();
                unsafe { c.data_slice::<T>() }
            })
        })
    }

    pub(crate) unsafe fn iter_data_slice_unchecked_mut<'a, T: Component>(
        &'a self,
    ) -> impl Iterator<Item = RefMapMut<&mut [T]>> + 'a {
        self.chunk_sets.iter().flat_map(move |set| {
            set.chunks.iter().map(move |chunk| {
                let c = chunk.components(ComponentTypeId::of::<T>()).unwrap();
                c.data_slice_mut::<T>()
            })
        })
    }
}

fn align_up(addr: usize, align: usize) -> usize { (addr + (align - 1)) & align.wrapping_neg() }

/// Describes the data layout for a chunk.
pub struct ComponentStorageLayout {
    capacity: usize,
    alloc_layout: std::alloc::Layout,
    data_layout: Vec<(ComponentTypeId, usize, ComponentMeta)>,
}

impl ComponentStorageLayout {
    /// The maximum number of entities that can be stored in each chunk.
    pub fn capacity(&self) -> usize { self.capacity }

    /// The components in each chunk.
    pub fn components(&self) -> &[(ComponentTypeId, usize, ComponentMeta)] { &self.data_layout }

    fn alloc_storage(&self, id: ChunkId) -> ComponentStorage {
        let storage_info = self
            .data_layout
            .iter()
            .map(|&(ty, _, ref meta)| {
                (
                    ty,
                    ComponentResourceSet {
                        ptr: AtomicRefCell::new(meta.align as *mut u8),
                        capacity: self.capacity,
                        count: UnsafeCell::new(0),
                        element_size: meta.size,
                        drop_fn: meta.drop_fn,
                        version: UnsafeCell::new(0),
                    },
                )
            })
            .collect();

        ComponentStorage {
            id,
            capacity: self.capacity,
            entities: Vec::with_capacity(self.capacity),
            component_offsets: self
                .data_layout
                .iter()
                .map(|&(ty, offset, _)| (ty, offset))
                .collect(),
            component_layout: self.alloc_layout,
            component_info: UnsafeCell::new(Components::new(storage_info)),
            component_data: None,
            subscribers: Subscribers::default(),
        }
    }
}

/// Contains chunks with the same layout and tag values.
#[derive(Default)]
pub struct Chunkset {
    chunks: Vec<ComponentStorage>,
    subscribers: Subscribers,
}

impl Deref for Chunkset {
    type Target = [ComponentStorage];

    fn deref(&self) -> &Self::Target { self.chunks.as_slice() }
}

impl DerefMut for Chunkset {
    fn deref_mut(&mut self) -> &mut Self::Target { self.chunks.as_mut_slice() }
}

impl Chunkset {
    pub(crate) fn new() -> Self {
        Self {
            chunks: Vec::new(),
            subscribers: Subscribers::default(),
        }
    }

    /// Pushes a new chunk into the set.
    pub fn push(&mut self, chunk: ComponentStorage) {
        let id = chunk.id();
        self.chunks.push(chunk);

        let index = ChunkIndex(self.chunks.len() - 1);
        let filter = ChunkFilterData {
            chunks: &self.chunks,
        };
        let mut subscribers = self.subscribers.matches_chunk(filter, index);
        subscribers.send(Event::ChunkCreated(id));
        self.chunks[index].set_subscribers(subscribers);
    }

    pub(crate) fn subscribe(&mut self, subscriber: Subscriber) {
        self.subscribers.push(subscriber.clone());

        for i in (0..self.chunks.len()).map(ChunkIndex) {
            let filter = ChunkFilterData {
                chunks: &self.chunks,
            };

            if subscriber.filter.matches_chunk(filter, i) {
                self.chunks[i].subscribe(subscriber.clone());
            }
        }
    }

    pub(crate) fn set_subscribers(&mut self, subscribers: Subscribers) {
        self.subscribers = subscribers;

        for i in (0..self.chunks.len()).map(ChunkIndex) {
            let filter = ChunkFilterData {
                chunks: &self.chunks,
            };

            let subscribers = self.subscribers.matches_chunk(filter, i);
            self.chunks[i].set_subscribers(subscribers);
        }
    }

    fn mark_modified(&mut self) {
        for chunk in self.chunks.iter_mut() {
            chunk.mark_modified();
        }
    }

    pub(crate) fn drain<R: RangeBounds<usize>>(
        &mut self,
        range: R,
    ) -> std::vec::Drain<ComponentStorage> {
        self.chunks.drain(range)
    }

    /// Gets a slice reference to occupied chunks.
    pub fn occupied(&self) -> &[ComponentStorage] {
        let mut len = self.chunks.len();
        while len > 0 {
            if unsafe { !self.chunks.get_unchecked(len - 1).is_empty() } {
                break;
            }
            len -= 1;
        }
        let (some, _) = self.chunks.as_slice().split_at(len);
        some
    }

    /// Gets a mutable slice reference to occupied chunks.
    pub fn occupied_mut(&mut self) -> &mut [ComponentStorage] {
        let mut len = self.chunks.len();
        while len > 0 {
            if unsafe { !self.chunks.get_unchecked(len - 1).is_empty() } {
                break;
            }
            len -= 1;
        }
        let (some, _) = self.chunks.as_mut_slice().split_at_mut(len);
        some
    }

    /// Defragments all chunks within the chunkset.
    ///
    /// This will compact entities down into lower index chunks, preferring to fill one
    /// chunk before moving on to the next.
    ///
    /// `budget` determines the maximum number of entities that can be moved, and is decremented
    /// as this function moves entities.
    ///
    /// `on_moved` is called when an entity is moved, with the entity's ID, new chunk index,
    /// new component index.
    ///
    /// Returns whether or not the chunkset has been fully defragmented.
    fn defrag<F: FnMut(Entity, ChunkIndex, ComponentIndex)>(
        &mut self,
        budget: &mut usize,
        mut on_moved: F,
    ) -> bool {
        let slice = self.occupied_mut();

        if slice.is_empty() {
            return true;
        }

        let mut first = 0;
        let mut last = slice.len() - 1;

        trace!("Defragmenting chunkset");

        loop {
            // find the first chunk that is not full
            while first < last && slice[first].is_full() {
                first += 1;
            }

            // find the last chunk that is not empty
            while last > first && slice[last].is_empty() {
                last -= 1;
            }

            // exit if the cursors meet; the chunkset is defragmented
            if first == last {
                return true;
            }

            // get mut references to both chunks
            let (with_first, with_last) = slice.split_at_mut(last);
            let target = &mut with_first[first];
            let source = &mut with_last[0];

            // move as many entities as we can from the last chunk into the first
            loop {
                if *budget == 0 {
                    return false;
                }

                *budget -= 1;

                // move the last entity
                let comp_index = ComponentIndex(source.len() - 1);
                let swapped = source.move_entity(target, comp_index);
                assert!(swapped.is_none());

                // notify move
                on_moved(
                    *target.entities.last().unwrap(),
                    ChunkIndex(first),
                    comp_index,
                );

                // exit if we cant move any more
                if target.is_full() || source.is_empty() {
                    break;
                }
            }
        }
    }

    pub(crate) fn chunk(&self, ChunkIndex(index): ChunkIndex) -> Option<&ComponentStorage> {
        self.chunks.get(index)
    }

    pub(crate) fn chunk_mut(
        &mut self,
        ChunkIndex(index): ChunkIndex,
    ) -> Option<&mut ComponentStorage> {
        self.chunks.get_mut(index)
    }

    pub(crate) unsafe fn chunk_unchecked(
        &self,
        ChunkIndex(index): ChunkIndex,
    ) -> &ComponentStorage {
        self.chunks.get_unchecked(index)
    }

    pub(crate) unsafe fn chunk_unchecked_mut(
        &mut self,
        ChunkIndex(index): ChunkIndex,
    ) -> &mut ComponentStorage {
        self.chunks.get_unchecked_mut(index)
    }
}

/// Unique ID of a chunk.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct ChunkId(ArchetypeId, SetIndex, ChunkIndex);

impl ChunkId {
    pub(crate) fn new(archetype: ArchetypeId, set: SetIndex, index: ChunkIndex) -> Self {
        ChunkId(archetype, set, index)
    }

    pub fn archetype_id(&self) -> ArchetypeId { self.0 }

    pub(crate) fn set(&self) -> SetIndex { self.1 }

    pub(crate) fn index(&self) -> ChunkIndex { self.2 }
}

/// A set of component slices located on a chunk.
pub struct Components(SmallVec<[(ComponentTypeId, ComponentResourceSet); 5]>);

impl Components {
    pub(crate) fn new(mut data: SmallVec<[(ComponentTypeId, ComponentResourceSet); 5]>) -> Self {
        data.sort_by_key(|&(t, _)| t);
        Self(data)
    }

    /// Gets a component slice accessor for the specified component type.
    #[inline]
    pub fn get(&self, type_id: ComponentTypeId) -> Option<&ComponentResourceSet> {
        self.0
            .binary_search_by_key(&type_id, |&(t, _)| t)
            .ok()
            .map(|i| unsafe { &self.0.get_unchecked(i).1 })
    }

    /// Gets a mutable component slice accessor for the specified component type.
    #[inline]
    pub fn get_mut(&mut self, type_id: ComponentTypeId) -> Option<&mut ComponentResourceSet> {
        self.0
            .binary_search_by_key(&type_id, |&(t, _)| t)
            .ok()
            .map(move |i| unsafe { &mut self.0.get_unchecked_mut(i).1 })
    }

    fn iter(&mut self) -> impl Iterator<Item = &(ComponentTypeId, ComponentResourceSet)> + '_ {
        self.0.iter()
    }

    fn iter_mut(
        &mut self,
    ) -> impl Iterator<Item = &mut (ComponentTypeId, ComponentResourceSet)> + '_ {
        self.0.iter_mut()
    }

    fn drain(&mut self) -> impl Iterator<Item = (ComponentTypeId, ComponentResourceSet)> + '_ {
        self.0.drain(..)
    }
}

/// Stores a chunk of entities and their component data of a specific data layout.
pub struct ComponentStorage {
    id: ChunkId,
    capacity: usize,
    entities: Vec<Entity>,
    component_layout: std::alloc::Layout,
    component_offsets: FxHashMap<ComponentTypeId, usize>,
    component_info: UnsafeCell<Components>,
    component_data: Option<NonNull<u8>>,
    subscribers: Subscribers,
}

pub struct StorageWriter<'a> {
    initial_count: usize,
    storage: &'a mut ComponentStorage,
}

impl<'a> StorageWriter<'a> {
    pub fn get(&mut self) -> (&mut Vec<Entity>, &UnsafeCell<Components>) {
        (&mut self.storage.entities, &self.storage.component_info)
    }
}

impl<'a> Drop for StorageWriter<'a> {
    fn drop(&mut self) {
        self.storage.update_count_gauge();
        for &entity in self.storage.entities.iter().skip(self.initial_count) {
            self.storage
                .subscribers
                .send(Event::EntityInserted(entity, self.storage.id()));
        }
    }
}

impl ComponentStorage {
    /// Gets the unique ID of the chunk.
    pub fn id(&self) -> ChunkId { self.id }

    /// Gets the number of entities stored in the chunk.
    pub fn len(&self) -> usize { self.entities.len() }

    /// Gets the maximum number of entities that can be stored in the chunk.
    pub fn capacity(&self) -> usize { self.capacity }

    /// Determines if the chunk is full.
    pub fn is_full(&self) -> bool { self.len() >= self.capacity }

    /// Determines if the chunk is empty.
    pub fn is_empty(&self) -> bool { self.entities.len() == 0 }

    /// Determines if the internal memory for this chunk has been allocated.
    pub fn is_allocated(&self) -> bool { self.component_data.is_some() }

    pub(crate) fn subscribe(&mut self, subscriber: Subscriber) {
        self.subscribers.push(subscriber);
    }

    pub(crate) fn set_subscribers(&mut self, subscribers: Subscribers) {
        self.subscribers = subscribers;
    }

    /// Gets a slice reference containing the IDs of all entities stored in the chunk.
    pub fn entities(&self) -> &[Entity] { self.entities.as_slice() }

    /// Gets a component accessor for the specified component type.
    pub fn components(&self, component_type: ComponentTypeId) -> Option<&ComponentResourceSet> {
        unsafe { &*self.component_info.get() }.get(component_type)
    }

    /// Increments all component versions, forcing the chunk to be seen as modified for all queries.
    fn mark_modified(&mut self) {
        unsafe {
            let components = &mut *self.component_info.get();
            for (_, component) in components.iter_mut() {
                // touch each slice mutably to increment its version
                let _ = component.data_raw_mut();
            }
        }
    }

    /// Removes an entity from the chunk by swapping it with the last entry.
    ///
    /// Returns the ID of the entity which was swapped into the removed entity's position.
    pub fn swap_remove(
        &mut self,
        ComponentIndex(index): ComponentIndex,
        drop: bool,
    ) -> Option<Entity> {
        let removed = self.entities.swap_remove(index);
        for (_, component) in unsafe { &mut *self.component_info.get() }.iter_mut() {
            component.writer().swap_remove(index, drop);
        }

        self.subscribers
            .send(Event::EntityRemoved(removed, self.id()));
        self.update_count_gauge();

        if self.entities.len() > index {
            Some(*self.entities.get(index).unwrap())
        } else {
            if self.is_empty() {
                self.free();
            }

            None
        }
    }

    /// Moves an entity from this chunk into a target chunk, moving all compatable components into
    /// the target chunk. Any components left over will be dropped.
    ///
    /// Returns the ID of the entity which was swapped into the removed entity's position.
    pub fn move_entity(
        &mut self,
        target: &mut ComponentStorage,
        index: ComponentIndex,
    ) -> Option<Entity> {
        debug_assert!(*index < self.len());
        debug_assert!(!target.is_full());
        if !target.is_allocated() {
            target.allocate();
        }

        trace!(index = *index, source = ?self.id, destination = ?target.id, "Moving entity");

        let entity = unsafe { *self.entities.get_unchecked(*index) };
        target.entities.push(entity);

        let self_components = unsafe { &mut *self.component_info.get() };
        let target_components = unsafe { &mut *target.component_info.get() };

        for (comp_type, accessor) in self_components.iter_mut() {
            if let Some(target_accessor) = target_components.get_mut(*comp_type) {
                // move the component into the target chunk
                unsafe {
                    let (ptr, element_size, _) = accessor.data_raw();
                    let component = ptr.add(element_size * *index);
                    target_accessor
                        .writer()
                        .push_raw(NonNull::new_unchecked(component), 1);
                }
            } else {
                // drop the component rather than move it
                unsafe { accessor.writer().drop_in_place(index) };
            }
        }

        // remove the entity from this chunk
        let removed = self.swap_remove(index, false);

        target
            .subscribers
            .send(Event::EntityInserted(entity, target.id()));
        target.update_count_gauge();

        removed
    }

    /// Gets mutable references to the internal data of the chunk.
    pub fn writer(&mut self) -> StorageWriter {
        if !self.is_allocated() {
            self.allocate();
        }
        StorageWriter {
            initial_count: self.entities.len(),
            storage: self,
        }
    }

    fn free(&mut self) {
        debug_assert!(self.is_allocated());
        debug_assert_eq!(0, self.len());

        self.entities.shrink_to_fit();

        trace!(
            world = self.id.archetype_id().world().index(),
            archetype = *self.id.archetype_id().index(),
            chunkset = *self.id.set(),
            chunk = *self.id.index(),
            layout = ?self.component_layout,
            "Freeing chunk memory"
        );

        // Safety Note:
        // accessors are left with pointers pointing to invalid memory (although aligned properly)
        // the slices returned from these accessors will be empty though, so no code
        // should ever dereference these pointers

        // free component memory
        unsafe {
            let ptr = self.component_data.take().unwrap();

            if self.component_layout.size() > 0 {
                std::alloc::dealloc(ptr.as_ptr(), self.component_layout);
            }
        }

        self.update_mem_gauge();
    }

    fn allocate(&mut self) {
        debug_assert!(!self.is_allocated());

        trace!(
            world = self.id.archetype_id().world().index(),
            archetype = *self.id.archetype_id().index(),
            chunkset = *self.id.set(),
            chunk = *self.id.index(),
            layout = ?self.component_layout,
            "Allocating chunk memory"
        );
        self.entities.reserve_exact(self.capacity);

        unsafe {
            // allocating backing store
            if self.component_layout.size() > 0 {
                let ptr = std::alloc::alloc(self.component_layout);
                self.component_data = Some(NonNull::new_unchecked(ptr));

                // update accessor pointers
                for (type_id, component) in (&mut *self.component_info.get()).iter_mut() {
                    let &offset = self.component_offsets.get(type_id).unwrap();
                    *component.ptr.get_mut() = ptr.add(offset);
                }
            } else {
                self.component_data =
                    Some(NonNull::new(self.component_layout.align() as *mut u8).unwrap());
            }
        }

        self.update_mem_gauge();
    }

    fn update_mem_gauge(&self) {
        #[cfg(feature = "metrics")]
        {
            use std::convert::TryInto;
            metrics::gauge!(
                "chunk_memory",
                if self.is_allocated() { self.component_layout.size().try_into().unwrap() } else { 0 },
                "world" => self.id.archetype_id().world().index().to_string(),
                "archetype" => self.id.archetype_id().index().to_string(),
                "chunkset" => self.id.set().to_string(),
                "chunk" => self.id.index().to_string()
            );
        }
    }

    fn update_count_gauge(&self) {
        #[cfg(feature = "metrics")]
        {
            use std::convert::TryInto;
            metrics::gauge!(
                "entity_count",
                self.len().try_into().unwrap(),
                "world" => self.id.archetype_id().world().index().to_string(),
                "archetype" => self.id.archetype_id().index().to_string(),
                "chunkset" => self.id.set().to_string(),
                "chunk" => self.id.index().to_string()
            );
        }
    }
}

unsafe impl Sync for ComponentStorage {}

unsafe impl Send for ComponentStorage {}

impl Drop for ComponentStorage {
    fn drop(&mut self) {
        if let Some(ptr) = self.component_data {
            // run the drop functions of all components
            for (_, info) in unsafe { &mut *self.component_info.get() }.drain() {
                if let Some(drop_fn) = info.drop_fn {
                    let ptr = info.ptr.get_mut();
                    for i in 0..self.len() {
                        unsafe {
                            drop_fn(ptr.add(info.element_size * i));
                        }
                    }
                }
            }

            for e in &self.entities {
                self.subscribers.send(Event::EntityRemoved(*e, self.id()));
            }

            self.update_count_gauge();

            // free the chunk's memory
            if self.component_layout.size() > 0 {
                unsafe {
                    std::alloc::dealloc(ptr.as_ptr(), self.component_layout);
                }
            }
        }
    }
}

/// Provides raw access to component data slices.
#[repr(align(64))]
pub struct ComponentResourceSet {
    ptr: AtomicRefCell<*mut u8>,
    element_size: usize,
    count: UnsafeCell<usize>,
    capacity: usize,
    drop_fn: Option<fn(*mut u8)>,
    version: UnsafeCell<u64>,
}

impl ComponentResourceSet {
    /// Gets the version of the component slice.
    pub fn version(&self) -> u64 { unsafe { *self.version.get() } }

    /// Gets a raw pointer to the start of the component slice.
    ///
    /// Returns a tuple containing `(pointer, element_size, count)`.
    ///
    /// # Safety
    ///
    /// Access to the component data within the slice is runtime borrow checked in debug builds.
    /// This call will panic if borrowing rules are broken in debug, and is undefined behavior in release.
    pub unsafe fn data_raw(&self) -> (Ref<*mut u8>, usize, usize) {
        (self.ptr.get(), self.element_size, *self.count.get())
    }

    /// Gets a raw pointer to the start of the component slice.
    ///
    /// Returns a tuple containing `(pointer, element_size, count)`.
    ///
    /// # Safety
    ///
    /// Access to the component data within the slice is runtime borrow checked in debug builds.
    /// This call will panic if borrowing rules are broken in debug, and is undefined behavior in release.
    ///
    /// # Panics
    ///
    /// Will panic when an internal u64 counter overflows.
    /// It will happen in 50000 years if you do 10000 mutations a millisecond.
    pub unsafe fn data_raw_mut(&self) -> (RefMut<*mut u8>, usize, usize) {
        // this version increment is not thread safe
        // - but the pointer `get_mut` ensures exclusive access at runtime
        let ptr = self.ptr.get_mut();
        *self.version.get() = next_version();
        (ptr, self.element_size, *self.count.get())
    }

    /// Gets a shared reference to the slice of components.
    ///
    /// # Safety
    ///
    /// Ensure that `T` is representative of the component data actually stored.
    ///
    /// Access to the component data within the slice is runtime borrow checked.
    /// This call will panic if borrowing rules are broken.
    pub unsafe fn data_slice<T>(&self) -> RefMap<&[T]> {
        let (ptr, _size, count) = self.data_raw();
        ptr.map_into(|&ptr| std::slice::from_raw_parts(ptr as *const _ as *const T, count))
    }

    /// Gets a mutable reference to the slice of components.
    ///
    /// # Safety
    ///
    /// Ensure that `T` is representative of the component data actually stored.
    ///
    /// Access to the component data within the slice is runtime borrow checked.
    /// This call will panic if borrowing rules are broken.
    ///
    /// # Panics
    ///
    /// Will panic when an internal u64 counter overflows.
    /// It will happen in 50000 years if you do 10000 mutations a millisecond.
    pub unsafe fn data_slice_mut<T>(&self) -> RefMapMut<&mut [T]> {
        let (ptr, _size, count) = self.data_raw_mut();
        ptr.map_into(|&mut ptr| std::slice::from_raw_parts_mut(ptr as *mut _ as *mut T, count))
    }

    /// Creates a writer for pushing components into or removing from the vec.
    pub fn writer(&mut self) -> ComponentWriter { ComponentWriter::new(self) }
}

impl Debug for ComponentResourceSet {
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
        write!(
            f,
            "ComponentResourceSet {{ ptr: {:?}, element_size: {}, count: {}, capacity: {}, version: {} }}",
            *self.ptr.get(),
            self.element_size,
            unsafe { *self.count.get() },
            self.capacity,
            self.version()
        )
    }
}

/// Provides methods adding or removing components from a component vec.
pub struct ComponentWriter<'a> {
    accessor: &'a ComponentResourceSet,
    ptr: RefMut<'a, *mut u8>,
}

impl<'a> ComponentWriter<'a> {
    fn new(accessor: &'a ComponentResourceSet) -> ComponentWriter<'a> {
        Self {
            accessor,
            ptr: accessor.ptr.get_mut(),
        }
    }

    /// Increases the length of the associated `ComponentResourceSet` by `count`
    /// and returns a pointer to the start of the memory that is reserved as a result.
    ///
    /// # Safety
    ///
    /// Ensure the memory returned by this function is properly initialized before calling
    /// any other storage function. Ensure that the data written into the returned pointer
    /// is representative of the component types stored in the associated ComponentResourceSet.
    ///
    /// # Panics
    ///
    /// Will panic when an internal u64 counter overflows.
    /// It will happen in 50000 years if you do 10000 mutations a millisecond.
    pub(crate) unsafe fn reserve_raw(&mut self, count: usize) -> NonNull<u8> {
        debug_assert!((*self.accessor.count.get() + count) <= self.accessor.capacity);
        let ptr = self
            .ptr
            .add(*self.accessor.count.get() * self.accessor.element_size);
        *self.accessor.count.get() += count;
        *self.accessor.version.get() = next_version();
        NonNull::new_unchecked(ptr)
    }

    /// Pushes new components onto the end of the vec.
    ///
    /// # Safety
    ///
    /// Ensure the components pointed to by `components` are representative
    /// of the component types stored in the vec.
    ///
    /// This function will _copy_ all elements into the chunk. If the source is not `Copy`,
    /// the caller must then `mem::forget` the source such that the destructor does not run
    /// on the original data.
    ///
    /// # Panics
    ///
    /// Will panic when an internal u64 counter overflows.
    /// It will happen in 50000 years if you do 10000 mutations a millisecond.
    pub unsafe fn push_raw(&mut self, components: NonNull<u8>, count: usize) {
        debug_assert!((*self.accessor.count.get() + count) <= self.accessor.capacity);
        std::ptr::copy_nonoverlapping(
            components.as_ptr(),
            self.ptr
                .add(*self.accessor.count.get() * self.accessor.element_size),
            count * self.accessor.element_size,
        );
        *self.accessor.count.get() += count;
        *self.accessor.version.get() = next_version();
    }

    /// Pushes new components onto the end of the vec.
    ///
    /// # Safety
    ///
    /// Ensure that the type `T` is representative of the component types stored in the vec.
    ///
    /// This function will _copy_ all elements of `T` into the chunk. If `T` is not `Copy`,
    /// the caller must then `mem::forget` the source such that the destructor does not run
    /// on the original data.
    pub unsafe fn push<T: Component>(&mut self, components: &[T]) {
        self.push_raw(
            NonNull::new_unchecked(components.as_ptr() as *mut u8),
            components.len(),
        );
    }

    /// Removes the component at the specified index by swapping it with the last component.
    pub fn swap_remove(&mut self, index: usize, drop: bool) {
        unsafe {
            let size = self.accessor.element_size;
            let to_remove = self.ptr.add(size * index);
            if drop {
                if let Some(drop_fn) = self.accessor.drop_fn {
                    drop_fn(to_remove);
                }
            }

            let count = *self.accessor.count.get();
            if index < count - 1 {
                let swap_target = self.ptr.add(size * (count - 1));
                std::ptr::copy_nonoverlapping(swap_target, to_remove, size);
            }

            *self.accessor.count.get() -= 1;
        }
    }

    /// Drops the component stored at `index` without moving any other data or
    /// altering the number of elements.
    ///
    /// # Safety
    ///
    /// Ensure that this function is only ever called once on a given index.
    pub unsafe fn drop_in_place(&mut self, ComponentIndex(index): ComponentIndex) {
        if let Some(drop_fn) = self.accessor.drop_fn {
            let size = self.accessor.element_size;
            let to_remove = self.ptr.add(size * index);
            drop_fn(to_remove);
        }
    }
}

/// A vector of tag values of a single type.
///
/// Each element in the vector represents the value of tag for
/// the chunk with the corresponding index.
pub struct TagStorage {
    ptr: NonNull<u8>,
    capacity: usize,
    len: usize,
    element: TagMeta,
}

impl TagStorage {
    pub(crate) fn new(element: TagMeta) -> Self {
        let capacity = if element.size == 0 { !0 } else { 4 };

        let ptr = unsafe {
            if element.size > 0 {
                let layout =
                    std::alloc::Layout::from_size_align(capacity * element.size, element.align)
                        .unwrap();
                NonNull::new_unchecked(std::alloc::alloc(layout))
            } else {
                NonNull::new_unchecked(element.align as *mut u8)
            }
        };

        TagStorage {
            ptr,
            capacity,
            len: 0,
            element,
        }
    }

    /// Gets the element metadata.
    pub fn element(&self) -> &TagMeta { &self.element }

    /// Gets the number of tags contained within the vector.
    pub fn len(&self) -> usize { self.len }

    /// Determines if the vector is empty.
    pub fn is_empty(&self) -> bool { self.len() < 1 }

    /// Allocates uninitialized memory for a new element.
    ///
    /// # Safety
    ///
    /// A valid element must be written into the returned address before the
    /// tag storage is next accessed.
    pub unsafe fn alloc_ptr(&mut self) -> *mut u8 {
        if self.len == self.capacity {
            self.grow();
        }

        let ptr = if self.element.size > 0 {
            self.ptr.as_ptr().add(self.len * self.element.size)
        } else {
            self.element.align as *mut u8
        };

        self.len += 1;
        ptr
    }

    /// Pushes a new tag onto the end of the vector.
    ///
    /// # Safety
    ///
    /// Ensure the tag pointed to by `ptr` is representative
    /// of the tag types stored in the vec.
    ///
    /// `ptr` must not point to a location already within the vector.
    ///
    /// The value at `ptr` is _copied_ into the tag vector. If the value
    /// is not `Copy`, then the caller must ensure that the original value
    /// is forgotten with `mem::forget` such that the finalizer is not called
    /// twice.
    pub unsafe fn push_raw(&mut self, ptr: *const u8) {
        if self.len == self.capacity {
            self.grow();
        }

        if self.element.size > 0 {
            let dst = self.ptr.as_ptr().add(self.len * self.element.size);
            std::ptr::copy_nonoverlapping(ptr, dst, self.element.size);
        }

        self.len += 1;
    }

    /// Pushes a new tag onto the end of the vector.
    ///
    /// # Safety
    ///
    /// Ensure that the type `T` is representative of the tag type stored in the vec.
    pub unsafe fn push<T: Tag>(&mut self, value: T) {
        debug_assert!(
            size_of::<T>() == self.element.size,
            "incompatible element data size"
        );
        self.push_raw(&value as *const T as *const u8);
        std::mem::forget(value);
    }

    /// Gets a raw pointer to the start of the tag slice.
    ///
    /// Returns a tuple containing `(pointer, element_size, count)`.
    ///
    /// # Safety
    /// This function returns a raw pointer with the size and length.
    /// Ensure that you do not access outside these bounds for this pointer.
    pub unsafe fn data_raw(&self) -> (NonNull<u8>, usize, usize) {
        (self.ptr, self.element.size, self.len)
    }

    /// Gets a shared reference to the slice of tags.
    ///
    /// # Safety
    ///
    /// Ensure that `T` is representative of the tag data actually stored.
    ///
    /// Access to the tag data within the slice is runtime borrow checked.
    /// This call will panic if borrowing rules are broken.
    pub unsafe fn data_slice<T>(&self) -> &[T] {
        debug_assert!(
            size_of::<T>() == self.element.size,
            "incompatible element data size"
        );
        std::slice::from_raw_parts(self.ptr.as_ptr() as *const T, self.len)
    }

    /// Drop the storage without dropping the tags contained in the storage
    pub(crate) fn forget_data(mut self) {
        // this is a bit of a hack, but it makes the Drop impl not drop the elements
        self.element.drop_fn = None;
    }

    fn grow(&mut self) {
        assert!(self.element.size != 0, "capacity overflow");
        unsafe {
            let (new_cap, ptr) = {
                let layout = std::alloc::Layout::from_size_align(
                    self.capacity * self.element.size,
                    self.element.align,
                )
                .unwrap();
                let new_cap = 2 * self.capacity;
                let ptr =
                    std::alloc::realloc(self.ptr.as_ptr(), layout, new_cap * self.element.size);

                (new_cap, ptr)
            };

            if ptr.is_null() {
                tracing::error!("out of memory");
                std::process::abort()
            }

            self.ptr = NonNull::new_unchecked(ptr);
            self.capacity = new_cap;
        }
    }
}

unsafe impl Sync for TagStorage {}

unsafe impl Send for TagStorage {}

impl Drop for TagStorage {
    fn drop(&mut self) {
        if self.element.size > 0 {
            let ptr = self.ptr.as_ptr();

            unsafe {
                if let Some(drop_fn) = self.element.drop_fn {
                    for i in 0..self.len {
                        drop_fn(ptr.add(i * self.element.size));
                    }
                }
                let layout = std::alloc::Layout::from_size_align_unchecked(
                    self.element.size * self.capacity,
                    self.element.align,
                );
                std::alloc::dealloc(ptr, layout);
            }
        }
    }
}

impl Debug for TagStorage {
    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
        write!(
            f,
            "TagStorage {{ element_size: {}, count: {}, capacity: {} }}",
            self.element.size, self.len, self.capacity
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::num::Wrapping;

    #[derive(Copy, Clone, PartialEq, Debug)]
    struct ZeroSize;

    #[test]
    pub fn create() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut archetypes = Storage::new(WorldId::default());

        let mut desc = ArchetypeDescription::default();
        desc.register_tag::<usize>();
        desc.register_component::<isize>();

        let (_arch_id, data) = archetypes.alloc_archetype(desc);
        let set = data.alloc_chunk_set(|tags| unsafe {
            tags.get_mut(TagTypeId::of::<usize>()).unwrap().push(1isize)
        });

        let chunk_index = data.get_free_chunk(set, 1);
        let components = data
            .chunkset_mut(set)
            .unwrap()
            .chunk_mut(chunk_index)
            .unwrap();
        let mut writer = components.writer();
        let (chunk_entities, chunk_components) = writer.get();

        chunk_entities.push(Entity::new(1, Wrapping(0)));
        unsafe {
            (&mut *chunk_components.get())
                .get_mut(ComponentTypeId::of::<isize>())
                .unwrap()
                .writer()
                .push(&[1usize]);
        }
    }

    #[test]
    pub fn create_lazy_allocated() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut archetypes = Storage::new(WorldId::default());

        let mut desc = ArchetypeDescription::default();
        desc.register_tag::<usize>();
        desc.register_component::<isize>();

        let (_arch_id, data) = archetypes.alloc_archetype(desc);
        let set = data.alloc_chunk_set(|tags| unsafe {
            tags.get_mut(TagTypeId::of::<usize>()).unwrap().push(1isize)
        });

        let chunk_index = data.get_free_chunk(set, 1);
        let chunk = data
            .chunkset_mut(set)
            .unwrap()
            .chunk_mut(chunk_index)
            .unwrap();

        assert!(!chunk.is_allocated());

        chunk.writer();

        assert!(chunk.is_allocated());
    }

    #[test]
    pub fn create_free_when_empty() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut archetypes = Storage::new(WorldId::default());

        let mut desc = ArchetypeDescription::default();
        desc.register_tag::<usize>();
        desc.register_component::<isize>();

        let (_arch_id, data) = archetypes.alloc_archetype(desc);
        let set = data.alloc_chunk_set(|tags| unsafe {
            tags.get_mut(TagTypeId::of::<usize>()).unwrap().push(1isize)
        });

        let chunk_index = data.get_free_chunk(set, 1);
        let chunk = data
            .chunkset_mut(set)
            .unwrap()
            .chunk_mut(chunk_index)
            .unwrap();

        assert!(!chunk.is_allocated());

        {
            let mut writer = chunk.writer();
            let (chunk_entities, chunk_components) = writer.get();

            chunk_entities.push(Entity::new(1, Wrapping(0)));
            unsafe {
                (&mut *chunk_components.get())
                    .get_mut(ComponentTypeId::of::<isize>())
                    .unwrap()
                    .writer()
                    .push(&[1usize]);
            }
        }

        assert!(chunk.is_allocated());

        chunk.swap_remove(ComponentIndex(0), true);

        assert!(!chunk.is_allocated());
    }

    #[test]
    pub fn read_components() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut archetypes = Storage::new(WorldId::default());

        let mut desc = ArchetypeDescription::default();
        desc.register_component::<isize>();
        desc.register_component::<usize>();
        desc.register_component::<ZeroSize>();

        let (_arch_id, data) = archetypes.alloc_archetype(desc);
        let set = data.alloc_chunk_set(|_| {});
        let chunk_index = data.get_free_chunk(set, 1);
        let components = data
            .chunkset_mut(set)
            .unwrap()
            .chunk_mut(chunk_index)
            .unwrap();

        let entities = [
            (Entity::new(1, Wrapping(0)), 1isize, 1usize, ZeroSize),
            (Entity::new(2, Wrapping(0)), 2isize, 2usize, ZeroSize),
            (Entity::new(3, Wrapping(0)), 3isize, 3usize, ZeroSize),
        ];

        let mut writer = components.writer();
        let (chunk_entities, chunk_components) = writer.get();
        for (entity, c1, c2, c3) in entities.iter() {
            chunk_entities.push(*entity);
            unsafe {
                (&mut *chunk_components.get())
                    .get_mut(ComponentTypeId::of::<isize>())
                    .unwrap()
                    .writer()
                    .push(&[*c1]);
                (&mut *chunk_components.get())
                    .get_mut(ComponentTypeId::of::<usize>())
                    .unwrap()
                    .writer()
                    .push(&[*c2]);
                (&mut *chunk_components.get())
                    .get_mut(ComponentTypeId::of::<ZeroSize>())
                    .unwrap()
                    .writer()
                    .push(&[*c3]);
            }
        }

        unsafe {
            for (i, c) in (*chunk_components.get())
                .get(ComponentTypeId::of::<isize>())
                .unwrap()
                .data_slice::<isize>()
                .iter()
                .enumerate()
            {
                assert_eq!(entities[i].1, *c);
            }

            for (i, c) in (*chunk_components.get())
                .get(ComponentTypeId::of::<usize>())
                .unwrap()
                .data_slice::<usize>()
                .iter()
                .enumerate()
            {
                assert_eq!(entities[i].2, *c);
            }

            for (i, c) in (*chunk_components.get())
                .get(ComponentTypeId::of::<ZeroSize>())
                .unwrap()
                .data_slice::<ZeroSize>()
                .iter()
                .enumerate()
            {
                assert_eq!(entities[i].3, *c);
            }
        }
    }

    #[test]
    pub fn read_tags() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut archetypes = Storage::new(WorldId::default());

        let mut desc = ArchetypeDescription::default();
        desc.register_tag::<isize>();
        desc.register_tag::<ZeroSize>();

        let (_arch_id, data) = archetypes.alloc_archetype(desc);

        let tag_values = [(0isize, ZeroSize), (1isize, ZeroSize), (2isize, ZeroSize)];

        for (t1, t2) in tag_values.iter() {
            data.alloc_chunk_set(|tags| {
                unsafe { tags.get_mut(TagTypeId::of::<isize>()).unwrap().push(*t1) };
                unsafe { tags.get_mut(TagTypeId::of::<ZeroSize>()).unwrap().push(*t2) };
            });
        }

        unsafe {
            let tags1 = data
                .tags()
                .get(TagTypeId::of::<isize>())
                .unwrap()
                .data_slice::<isize>();
            assert_eq!(tags1.len(), tag_values.len());
            for (i, t) in tags1.iter().enumerate() {
                assert_eq!(tag_values[i].0, *t);
            }

            let tags2 = data
                .tags()
                .get(TagTypeId::of::<ZeroSize>())
                .unwrap()
                .data_slice::<ZeroSize>();
            assert_eq!(tags2.len(), tag_values.len());
            for (i, t) in tags2.iter().enumerate() {
                assert_eq!(tag_values[i].1, *t);
            }
        }
    }

    #[test]
    pub fn create_zero_size_tags() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut archetypes = Storage::new(WorldId::default());

        let mut desc = ArchetypeDescription::default();
        desc.register_tag::<ZeroSize>();
        desc.register_component::<isize>();

        let (_arch_id, data) = archetypes.alloc_archetype(desc);
        let set = data.alloc_chunk_set(|tags| unsafe {
            tags.get_mut(TagTypeId::of::<ZeroSize>())
                .unwrap()
                .push(ZeroSize);
        });

        let chunk_index = data.get_free_chunk(set, 1);
        let components = data
            .chunkset_mut(set)
            .unwrap()
            .chunk_mut(chunk_index)
            .unwrap();
        let mut writer = components.writer();
        let (chunk_entities, chunk_components) = writer.get();

        chunk_entities.push(Entity::new(1, Wrapping(0)));
        unsafe {
            (&mut *chunk_components.get())
                .get_mut(ComponentTypeId::of::<isize>())
                .unwrap()
                .writer()
                .push(&[1usize]);
        }
    }

    #[test]
    pub fn create_zero_size_components() {
        let _ = tracing_subscriber::fmt::try_init();

        let mut archetypes = Storage::new(WorldId::default());

        let mut desc = ArchetypeDescription::default();
        desc.register_tag::<usize>();
        desc.register_component::<ZeroSize>();

        let (_arch_id, data) = archetypes.alloc_archetype(desc);
        let set = data.alloc_chunk_set(|tags| unsafe {
            tags.get_mut(TagTypeId::of::<usize>()).unwrap().push(1isize);
        });

        let chunk_index = data.get_free_chunk(set, 1);
        let components = data
            .chunkset_mut(set)
            .unwrap()
            .chunk_mut(chunk_index)
            .unwrap();
        let mut writer = components.writer();
        let (chunk_entities, chunk_components) = writer.get();

        chunk_entities.push(Entity::new(1, Wrapping(0)));
        unsafe {
            (&mut *chunk_components.get())
                .get_mut(ComponentTypeId::of::<ZeroSize>())
                .unwrap()
                .writer()
                .push(&[ZeroSize]);
        }
    }
}