1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
use smallvec::{SmallVec};
use std::{i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, slice, str};
use std::any::{type_name};
use std::cell::{Ref};
use std::cmp::{min, Ordering};
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::convert::{TryFrom, TryInto};
use std::error::{Error};
use std::hash::{BuildHasher, Hash};
use std::io::{Write};
use std::iter::{Extend, FromIterator, IntoIterator};
use std::ffi::{CString, CStr, OsString, OsStr};
use std::marker::{PhantomData};
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::path::{Path, PathBuf};
use std::slice::{SliceIndex};
use super::code::{Coro, GFn};
use super::collections::{Arr, DequeAccess, DequeOps, Deque, Str, Tab};
use super::class::{Class, Obj};
use super::engine::{
	glsp, RData, RFn, RGlobal, RGlobalRef, RGlobalRefMut,
	RRef, RRefMut, RRoot, stock_syms::*, Sym
};
use super::error::{GError, GResult};
use super::eval::{EnvMode, Expander};
use super::gc::{Raw, Root, Slot};
use super::iter::{GIter, Iterable, GIterLen};
use super::val::{Num, Val};

/*
this module defines:
- the IntoVal and FromVal traits, with all of their built-in implementations
- the pub(crate) wrap() function and its supporting traits, which are used to convert arbitrary 
  Rust functions into boxed type-erased functions which can be called by GameLisp

todos:
- should FromVal support Option<T> and Rest<T> in tuples?
- should we support Either<T, U>? this will require argument type conversions to fail more cheaply
  than they currently do, perhaps returning a dedicated error type rather than GError
	- this would also enable us to provide a method Val::is::<T>(&self) -> bool
- should FromVal::from_val accept any parameter which implements AsRef<Val>, so that it
  can accept either Val or &Val when called explicitly?
	- for now, i'm reluctant to add the extra monomorphization cost
- it ought to be possible to add a write_back() method to FromArgRef, and implement that method
  for any &mut T: FromVal + IntoVal, as well as &mut str etc. this would enable us to support
  more Rust apis without any manual translation. however...
	- i'm skeptical there would be much demand for it, and perf would be poor
	- it would require us to remove the blanket implementations which let the user move
	  RData on and off the heap using t.into_val() and T::from_val()
	- it could only support arrs, strs and tabs - not, e.g., &mut Sym
	- we probably want to encourage use of RData rather than FromVal/ToVal
	- non-self &mut references are fairly uncommon in Rust apis anyway, and when they do exist
	  they're usually only there as a performance optimization
- a lesser version of that feature would be to support &T args for all T: FromVal, constructing
  the T on the stack. however, this would require us to yank our blanket FromVal impl for all 
  'static types, which would be a shame
*/


//-------------------------------------------------------------------------------------------------
// marker traits
//-------------------------------------------------------------------------------------------------

/*
if this style of specialization gets yanked from rustc, we have various backup plans...

IntoVal:

	implement two traits on wrappable function pointers for transforming their return value:
		glsp::bind_rfn("name", name.move_rdata())?;
		glsp::bind_rfn("name", name.move_inner_rdata())?;

	move_inner_rdata() would be implemented for standard library types like Option<T>, Vec<T>,
	Result<T, E> (with a dynamic "specialization" for GResult), and so on. move_rdata() could 
	use type_name::<T>() to lint against moving types like Option<T> onto the heap, with a 
	move_rdata_force() method on the same trait to override the lint.

	we would also implement analogous methods .into_rdata() and .into_inner_rdata() on the
	return types themselves

FromVal, FromArg, FromArgRef:
	
	we could potentially emit code in the macro-generated impls which manually checks
	whether *any* argument is an RData before converting it, in which case it's tested for
	a matching type id, proceeding with the original conversion if there's a mismatch.
	(this would obviously carry a non-trivial performance cost)

	the specialization for &T where T: RGlobal would be abandoned. we could either capture
	globals using an explicit wrapper type, probably called Res<T> and ResMut<T>, or we could
	go back to requiring the user to define their global types using a macro
*/

#[doc(hidden)]
#[rustc_unsafe_specialization_marker]
pub trait StaticMarker: 'static { }
impl<T: 'static + ?Sized> StaticMarker for T { }

#[doc(hidden)]
#[rustc_unsafe_specialization_marker]
pub trait RGlobalMarker: RGlobal { }
impl<T: RGlobal> RGlobalMarker for T { }

#[doc(hidden)]
#[rustc_unsafe_specialization_marker]
pub trait ErrorMarker: Error { }
impl<T: Error> ErrorMarker for T { }

#[doc(hidden)]
#[rustc_unsafe_specialization_marker]
pub trait HashEqMarker: Hash + Eq { }
impl<T: Hash + Eq> HashEqMarker for T { }

#[doc(hidden)]
#[rustc_unsafe_specialization_marker]
pub trait BuildHasherDefaultMarker: BuildHasher + Default { }
impl<T: BuildHasher + Default> BuildHasherDefaultMarker for T { }

#[doc(hidden)]
#[rustc_unsafe_specialization_marker]
pub trait OrdMarker: Ord { }
impl<T: Ord> OrdMarker for T { }


//-------------------------------------------------------------------------------------------------
// IntoVal and FromVal: definitions and blanket impls
//-------------------------------------------------------------------------------------------------

/**
A type which can be converted to a GameLisp value.

Many functions in the `glsp` crate receive a generic parameter `T: IntoVal`. This enables those 
functions to accept various different Rust types, silently converting those types into a 
[`Val`](enum.Val.html).
	
	glsp::set_global("numbers", (0, 1, 2, 3, 4))?;
	arr.push("an example string")?;

Invoking a type's [`into_val` method](#tymethod.into_val) is usually the most convenient way to 
produce a `Val`. `IntoVal` is part of the [prelude](prelude/index.html), so there's no need to 
import it into scope.
	
	let thousand = 10.0_f64.pow(3.0).into_val()?;

We provide `IntoVal` implementations for many common types, including all of Rust's primitive 
integer and floating-point types; primitive Rust types like `()` and `bool`; most standard 
collections, including arrays, slices and tuples; `Root` and `RRoot`; type-erased enums like 
`Deque` and `Callable`; shared and mutable references to all of the above; and shared references 
to primitive GameLisp types like `&Arr` and `&GFn`.

`Option` and `Result` have special handling, which can be useful for return values:

	- `Option` will produce a nil value if it's `None`, or otherwise call `into_val()`
	  for its `Some` value.

	- `Result` will trigger an error if it's `Err`, or otherwise call `into_val()`
	  for its `Ok` value. Non-GameLisp errors are fully supported.

There is a default `IntoVal` implementation for all `'static` types. This implementation moves
the Rust value onto the garbage-collected heap, wrapping it in an [`RData`](struct.RData.html).
The conversion returns a [`Val::RData`](enum.Val.html).

Because this default implementation applies to all `'static` types, including those defined in
external crates, it's possible to move most foreign types onto the GameLisp heap. For example, 
to construct a standard Rust `File` and transfer its ownership to GameLisp, you can simply call:
	
	File::open("my_file.png").into_val()?

If you'd like one of your own types to be represented in GameLisp by something other than an
`rdata` (for example, converting an enum to a GameLisp symbol, or a tuple struct to a GameLisp
array), you can implement `IntoVal` for your type. This will enable automatic conversions when 
your type is passed to a generic function like [`glsp::set_global`](fn.set_global.html). It will 
also automatically convert your type into GameLisp data when it's used as an `RFn` return value.

**Implementing `IntoVal` for your own types currently requires the `min_specialization` nightly
feature. Enable it by writing `#![feature(min_specialization)]` at the top of your crate's 
`main.rs` or `lib.rs` file.**
	
	struct Rgb(u8, u8, u8);

	impl IntoVal for Rgb {
		fn into_val(self) -> GResult<Val> {
			let Rgb(r, g, b) = self;
			arr![r, g, b].into_val()
		}
	}

	fn light_sea_green() -> Rgb {
		Rgb(32, 178, 170)
	}

	glsp::bind_rfn("light-sea-green", &light_sea_green)?;

	//calling (light-sea-green) from a GameLisp script will 
	//automatically call Rgb::into_val(), converting the function's
	//Rgb return value into an array of three integers, (32 178 178)

When implementing `IntoVal` for your own types, it's generally a good idea to also provide 
implementations for shared and mutable references to your type. It makes the borrow checker
easier to deal with, and it enables you to bind Rust functions which return references. It's 
usually straightforward:

	impl<'a> IntoVal for &'a MyType {
		fn into_val(self) -> GResult<Val> {
			//...the actual conversion...
		}
	}

	impl<'a> IntoVal for &'a mut MyType {
		fn into_val(self) -> GResult<Val> {
			(self as &MyType).into_val()
		}
	}

	impl IntoVal for MyType {
		fn into_val(self) -> GResult<Val> {
			(&self).into_val()
		}
	}
*/

#[rustc_specialization_trait]
pub trait IntoVal: Sized {
	fn into_val(self) -> GResult<Val>;

	#[doc(hidden)]
	fn into_slot(self) -> GResult<Slot> {
		self.into_val()?.into_slot()
	}
}

impl<T: StaticMarker> IntoVal for T {
	#[inline]
	default fn into_val(self) -> GResult<Val> {
		Ok(Val::RData(glsp::rdata(self)))
	}

	/*
	we need to be conservative here. if a more-specific impl only implements into_val(), its 
	into_slot() implementation will be this one, so we can't assume that we want the
	RData-like behaviour.
	*/

	#[doc(hidden)]
	#[inline]
	default fn into_slot(self) -> GResult<Slot> {
		self.into_val()?.into_slot()
	}
}

/**
A type which can be converted from a GameLisp value.

Many functions in the `glsp` crate have a generic return value `R: FromVal`. They can 
automatically convert their return value to many different Rust types.
	
	let numbers: Vec<u8> = glsp::global("numbers")?;
	let text: Root<Str> = arr.pop()?;

Writing `T::from_val(v)?` is usually the most convenient way to destructure a `Val`. `FromVal`
is part of the [prelude](prelude/index.html), so there's usually no need to import it into scope.
	
	let f = f64::from_val(val)?;

We provide `FromVal` implementations for many common types, including all of Rust's primitive 
integer and floating-point types; primitive Rust types like `bool`; most standard collections, 
including arrays, slices and tuples; `Root` and `RRoot`; type-erased enums like `Deque` and 
`Callable`; and owned string types, including `PathBuf`, `OsString` and `CString`.

You can also implement `FromVal` for your own types, which will enable them to take advantage of 
automatic conversions when they're [bound as an `RFn` parameter](fn.rfn.html).

**Implementing `FromVal` for your own types currently requires the `min_specialization` nightly
feature. Enable it by writing `#![feature(min_specialization)]` at the top of your crate's 
`main.rs` or `lib.rs` file.**
	
	struct Rgb(u8, u8, u8);

	impl FromVal for Rgb {
		fn from_val(val: &Val) -> GResult<Rgb> {
			let (r, g, b) = <(u8, u8, u8)>::from_val(val)?; 
			Ok(Rgb(r, g, b))
		}
	}

	fn describe_rgb(rgb: Rgb) {
		let Rgb(r, g, b) = rgb;
		println!("Red: {}\nGreen: {}\nBlue: {}", r, g, b);
	}

	glsp::bind_rfn("describe-rgb", &describe_rgb)?;

	//calling (describe-rgb '(32 178 178)) from a GameLisp script will
	//automatically invoke Rgb::from_val, converting the array of three
	//integers into an Rgb struct
*/

#[rustc_specialization_trait]
pub trait FromVal: Sized + StaticMarker {
	fn from_val(val: &Val) -> GResult<Self>;

	#[doc(hidden)]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		Self::from_val(&slot.root())
	}
}

//we won't be able to switch this on until associated type specialization is supported
//(todo? it might never be supported; it's not part of min_specialization). otherwise, we'd
//be forced to have a dynamic Temp type for FromArg, the way we do for FromArgRef.
/*
impl<T: StaticMarker> FromVal for T {
	#[inline]
	default fn from_val(val: &Val) -> GResult<Self> {
		todo!() //rdata.take()
	}

	//we need to be conservative here. see IntoVal::into_slot, above
	#[doc(hidden)]
	#[inline]
	default fn from_slot(slot: &Slot) -> GResult<Self> {
		Self::from_val(&slot.root())
	}
}
*/


//-------------------------------------------------------------------------------------------------
// IntoVal implementations
//-------------------------------------------------------------------------------------------------

impl IntoVal for Val {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(self)
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::from_val(&self))
	}
}

impl<'a> IntoVal for &'a Val {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok((*self).clone())
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::from_val(self))
	}
}

impl<'a> IntoVal for &'a mut Val {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok((*self).clone())
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::from_val(self))
	}
}

impl IntoVal for Slot {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(match self {
			Slot::Nil => Val::Nil,
			Slot::Int(i) => Val::Int(i),
			Slot::Char(c) => Val::Char(c),
			Slot::Flo(f) => Val::Flo(f),
			Slot::Bool(b) => Val::Bool(b),
			Slot::Sym(s) => Val::Sym(s),
			Slot::RFn(r) => Val::RFn(r.into_root()),
			Slot::Arr(a) => Val::Arr(a.into_root()),
			Slot::Str(s) => Val::Str(s.into_root()),
			Slot::Tab(t) => Val::Tab(t.into_root()),
			Slot::GIter(g) => Val::GIter(g.into_root()),
			Slot::Obj(o) => Val::Obj(o.into_root()),
			Slot::Class(c) => Val::Class(c.into_root()),
			Slot::GFn(c) => Val::GFn(c.into_root()),
			Slot::Coro(c) => Val::Coro(c.into_root()),
			Slot::RData(r) => Val::RData(r.into_root()),
		})
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(self)
	}
}

impl<'a> IntoVal for &'a Slot {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(*self).clone().into_val()
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok((*self).clone())
	}
}

impl<'a> IntoVal for &'a mut Slot {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(*self).clone().into_val()
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok((*self).clone())
	}
}

impl<T: IntoVal> IntoVal for Option<T> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			Some(src) => src.into_val(),
			None => Ok(Val::Nil)
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		match self {
			Some(src) => src.into_slot(),
			None => Ok(Slot::Nil)
		}
	}
}

impl<'a, T> IntoVal for &'a Option<T> where &'a T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		self.as_ref().into_val()
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		self.as_ref().into_slot()
	}
}

impl<'a, T> IntoVal for &'a mut Option<T> where &'a mut T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		self.as_mut().into_val()
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		self.as_mut().into_slot()
	}
}

impl<T: IntoVal, E: ErrorMarker + StaticMarker> IntoVal for Result<T, E> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			Ok(src) => src.into_val(),
			Err(err) => {
				/*
				we're forced to dynamically "specialize" for GResult here, so that 
				GError::MacroNoOp will propagate properly rather than being promoted
				to a true error. we could use actual specialization instead (which
				would eliminate the allocation here), but i prefer to avoid it
				*/

				let dyn_err: &(dyn Error + 'static) = &err;
				if dyn_err.is::<GError>() {
					let dyn_err_boxed: Box<dyn Error + 'static> = Box::new(err);
					let g_err: GError = *dyn_err_boxed.downcast::<GError>().unwrap();
					Err(g_err)
				} else {
					Err(error!("IntoVal encountered {}", type_name::<E>()).with_source(err))
				}
			}
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		self.into_val()?.into_slot()
	}
}

impl IntoVal for () {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Nil)
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::Nil)
	}
}

impl<'a> IntoVal for &'a () {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Nil)
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::Nil)
	}
}

impl<'a> IntoVal for &'a mut () {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Nil)
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::Nil)
	}
}

macro_rules! impl_into_val_infallible {
	($self_type:ty, $variant:ident) => (
		impl IntoVal for $self_type {
			#[inline]
			fn into_val(self) -> GResult<Val> {
				Ok(Val::$variant(self.into()))
			}

			#[doc(hidden)]
			#[inline]
			fn into_slot(self) -> GResult<Slot> {
				Ok(Slot::$variant(self.into()))
			}
		}

		impl<'a> IntoVal for &'a $self_type {
			#[inline]
			fn into_val(self) -> GResult<Val> {
				Ok(Val::$variant((*self).into()))
			}

			#[doc(hidden)]
			#[inline]
			fn into_slot(self) -> GResult<Slot> {
				Ok(Slot::$variant((*self).into()))
			}
		}

		impl<'a> IntoVal for &'a mut $self_type {
			#[inline]
			fn into_val(self) -> GResult<Val> {
				Ok(Val::$variant((*self).into()))
			}

			#[doc(hidden)]
			#[inline]
			fn into_slot(self) -> GResult<Slot> {
				Ok(Slot::$variant((*self).into()))
			}
		}
	);
}

impl_into_val_infallible!(i8, Int);
impl_into_val_infallible!(i16, Int);
impl_into_val_infallible!(i32, Int);
impl_into_val_infallible!(u8, Int);
impl_into_val_infallible!(u16, Int);
impl_into_val_infallible!(f32, Flo);
impl_into_val_infallible!(char, Char);
impl_into_val_infallible!(bool, Bool);
impl_into_val_infallible!(Sym, Sym);

macro_rules! impl_into_val_root {
	($t:ident) => (
		impl IntoVal for Root<$t> {
			#[inline]
			fn into_val(self) -> GResult<Val> {
				Ok(Val::$t(self))
			}

			#[doc(hidden)]
			#[inline]
			fn into_slot(self) -> GResult<Slot> {
				Ok(Slot::$t(self.into_raw()))
			}
		}
		
		impl<'a> IntoVal for &'a Root<$t> {
			#[inline]
			fn into_val(self) -> GResult<Val> {
				Ok(Val::$t((*self).clone()))
			}

			#[doc(hidden)]
			#[inline]
			fn into_slot(self) -> GResult<Slot> {
				Ok(Slot::$t((*self).to_raw()))
			}
		}
		
		impl<'a> IntoVal for &'a mut Root<$t> {
			#[inline]
			fn into_val(self) -> GResult<Val> {
				Ok(Val::$t((*self).clone()))
			}

			#[doc(hidden)]
			#[inline]
			fn into_slot(self) -> GResult<Slot> {
				Ok(Slot::$t((*self).to_raw()))
			}
		}

		impl IntoVal for Raw<$t> {
			#[inline]
			fn into_val(self) -> GResult<Val> {
				Ok(Val::$t(self.into_root()))
			}

			#[doc(hidden)]
			#[inline]
			fn into_slot(self) -> GResult<Slot> {
				Ok(Slot::$t(self))
			}
		}
	);
}

impl_into_val_root!(Arr);
impl_into_val_root!(Str);
impl_into_val_root!(Tab);
impl_into_val_root!(GIter);
impl_into_val_root!(Obj);
impl_into_val_root!(Class);
impl_into_val_root!(GFn);
impl_into_val_root!(Coro);
impl_into_val_root!(RData);
impl_into_val_root!(RFn);

impl<T> IntoVal for RRoot<T> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::RData(self.into_root()))
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::RData(self.into_raw()))
	}
}

impl<'a, T> IntoVal for &'a RRoot<T> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::RData((*self).to_root()))
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::RData((*self).to_raw()))
	}
}

impl<'a, T> IntoVal for &'a mut RRoot<T> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::RData((*self).to_root()))
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::RData((*self).to_raw()))
	}
}

impl IntoVal for Deque {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			Deque::Arr(root) => Ok(Val::Arr(root)),
			Deque::Str(root) => Ok(Val::Str(root))
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		match self {
			Deque::Arr(root) => Ok(Slot::Arr(root.into_raw())),
			Deque::Str(root) => Ok(Slot::Str(root.into_raw()))
		}
	}
}

impl IntoVal for Callable {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			Callable::GFn(root) => Ok(Val::GFn(root)),
			Callable::RFn(root) => Ok(Val::RFn(root)),
			Callable::Class(root) => Ok(Val::Class(root))
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		match self {
			Callable::GFn(root) => Ok(Slot::GFn(root.into_raw())),
			Callable::RFn(root) => Ok(Slot::RFn(root.into_raw())),
			Callable::Class(root) => Ok(Slot::Class(root.into_raw()))
		}
	}
}

impl IntoVal for Expander {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			Expander::GFn(root) => Ok(Val::GFn(root)),
			Expander::RFn(root) => Ok(Val::RFn(root))
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		match self {
			Expander::GFn(root) => Ok(Slot::GFn(root.into_raw())),
			Expander::RFn(root) => Ok(Slot::RFn(root.into_raw()))
		}
	}
}

impl IntoVal for Iterable {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			Iterable::Arr(root) => Ok(Val::Arr(root)),
			Iterable::Str(root) => Ok(Val::Str(root)),
			Iterable::Tab(root) => Ok(Val::Tab(root)),
			Iterable::GIter(root) => Ok(Val::GIter(root)),
			Iterable::Coro(root) => Ok(Val::Coro(root))
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		match self {
			Iterable::Arr(root) => Ok(Slot::Arr(root.into_raw())),
			Iterable::Str(root) => Ok(Slot::Str(root.into_raw())),
			Iterable::Tab(root) => Ok(Slot::Tab(root.into_raw())),
			Iterable::GIter(root) => Ok(Slot::GIter(root.into_raw())),
			Iterable::Coro(root) => Ok(Slot::Coro(root.into_raw()))
		}
	}
}

impl IntoVal for GIterLen {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			GIterLen::Exact(len) => Ok(Val::Int(len as i32)),
			GIterLen::Infinite => Ok(Val::Sym(INFINITE_SYM)),
			GIterLen::Unknown => Ok(Val::Sym(UNKNOWN_SYM))
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		match self {
			GIterLen::Exact(len) => Ok(Slot::Int(len as i32)),
			GIterLen::Infinite => Ok(Slot::Sym(INFINITE_SYM)),
			GIterLen::Unknown => Ok(Slot::Sym(UNKNOWN_SYM))
		}
	}
}

impl IntoVal for Ordering {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			Ordering::Less => Ok(Val::Sym(LT_SYM)),
			Ordering::Equal => Ok(Val::Sym(NUM_EQ_SYM)),
			Ordering::Greater => Ok(Val::Sym(GT_SYM))
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		match self {
			Ordering::Less => Ok(Slot::Sym(LT_SYM)),
			Ordering::Equal => Ok(Slot::Sym(NUM_EQ_SYM)),
			Ordering::Greater => Ok(Slot::Sym(GT_SYM))
		}
	}
}

macro_rules! impl_refs_to_clone_types {
	($($t:ty),+) => (
		$(
			impl<'a> IntoVal for &'a $t {
				#[inline]
				fn into_val(self) -> GResult<Val> {
					(*self).clone().into_val()
				}

				#[doc(hidden)]
				#[inline]
				fn into_slot(self) -> GResult<Slot> {
					(*self).clone().into_slot()
				}
			}

			impl<'a> IntoVal for &'a mut $t {
				#[inline]
				fn into_val(self) -> GResult<Val> {
					(*self).clone().into_val()
				}

				#[doc(hidden)]
				#[inline]
				fn into_slot(self) -> GResult<Slot> {
					(*self).clone().into_slot()
				}
			}
		)+
	);
}

impl_refs_to_clone_types!(Deque, Callable, Expander, Iterable, GIterLen, Ordering);

macro_rules! impl_into_val_bounded_int {
	($self_type:ty) => (
		impl IntoVal for $self_type {
			#[inline]
			fn into_val(self) -> GResult<Val> {
				if let Ok(converted) = self.try_into() {
					Ok(Val::Int(converted))
				} else {
					bail!("the result was {}, which is outside the range of an i32", self)
				}
			}

			#[doc(hidden)]
			#[inline]
			fn into_slot(self) -> GResult<Slot> {
				if let Ok(converted) = self.try_into() {
					Ok(Slot::Int(converted))
				} else {
					bail!("the result was {}, which is outside the range of an i32", self)
				}
			}
		}
	);
}

impl_into_val_bounded_int!(i64);
impl_into_val_bounded_int!(i128);
impl_into_val_bounded_int!(isize);
impl_into_val_bounded_int!(u32);
impl_into_val_bounded_int!(u64);
impl_into_val_bounded_int!(u128);
impl_into_val_bounded_int!(usize);

impl IntoVal for f64 {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Flo(self as f32))
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		Ok(Slot::Flo(self as f32))
	}
}

impl IntoVal for Num {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self {
			Num::Int(i) => Ok(Val::Int(i)),
			Num::Flo(f) => Ok(Val::Flo(f))
		}
	}

	#[doc(hidden)]
	#[inline]
	fn into_slot(self) -> GResult<Slot> {
		match self {
			Num::Int(i) => Ok(Slot::Int(i)),
			Num::Flo(f) => Ok(Slot::Flo(f))
		}
	}
}

impl<T: IntoVal> IntoVal for Vec<T> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<'a, T> IntoVal for &'a Vec<T> where &'a T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<'a, T> IntoVal for &'a mut Vec<T> where &'a mut T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<T: IntoVal> IntoVal for VecDeque<T> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<'a, T> IntoVal for &'a VecDeque<T> where &'a T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<'a, T> IntoVal for &'a mut VecDeque<T> where &'a mut T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<A: smallvec::Array> IntoVal for SmallVec<A> where A::Item: IntoVal {
	#[inline]
	fn into_val(mut self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self.drain(..))?))
	}
}

impl<'a, A: smallvec::Array> IntoVal for &'a SmallVec<A> where &'a A::Item: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<'a, A: smallvec::Array> IntoVal for &'a mut SmallVec<A> where &'a mut A::Item: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<'a, T> IntoVal for &'a [T] where &'a T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<'a, T> IntoVal for &'a mut [T] where &'a mut T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(self)?))
	}
}

impl<T, const N: usize> IntoVal for [T; N] where for<'a> &'a T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(&self[..])?))
	}
}

impl<'a, T, const N: usize> IntoVal for &'a [T; N] where &'a T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(&self[..])?))
	}
}

impl<'a, T, const N: usize> IntoVal for &'a mut [T; N] where &'a mut T: IntoVal {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Arr(glsp::arr_from_iter(&mut self[..])?))
	}
}

macro_rules! impl_into_val_tuple {
	($len:literal: $($t:ident $i:tt),+) => (
		impl<$($t),+> IntoVal for ($($t,)+) 
		where 
			$( $t: IntoVal ),+ 
		{
			#[inline]
			fn into_val(self) -> GResult<Val> {
				let arr = glsp::arr_with_capacity($len);

				$(
					arr.push(self.$i)?;
				)+

				Ok(Val::Arr(arr))
			}
		}

		impl<'a, $($t),+> IntoVal for &'a ($($t,)+) 
		where 
			$( &'a $t: IntoVal ),+ 
		{
			#[inline]
			fn into_val(self) -> GResult<Val> {
				let arr = glsp::arr_with_capacity($len);

				$(
					arr.push(&self.$i)?;
				)+

				Ok(Val::Arr(arr))
			}
		}

		impl<'a, $($t),+> IntoVal for &'a mut ($($t,)+) 
		where 
			$( &'a mut $t: IntoVal ),+ 
		{
			#[inline]
			fn into_val(self) -> GResult<Val> {
				let arr = glsp::arr_with_capacity($len);

				$(
					arr.push(&mut self.$i)?;
				)+

				Ok(Val::Arr(arr))
			}
		}
	);
}

impl_into_val_tuple!( 1: A 0);
impl_into_val_tuple!( 2: A 0, B 1);
impl_into_val_tuple!( 3: A 0, B 1, C 2);
impl_into_val_tuple!( 4: A 0, B 1, C 2, D 3);
impl_into_val_tuple!( 5: A 0, B 1, C 2, D 3, E 4);
impl_into_val_tuple!( 6: A 0, B 1, C 2, D 3, E 4, F 5);
impl_into_val_tuple!( 7: A 0, B 1, C 2, D 3, E 4, F 5, G 6);
impl_into_val_tuple!( 8: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);
impl_into_val_tuple!( 9: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8);
impl_into_val_tuple!(10: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9);
impl_into_val_tuple!(11: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10);
impl_into_val_tuple!(12: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11);

impl IntoVal for String {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Str(glsp::str_from_rust_str(&self)))
	}
}

impl<'a> IntoVal for &'a String {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Str(glsp::str_from_rust_str(self)))
	}
}

impl<'a> IntoVal for &'a mut String {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Str(glsp::str_from_rust_str(self)))
	}
}

impl<'a> IntoVal for &'a str {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Str(glsp::str_from_rust_str(self)))
	}
}

impl<'a> IntoVal for &'a mut str {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Str(glsp::str_from_rust_str(self)))
	}
}

impl IntoVal for CString {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(&self as &CStr).into_val()
	}
}

impl<'a> IntoVal for &'a CString {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(self as &CStr).into_val()
	}
}

impl<'a> IntoVal for &'a mut CString {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(self as &CStr).into_val()
	}
}

impl<'a> IntoVal for &'a CStr {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self.to_str() {
			Ok(str_ref) => str_ref.into_val(),
			Err(_) => bail!("CStr contained non-UTF-8 data")
		}
	}
}

impl<'a> IntoVal for &'a mut CStr {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(self as &CStr).into_val()
	}
}

impl IntoVal for OsString {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(&self as &OsStr).into_val()
	}
}

impl<'a> IntoVal for &'a OsString {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(self as &OsStr).into_val()
	}
}

impl<'a> IntoVal for &'a mut OsString {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(self as &OsStr).into_val()
	}
}

impl<'a> IntoVal for &'a OsStr {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		match self.to_str() {
			Some(str_ref) => str_ref.into_val(),
			None => bail!("OsStr contained non-UTF-8 data")
		}
	}
}

impl<'a> IntoVal for &'a mut OsStr {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(self as &OsStr).into_val()
	}
}

impl IntoVal for PathBuf {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(&self as &Path).into_val()
	}
}

impl<'a> IntoVal for &'a PathBuf {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(&self as &Path).into_val()
	}
}

impl<'a> IntoVal for &'a mut PathBuf {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(&self as &Path).into_val()
	}
}

impl<'a> IntoVal for &'a Path {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		self.as_os_str().into_val()
	}
}

impl<'a> IntoVal for &'a mut Path {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		(self as &Path).into_val()
	}
}

impl<K: IntoVal, V: IntoVal, S> IntoVal for HashMap<K, V, S> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Tab(glsp::tab_from_iter(self)?))
	}
}

impl<'a, K, V, S> IntoVal for &'a HashMap<K, V, S>
where
	&'a K: IntoVal,
	&'a V: IntoVal
{
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Tab(glsp::tab_from_iter(self)?))
	}
}

impl<'a, K, V, S> IntoVal for &'a mut HashMap<K, V, S>
where
	&'a K: IntoVal,
	&'a mut V: IntoVal
{
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Tab(glsp::tab_from_iter(self)?))
	}
}

impl<K: IntoVal, V: IntoVal> IntoVal for BTreeMap<K, V> {
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Tab(glsp::tab_from_iter(self)?))
	}
}

impl<'a, K, V> IntoVal for &'a BTreeMap<K, V> 
where
	&'a K: IntoVal,
	&'a V: IntoVal
{
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Tab(glsp::tab_from_iter(self)?))
	}
}

impl<'a, K, V> IntoVal for &'a mut BTreeMap<K, V> 
where
	&'a K: IntoVal,
	&'a mut V: IntoVal
{
	#[inline]
	fn into_val(self) -> GResult<Val> {
		Ok(Val::Tab(glsp::tab_from_iter(self)?))
	}
}


//-------------------------------------------------------------------------------------------------
// FromVal implementations
//-------------------------------------------------------------------------------------------------

impl FromVal for Val {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		Ok(val.clone())
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		Ok(slot.root())
	}
}

impl FromVal for Slot {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		Ok(Slot::from_val(val))
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		Ok(slot.clone())
	}
}

macro_rules! impl_from_val_infallible(
	($(($t:ty, $variant:ident)),+) => (
		$(
			impl FromVal for $t {
				#[inline]
				fn from_val(val: &Val) -> GResult<Self> {
					match *val {
						Val::$variant(interior) => Ok(interior as $t),
						ref val => bail!("expected {}, received {}", 
						                 stringify!($t), val.a_type_name())
					}
				}

				#[doc(hidden)]
				#[inline]
				fn from_slot(slot: &Slot) -> GResult<Self> {
					match *slot {
						Slot::$variant(interior) => Ok(interior as $t),
						ref slot => bail!("expected {}, received {}", 
						                  stringify!($t), slot.a_type_name())
					}
				}
			}
		)+
	);
);

impl_from_val_infallible!(
	(i32, Int),
	(i64, Int),
	(i128, Int),
	(isize, Int),
	(char, Char),
	(bool, Bool),
	(Sym, Sym)
);

macro_rules! impl_from_val_root(
	($(($t:ty, $variant:ident)),+) => (
		$(
			impl FromVal for Root<$t> {
				#[inline]
				fn from_val(val: &Val) -> GResult<Self> {
					match *val {
						Val::$variant(ref root) => Ok(root.clone()),
						ref val => bail!("expected {}, received {}", 
						                 stringify!(Root<$t>), val.a_type_name())
					}
				}

				#[doc(hidden)]
				#[inline]
				fn from_slot(slot: &Slot) -> GResult<Self> {
					match *slot {
						Slot::$variant(ref raw) => Ok(raw.root()),
						ref slot => bail!("expected {}, received {}", 
						                  stringify!(Root<$t>), slot.a_type_name())
					}
				}
			}

			impl FromVal for Raw<$t> {
				#[inline]
				fn from_val(val: &Val) -> GResult<Self> {
					match *val {
						Val::$variant(ref root) => Ok(root.as_raw().clone()),
						ref val => bail!("expected {}, received {}", 
						                 stringify!(Raw<$t>), val.a_type_name())
					}
				}

				#[doc(hidden)]
				#[inline]
				fn from_slot(slot: &Slot) -> GResult<Self> {
					match *slot {
						Slot::$variant(ref raw) => Ok(raw.clone()),
						ref slot => bail!("expected {}, received {}", 
						                  stringify!(Raw<$t>), slot.a_type_name())
					}
				}
			}
		)+
	);
);

impl_from_val_root!(
	(Arr, Arr),
	(Str, Str),
	(Tab, Tab),
	(GIter, GIter),
	(Obj, Obj),
	(GFn, GFn),
	(Class, Class),
	(Coro, Coro),
	(RData, RData),
	(RFn, RFn)
);

impl<T: StaticMarker> FromVal for RRoot<T> {
	#[inline]
	fn from_val(val: &Val) -> GResult<RRoot<T>> {
		match val {
			Val::RData(root) => Ok(RRoot::new(root.clone())),
			val => bail!("expected RRoot<{}>, received {}", type_name::<T>(), val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<RRoot<T>> {
		match slot {
			Slot::RData(raw) => Ok(RRoot::new(raw.root())),
			val => bail!("expected RRoot<{}>, received {}", type_name::<T>(), val.a_type_name())
		}
	}
}

macro_rules! impl_from_val_int_fallible_small(
	($($t:ident),+) => (
		$(
			impl FromVal for $t {
				#[inline]
				fn from_val(val: &Val) -> GResult<Self> {
					match *val {
						Val::Int(i) if i >= $t::MIN as i32 && i <= $t::MAX as i32 => {
							Ok(i as $t)
						}
						Val::Int(i) => {
							bail!("expected {}, received an int with value {}",
							      stringify!($t), i)
						}
						ref val => bail!("expected {}, received {}", 
						                 stringify!($t), val.a_type_name())
					}
				}

				#[doc(hidden)]
				#[inline]
				fn from_slot(slot: &Slot) -> GResult<Self> {
					match *slot {
						Slot::Int(i) if i >= $t::MIN as i32 && i <= $t::MAX as i32 => {
							Ok(i as $t)
						}
						Slot::Int(i) => {
							bail!("expected {}, received an int with value {}",
							      stringify!($t), i)
						}
						ref slot => bail!("expected {}, received {}", 
						                  stringify!($t), slot.a_type_name())
					}
				}
			}
		)+
	);
);

impl_from_val_int_fallible_small!(i8, i16, u8, u16);

macro_rules! impl_from_val_int_fallible_large(
	($($t:ty),+) => (
		$(
			impl FromVal for $t {
				#[inline]
				fn from_val(val: &Val) -> GResult<Self> {
					match *val {
						Val::Int(i) if i >= 0 => {
							Ok(i as $t)
						}
						Val::Int(i) => {
							bail!("expected {}, received an int with value {}",
							      stringify!($t), i)
						}
						ref val => bail!("expected {}, received {}", 
						                 stringify!($t), val.a_type_name())
					}
				}

				#[doc(hidden)]
				#[inline]
				fn from_slot(slot: &Slot) -> GResult<Self> {
					match *slot {
						Slot::Int(i) if i >= 0 => {
							Ok(i as $t)
						}
						Slot::Int(i) => {
							bail!("expected {}, received an int with value {}",
							      stringify!($t), i)
						}
						ref slot => bail!("expected {}, received {}", 
						                  stringify!($t), slot.a_type_name())
					}
				}
			}
		)+
	);
);

impl_from_val_int_fallible_large!(u32, u64, u128, usize);

impl FromVal for f32 {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Flo(f) => Ok(f),
			ref val => bail!("expected f32, received {}", val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		match *slot {
			Slot::Flo(f) => Ok(f),
			ref slot => bail!("expected f32, received {}", slot.a_type_name())
		}
	}
}

impl FromVal for f64 {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Flo(f) => Ok(f as f64),
			ref val => bail!("expected f64, received {}", val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		match *slot {
			Slot::Flo(f) => Ok(f as f64),
			ref slot => bail!("expected f64, received {}", slot.a_type_name())
		}
	}
}

impl FromVal for Num {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Int(i) => Ok(Num::Int(i)),
			Val::Flo(f) => Ok(Num::Flo(f)),
			ref val => bail!("expected Num, received {}", val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		match *slot {
			Slot::Int(i) => Ok(Num::Int(i)),
			Slot::Flo(f) => Ok(Num::Flo(f)),
			ref slot => bail!("expected Num, received {}", slot.a_type_name())
		}
	}
}

impl FromVal for Deque {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Arr(ref root) => Ok(Deque::Arr(root.clone())),
			Val::Str(ref root) => Ok(Deque::Str(root.clone())),
			ref val => bail!("expected Deque, received {}", val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		match *slot {
			Slot::Arr(ref raw) => Ok(Deque::Arr(raw.root())),
			Slot::Str(ref raw) => Ok(Deque::Str(raw.root())),
			ref slot => bail!("expected Deque, received {}", slot.a_type_name())
		}
	}
}

impl FromVal for Callable {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::GFn(ref root) => Ok(Callable::GFn(root.clone())),
			Val::RFn(ref root) => Ok(Callable::RFn(root.clone())),
			Val::Class(ref root) => Ok(Callable::Class(root.clone())),
			ref val => bail!("expected Callable, received {}", val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		match *slot {
			Slot::GFn(ref raw) => Ok(Callable::GFn(raw.root())),
			Slot::RFn(ref raw) => Ok(Callable::RFn(raw.root())),
			Slot::Class(ref raw) => Ok(Callable::Class(raw.root())),
			ref slot => bail!("expected Callable, received {}", slot.a_type_name())
		}
	}
}

impl FromVal for Expander {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::GFn(ref root) => Ok(Expander::GFn(root.clone())),
			Val::RFn(ref root) => Ok(Expander::RFn(root.clone())),
			ref val => bail!("expected Expander, received {}", val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		match *slot {
			Slot::GFn(ref raw) => Ok(Expander::GFn(raw.root())),
			Slot::RFn(ref raw) => Ok(Expander::RFn(raw.root())),
			ref slot => bail!("expected Expander, received {}", slot.a_type_name())
		}
	}
}

impl FromVal for Iterable {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match val {
			Val::Arr(root) => Ok(Iterable::Arr(root.clone())),
			Val::Str(root) => Ok(Iterable::Str(root.clone())),
			Val::Tab(root) => Ok(Iterable::Tab(root.clone())),
			Val::GIter(root) => Ok(Iterable::GIter(root.clone())),
			Val::Coro(root) => Ok(Iterable::Coro(root.clone())),
			val => bail!("expected Iterable, received {}", val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		match slot {
			Slot::Arr(raw) => Ok(Iterable::Arr(raw.root())),
			Slot::Str(raw) => Ok(Iterable::Str(raw.root())),
			Slot::Tab(raw) => Ok(Iterable::Tab(raw.root())),
			Slot::GIter(raw) => Ok(Iterable::GIter(raw.root())),
			Slot::Coro(raw) => Ok(Iterable::Coro(raw.root())),
			slot => bail!("expected Iterable, received {}", slot.a_type_name())
		}
	}
}

impl FromVal for EnvMode {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Sym(sym) => {
				match sym {
					FRESH_SYM => Ok(EnvMode::Fresh),
					COPIED_SYM => Ok(EnvMode::Copied),
					_ => bail!("expected an EnvMode, received the symbol {}", sym)
				}
			}
			ref val => bail!("expected an EnvMode, received {}", val.a_type_name())
		}
	}
}

impl FromVal for Ordering {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Sym(LT_SYM) => Ok(Ordering::Less),
			Val::Sym(NUM_EQ_SYM) => Ok(Ordering::Equal),
			Val::Sym(GT_SYM) => Ok(Ordering::Greater),
			ref val => bail!("expected Ordering, received {}", val.a_type_name())
		}
	}

	#[doc(hidden)]
	#[inline]
	fn from_slot(slot: &Slot) -> GResult<Self> {
		match *slot {
			Slot::Sym(LT_SYM) => Ok(Ordering::Less),
			Slot::Sym(NUM_EQ_SYM) => Ok(Ordering::Equal),
			Slot::Sym(GT_SYM) => Ok(Ordering::Greater),
			ref slot => bail!("expected Ordering, received {}", slot.a_type_name())
		}
	}
}

impl<T: FromVal> FromVal for Vec<T> {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Arr(ref arr) => {
				let mut vec = Vec::<T>::with_capacity(arr.len());

				let arr_borrow = arr.borrow();
				for slot in arr_borrow.iter() {
					vec.push(T::from_slot(slot)?);
				}

				Ok(vec)
			}
			ref val => bail!("expected a Vec, received {}", val.a_type_name())
		}
	}
}

impl<T: FromVal> FromVal for VecDeque<T> {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Arr(ref arr) => {
				let mut vec = VecDeque::<T>::with_capacity(arr.len());

				let arr_borrow = arr.borrow();
				for slot in arr_borrow.iter() {
					vec.push_back(T::from_slot(slot)?);
				}

				Ok(vec)
			}
			ref val => bail!("expected a VecDeque, received {}", val.a_type_name())
		}
	}
}

impl<A> FromVal for SmallVec<A>
where
	A: smallvec::Array + StaticMarker,
	A::Item: FromVal
{
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Arr(ref arr) => {
				let mut small_vec = SmallVec::<A>::with_capacity(arr.len());

				let arr_borrow = arr.borrow();
				for slot in arr_borrow.iter() {
					small_vec.push(A::Item::from_slot(slot)?);
				}

				Ok(small_vec)
			}
			ref val => bail!("expected a SmallVec, received {}", val.a_type_name())
		}
	}
}

impl<T: FromVal, const N: usize> FromVal for [T; N] {
	#[inline]
	fn from_val(val: &Val) -> GResult<[T; N]> {
		match *val {
			Val::Arr(ref arr) => {
				ensure!(arr.len() == N, 
				        "expected a [T; {}], received an array of length {}",
				        N, arr.len());

				//todo: this is wildly inefficient; improve it once better ways to construct
				//non-Copy const generic arrays are available. maybe SmallVec?
				let mut vals = Vec::<T>::with_capacity(N);
				for i in 0 .. N {
					vals.push(arr.get::<T>(i)?);
				}

				Ok(TryFrom::try_from(vals).ok().unwrap())
			}
			ref val => {
				bail!("expected a [T; {}], received {}", N, val.a_type_name())
			}
		}
	}
}

macro_rules! impl_from_val_tuple {
	($len:literal: $($t:ident $i:tt),+) => (
		impl<$($t),+> FromVal for ($($t,)+)
		where
			$($t: FromVal),+
		{
			#[inline]
			fn from_val(val: &Val) -> GResult<($($t,)+)> {
				match *val {
					Val::Arr(ref arr) => {
						ensure!(arr.len() == $len, 
						        "expected a {}-element tuple, received an arr of length {}", 
						        $len, arr.len());

						Ok(($(
							arr.get::<$t>($i)?,
						)*))
					}
					ref val => bail!("expected a tuple, received {}", val.a_type_name())
				}
			}
		}
	);
}

impl_from_val_tuple!( 1: A 0);
impl_from_val_tuple!( 2: A 0, B 1);
impl_from_val_tuple!( 3: A 0, B 1, C 2);
impl_from_val_tuple!( 4: A 0, B 1, C 2, D 3);
impl_from_val_tuple!( 5: A 0, B 1, C 2, D 3, E 4);
impl_from_val_tuple!( 6: A 0, B 1, C 2, D 3, E 4, F 5);
impl_from_val_tuple!( 7: A 0, B 1, C 2, D 3, E 4, F 5, G 6);
impl_from_val_tuple!( 8: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);
impl_from_val_tuple!( 9: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8);
impl_from_val_tuple!(10: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9);
impl_from_val_tuple!(11: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10);
impl_from_val_tuple!(12: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11);

impl FromVal for String {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Str(ref st) => Ok(st.to_string()),
			ref val => bail!("expected a str, received {}", val.a_type_name())
		}
	}
}

impl FromVal for CString {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Str(ref st) => {
				match CString::new(st.to_string()) {
					Ok(cstring) => Ok(cstring),
					Err(_) => {
						bail!("expected a C string, received a str with an inner nul")
					}
				}
			}
			ref val => bail!("expected a C string, received {}", val.a_type_name())
		}
	}
}

impl FromVal for PathBuf {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Str(ref st) => Ok(PathBuf::from(st.to_string())),
			ref val => bail!("expected a path, received {}", val.a_type_name())
		}
	}
}

impl FromVal for OsString {
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Str(ref st) => Ok(OsString::from(st.to_string())),
			ref val => bail!("expected an OS string, received {}", val.a_type_name())
		}
	}
}

impl<K, V, S> FromVal for HashMap<K, V, S>
where
	K: HashEqMarker + FromVal + StaticMarker,
	V: FromVal + StaticMarker,
	S: BuildHasherDefaultMarker + StaticMarker
{
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Tab(ref tab) => {
				let s = S::default();
				let mut hash_map = HashMap::<K, V, S>::with_capacity_and_hasher(tab.len(), s);

				let tab_borrow = tab.borrow();
				for (internal_key, internal_value) in tab_borrow.iter() {
					let key = K::from_slot(internal_key)?;
					let value = V::from_slot(internal_value)?;
					
					if hash_map.insert(key, value).is_some() {
						bail!("duplicate key in HashMap argument");
					}
				}

				Ok(hash_map)
			}
			ref val => bail!("expected a HashMap, received {}", val.a_type_name())
		}
	}
}

// BTreeMap<K, V>
//-----------------------------------------------------------------------------

impl<K, V> FromVal for BTreeMap<K, V> 
where
	K: OrdMarker + FromVal + StaticMarker,
	V: FromVal + StaticMarker
{
	#[inline]
	fn from_val(val: &Val) -> GResult<Self> {
		match *val {
			Val::Tab(ref tab) => {
				let mut btree_map = BTreeMap::<K, V>::new();

				let tab_borrow = tab.borrow();
				for (internal_key, internal_value) in tab_borrow.iter() {
					let key = K::from_slot(internal_key)?;
					let value = V::from_slot(internal_value)?;
					
					if btree_map.insert(key, value).is_some() {
						bail!("duplicate key in BTreeMap argument");
					}
				}

				Ok(btree_map)
			}
			ref val => bail!("expected a BTreeMap, received {}", val.a_type_name())
		}
	}
}


//-------------------------------------------------------------------------------------------------
// FromArg, FromArgRef
//-------------------------------------------------------------------------------------------------

#[doc(hidden)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ArgType {
	RGlobal,
	Normal,
	Option,
	Rest
}

#[doc(hidden)]
#[rustc_specialization_trait]
pub trait FromArg: Sized {
	type Temp: 'static;
	type OutputCtor: ?Sized + for<'a> Ctor<'a>;

	fn arg_type() -> ArgType;
	fn make_temp(args: &[Slot], i: usize) -> GResult<Self::Temp>;
    fn from_arg<'a>(temp: &'a mut Self::Temp) -> GResult<<Self::OutputCtor as Ctor<'a>>::Ty>;
}

#[doc(hidden)]
#[rustc_specialization_trait]
pub trait FromArgRef: Sized {
	type Temp: 'static;
	type OutputCtor: for<'a> Ctor<'a>;

	fn arg_type() -> ArgType;
	fn make_temp(args: &[Slot], i: usize) -> GResult<Self::Temp>;
    fn from_arg<'a>(temp: &'a mut Self::Temp) -> GResult<<Self::OutputCtor as Ctor<'a>>::Ty>;
}

/*
The Ctor trait is a workaround for rust's current lack of GAT support. it should
eventually be replaced with a generic lifetime on FromArgRef::OutputCtor (todo)
*/

#[doc(hidden)]
pub trait Ctor<'a> {
    type Ty;
}

#[doc(hidden)]
pub struct ValCtor<T>(PhantomData<T>);

impl<'a, T> Ctor<'a> for ValCtor<T> {
    type Ty = T;
}

#[doc(hidden)]
pub struct OptionCtor<T: ?Sized>(PhantomData<T>);

impl<'a, T: Ctor<'a> + ?Sized> Ctor<'a> for OptionCtor<T> {
    type Ty = Option<T::Ty>;
}

#[doc(hidden)]
pub struct RestCtor<T>(PhantomData<T>);

impl<'a, T: 'a> Ctor<'a> for RestCtor<T> {
    type Ty = Rest<'a, T>;
}

#[doc(hidden)]
pub struct RefCtor<T: ?Sized>(PhantomData<T>);

impl<'a, T: ?Sized + 'a> Ctor<'a> for RefCtor<T> {
    type Ty = &'a T;
}

#[doc(hidden)]
pub struct RefMutCtor<T: ?Sized>(PhantomData<T>);

impl<'a, T: ?Sized + 'a> Ctor<'a> for RefMutCtor<T> {
    type Ty = &'a mut T;
}

impl<T: FromVal> FromArg for T {
	type Temp = Slot;
	type OutputCtor = ValCtor<T>;

	#[inline]
	fn arg_type() -> ArgType {
		ArgType::Normal
	}

	#[inline]
	fn make_temp(args: &[Slot], i: usize) -> GResult<Slot> {
		Ok(args[i].clone())
	}

	#[inline]
	fn from_arg<'a>(temp: &'a mut Slot) -> GResult<T> {
		T::from_slot(temp)
	}
}

impl<T: FromArg> FromArg for Option<T> {
	type Temp = Option<T::Temp>;
	type OutputCtor = OptionCtor<<T as FromArg>::OutputCtor>;

	#[inline]
	fn arg_type() -> ArgType {
		ArgType::Option
	}

	#[inline]
	fn make_temp(args: &[Slot], i: usize) -> GResult<Option<T::Temp>> {
		if i >= args.len() || matches!(args[i], Slot::Nil) {
			Ok(None)
		} else {
			Ok(Some(T::make_temp(args, i)?))
		}
	}

	#[inline]
	fn from_arg<'a>(
		temp: &'a mut Option<T::Temp>
	) -> GResult<<<Self as FromArg>::OutputCtor as Ctor>::Ty> {
		match temp {
			None => Ok(None),
			Some(temp) => Ok(Some(T::from_arg(temp)?))
		}
	}
}

/**
An adapter type which collects any number of trailing function arguments.

When [binding a Rust function](fn.rfn.html) so that it can be called from GameLisp, if `Rest<T>` 
appears at the end of the function's parameter list, it will collect all of the function's trailing 
arguments, converting them into a temporary array of `T` which is usually stored on the stack.

`Rest<T>` can be dereferenced to a mutable slice, `[T]`. It's also iterable, yielding `T`, `&T` 
or `&mut T` as appropriate.

	//a function which adds one or more integers together
	fn add_integers(first: i32, rest: Rest<i32>) -> i32 {
		let mut accumulator = first;
		for integer in rest {
			accumulator += integer;
		}

		accumulator
	}

	//bind this function to a global variable
	glsp::bind_rfn("add-integers", &add_integers)?;

	//the function can now be called from GameLisp
	eval!("
		(add-integers 42)
		(add-integers 10 20 30 40 50)
	")?;

	//or from Rust
	Rest::with([20, 30, 40, 50].iter().copied(), |rest| {
		add_integers(10, rest);
	});

It's possible to construct a custom `Rest<T>` yourself by calling [`Rest::with`](#method.with), 
but it's usually not elegant. Instead, consider defining a Rust function which receives a slice 
or a generic `IntoIterator`, and a wrapper which receives a `Rest<T>` and forwards it to the 
original function.
	
	fn add_integers(first: i32, rest: &[i32]) -> i32 {
		rest.iter().fold(first, |a, b| a + *b)
	}

	glsp::bind_rfn("add_integers", &|first: i32, rest: Rest<i32>| -> i32 {
		add_integers(first, &*rest)
	})?;
*/
pub struct Rest<'a, T>(&'a mut Option<SmallVec<[T; 8]>>);

impl<'a, T> Rest<'a, T> {
	#[inline]
	pub fn with<S, F, R>(src: S, f: F) -> R
	where
		S: IntoIterator<Item = T>,
		F: FnOnce(Rest<T>) -> R
	{
		f(Rest(&mut Some(SmallVec::from_iter(src))))
	}
}

impl<'a, T> Deref for Rest<'a, T> {
	type Target = [T];

	#[inline]
	fn deref(&self) -> &[T] {
		self.0.as_ref().unwrap()
	}
}

impl<'a, T> DerefMut for Rest<'a, T> {
	#[inline]
	fn deref_mut(&mut self) -> &mut[T] {
		self.0.as_mut().unwrap()
	}
}

impl<'a, T, I: SliceIndex<[T]>> Index<I> for Rest<'a, T> {
	type Output = I::Output;

	#[inline]
	fn index(&self, index: I) -> &I::Output {
		&(**self)[index]
	}
}

impl<'a, T, I: SliceIndex<[T]>> IndexMut<I> for Rest<'a, T> {
	#[inline]
	fn index_mut(&mut self, index: I) -> &mut I::Output {
		&mut (&mut **self)[index]
	}
}

impl<'a, T> IntoIterator for Rest<'a, T> {
	type Item = T;
	type IntoIter = smallvec::IntoIter<[T; 8]>;

	#[inline]
	fn into_iter(self) -> smallvec::IntoIter<[T; 8]> {
		self.0.take().unwrap().into_iter()
	}
}

impl<'r, 'a: 'r, T> IntoIterator for &'r Rest<'a, T> {
	type Item = &'r T;
	type IntoIter = slice::Iter<'r, T>;

	#[inline]
	fn into_iter(self) -> slice::Iter<'r, T> {
		self.0.as_ref().unwrap().iter()
	}
}

impl<'r, 'a: 'r, T> IntoIterator for &'r mut Rest<'a, T> {
	type Item = &'r mut T;
	type IntoIter = slice::IterMut<'r, T>;

	#[inline]
	fn into_iter(self) -> slice::IterMut<'r, T> {
		self.0.as_mut().unwrap().iter_mut()
	}
}

impl<'r, T: FromVal> FromArg for Rest<'r, T> {
	type Temp = (SmallVec<[Slot; 8]>, Option<SmallVec<[T; 8]>>);
	type OutputCtor = RestCtor<T>;

	#[inline]
	fn arg_type() -> ArgType {
		ArgType::Rest
	}

	#[inline]
	fn make_temp(
		args: &[Slot],
		i: usize
	) -> GResult<(SmallVec<[Slot; 8]>, Option<SmallVec<[T; 8]>>)> {
		/*
		we can't just call T::from_slot() here, because the argument slice
		is borrowed. a user-defined from_val() could do something which
		pushes to the reg stack, causing a panic
		*/

		Ok((
			SmallVec::from_iter(args[min(i, args.len())..].iter().cloned()),
			Some(SmallVec::with_capacity(args.len().saturating_sub(i)))
		))
	}

	#[inline]
	fn from_arg<'a>(
		temp: &'a mut (SmallVec<[Slot; 8]>, Option<SmallVec<[T; 8]>>)
	) -> GResult<Rest<'a, T>> {

		for arg in &temp.0 {
			temp.1.as_mut().unwrap().push(T::from_slot(arg)?);
		}

		Ok(Rest(&mut temp.1))
	}
}


impl<'r, T: FromVal> FromArg for &'r [T] {
	type Temp = (Slot, SmallVec<[T; 8]>);
	type OutputCtor = RefCtor<[T]>;

	#[inline]
	fn arg_type() -> ArgType {
		ArgType::Normal
	}

	#[inline]
	fn make_temp(args: &[Slot], i: usize) -> GResult<(Slot, SmallVec<[T; 8]>)> {
		/*
		we can't just call T::from_slot() here, because the argument slice
		is borrowed. a user-defined from_val() could do something which
		pushes to the reg stack, causing a panic
		*/

		Ok((args[i].clone(), SmallVec::with_capacity(args.len() - i)))
	}

	#[inline]
	fn from_arg<'a>(temp: &'a mut (Slot, SmallVec<[T; 8]>)) -> GResult<&'a [T]> {
		temp.1 = SmallVec::from_slot(&temp.0)?;
		Ok(&mut temp.1)
	}
}

impl<'r> FromArg for &'r str {
	type Temp = SmallVec<[u8; 128]>;
	type OutputCtor = RefCtor<str>;

	#[inline]
	fn arg_type() -> ArgType {
		ArgType::Normal
	}

	#[inline]
	fn make_temp(args: &[Slot], i: usize) -> GResult<SmallVec<[u8; 128]>> {
		let mut vec = SmallVec::<[u8; 128]>::new();

		match &args[i] {
			Slot::Str(st) => write!(&mut vec, "{}", st).unwrap(),
			slot => bail!("expected a &str, received {}", slot.a_type_name())
		}

		Ok(vec)
	}

	#[inline]
	fn from_arg<'a>(temp: &'a mut SmallVec<[u8; 128]>) -> GResult<&'a str> {
		Ok(str::from_utf8(&temp[..]).unwrap())
	}
}

macro_rules! impl_from_arg_text_slice (
	($(($slice_type:ident, $owned_type:ident)),+) => (
		$(
			impl<'r> FromArg for &'r $slice_type {
				type Temp = $owned_type;
				type OutputCtor = RefCtor<$slice_type>;

				#[inline]
				fn arg_type() -> ArgType {
					ArgType::Normal
				}

				#[inline]
				fn make_temp(args: &[Slot], i: usize) -> GResult<$owned_type> {
					$owned_type::from_slot(&args[i])
				}

				#[inline]
				fn from_arg<'a>(temp: &'a mut $owned_type) -> GResult<&'a $slice_type> {
					Ok(&**temp)
				}
			}
		)+
	);
);

impl_from_arg_text_slice!(
	(Path, PathBuf),
	(CStr, CString),
	(OsStr, OsString)
);

/*
rustc doesn't yet support specialization of associated types, so we need to dispatch this 
dynamically instead. i suspect that we won't see any unnecessary memcpys/memsets when,
for example, a DynTemp::<SmallVec<[u8; 1024]>>::RRef is constructed; it'll just waste a 
bit of uninitialized stack space instead.

i also hope that the `match` branches will be consistently optimized out, but i'm less 
confident of that...

we hedge our bets by marking make_temp() as #[inline(always)] rather than #[inline]
*/

pub enum DynTemp<T: 'static> {
	RGlobalRef(RGlobalRef<T>),
	RRef(RRef<T>),
	Slot(Slot)
}

pub enum DynTempMut<T: 'static> {
	RGlobalRefMut(RGlobalRefMut<T>),
	RRefMut(RRefMut<T>)
}

impl<'r, T: StaticMarker> FromArgRef for &'r T {
	type Temp = DynTemp<T>;
	type OutputCtor = RefCtor<T>;

	#[inline]
	default fn arg_type() -> ArgType {
		ArgType::Normal
	}

	#[inline(always)]
	default fn make_temp(args: &[Slot], i: usize) -> GResult<DynTemp<T>> {
		match &args[i] {
			Slot::RData(rdata) => Ok(DynTemp::RRef(rdata.borrow())),
			slot => bail!("expected &{}, received {}", type_name::<T>(), slot.a_type_name())
		}
	}

	#[inline]
	default fn from_arg<'a>(temp: &'a mut DynTemp<T>) -> GResult<&'a T> {
		match temp {
			DynTemp::RRef(temp) => Ok(&**temp),
			_ => unreachable!()
		}
	}
}

impl<'r, T: StaticMarker> FromArgRef for &'r mut T {
	type Temp = DynTempMut<T>;
	type OutputCtor = RefMutCtor<T>;

	#[inline]
	default fn arg_type() -> ArgType {
		ArgType::Normal
	}

	#[inline(always)]
	default fn make_temp(args: &[Slot], i: usize) -> GResult<DynTempMut<T>> {
		match &args[i] {
			Slot::RData(rdata) => Ok(DynTempMut::RRefMut(rdata.borrow_mut())),
			slot => bail!("expected &mut {}, received {}", type_name::<T>(), slot.a_type_name())
		}
	}

	#[inline]
	default fn from_arg<'a>(temp: &'a mut DynTempMut<T>) -> GResult<&'a mut T> {
		match temp {
			DynTempMut::RRefMut(temp) => Ok(&mut **temp),
			_ => unreachable!()
		}
	}
}

/*
ideally, we would impl FromArgRef for &mut T: FromVal + IntoVal. this would bring us closer
to supporting more Rust function signatures verbatim (e.g. File::read_to_string takes a 
&mut String), although it would carry a counterintuitive performance cost in some cases.

however, there's no way to establish a hierarchial relationship between those two traits 
and RGlobal.

we could exploit the FromArg/FromArgRef dichotomy by implementing FromArg for
&mut T: FromVal + IntoVal, because users are unlikely to implement both RGlobal and
FromVal/IntoVal for the same type. however, this conflicts with the blanket IntoVal
implementation for all 'static types.
*/

impl<'r, T: RGlobalMarker + Sized + StaticMarker> FromArgRef for &'r T {
	#[inline]
	fn arg_type() -> ArgType {
		ArgType::RGlobal
	}

	#[inline(always)]
	fn make_temp(_args: &[Slot], _i: usize) -> GResult<DynTemp<T>> {
		Ok(DynTemp::RGlobalRef(glsp::try_rglobal::<T>()?))
	}

	#[inline]
	fn from_arg<'a>(temp: &'a mut DynTemp<T>) -> GResult<&'a T> {
		match temp {
			DynTemp::RGlobalRef(temp) => Ok(&**temp),
			_ => unreachable!()
		}
	}
}

impl<'r, T: RGlobalMarker + Sized + StaticMarker> FromArgRef for &'r mut T {
	#[inline]
	fn arg_type() -> ArgType {
		ArgType::RGlobal
	}

	#[inline(always)]
	fn make_temp(_args: &[Slot], _i: usize) -> GResult<DynTempMut<T>> {
		Ok(DynTempMut::RGlobalRefMut(glsp::try_rglobal_mut::<T>()?))
	}

	#[inline]
	fn from_arg<'a>(temp: &'a mut DynTempMut<T>) -> GResult<&'a mut T> {
		match temp {
			DynTempMut::RGlobalRefMut(temp) => Ok(&mut **temp),
			_ => unreachable!()
		}
	}
}

impl<T: FromArgRef> FromArgRef for Option<T> {
	type Temp = Option<T::Temp>;
	type OutputCtor = OptionCtor<<T as FromArgRef>::OutputCtor>;

	#[inline]
	fn arg_type() -> ArgType {
		ArgType::Option
	}

	#[inline]
	fn make_temp(args: &[Slot], i: usize) -> GResult<Option<T::Temp>> {
		if i >= args.len() || matches!(args[i], Slot::Nil) {
			Ok(None)
		} else {
			Ok(Some(T::make_temp(args, i)?))
		}
	}

	#[inline]
	fn from_arg<'a>(
		temp: &'a mut Option<T::Temp>
	) -> GResult<<<Self as FromArgRef>::OutputCtor as Ctor>::Ty> {
		match temp {
			None => Ok(None),
			Some(temp) => Ok(Some(T::from_arg(temp)?))
		}
	}
}

macro_rules! impl_pointee_from_arg_ref {
	($($pointee:ident),+) => (
		$(
			impl<'r> FromArgRef for &'r $pointee {
				#[inline(always)]
				fn make_temp(args: &[Slot], i: usize) -> GResult<DynTemp<$pointee>> {
					Ok(DynTemp::Slot(args[i].clone()))
				}

				#[inline]
				fn from_arg<'a>(temp: &'a mut DynTemp<$pointee>) -> GResult<&'a $pointee> {
					match temp {
						DynTemp::Slot(Slot::$pointee(ref raw)) => Ok(&**raw),
						DynTemp::Slot(val) => {
							bail!(
								"expected &{}, received {}", 
								stringify!($pointee), (val.type_name())
							)
						}
						_ => unreachable!()
					}
				}
			}
		)+
	);
}

impl_pointee_from_arg_ref!(
	Arr,
	Str,
	Tab,
	GIter,
	GFn,
	Obj,
	Class,
	Coro,
	RData,
	RFn
);


//-------------------------------------------------------------------------------------------------
// wrap() and its supporting traits
//-------------------------------------------------------------------------------------------------

#[doc(hidden)]
pub trait CalculateArgLimits {
    fn calculate_arg_limits() -> (usize, usize) where Self: Sized;
}

#[doc(hidden)]
pub trait WrappedCall: CalculateArgLimits {
	fn arg_limits(&self) -> (usize, usize);
    fn wrapped_call(&self, args: Ref<[Slot]>) -> GResult<Slot>;
}

#[doc(hidden)]
pub struct Wrapper<ArgsWithTag, Ret, F> {
	f: F,
	arg_limits: (usize, usize),
	phantom: PhantomData<(ArgsWithTag, Ret)>
}

/*
previously, we enforced that F, and the wrapper thunk itself, must be convertible to bare 
function pointers. however, this was difficult to enforce and carried almost no performance 
benefit - benchmarking suggests that indirecting a call through Box<dyn WrappedCall> only costs
one additional cpu cycle, compared to a bare function pointer.
*/

pub(crate) fn wrap<ArgsWithTag, Ret, F>(f: F) -> Box<dyn WrappedCall> 
where
	Wrapper<ArgsWithTag, Ret, F>: WrappedCall + 'static
{
	Box::new(Wrapper {
		f,
		arg_limits: Wrapper::<ArgsWithTag, Ret, F>::calculate_arg_limits(),
		phantom: PhantomData
	})
}

macro_rules! arg_limits_fn {
	($fn_name:ident, $arg_count: literal; $($i:literal)*) => (

		#[allow(dead_code, unused_assignments, unused_mut, unused_variables)]
		const fn $fn_name(args: [ArgType; $arg_count]) -> Result<(usize, usize), &'static str> {
			let mut required_args = 0;
		    let mut optional_args = 0;
		    let mut seen_rest = false;
		    let mut seen_opt = false;
		    
		   $(
			    match args[$i] {
			        ArgType::Normal => {
			            if seen_rest {
			                return Err("Rest<T> followed by a normal argument")
			            }
			            
			            required_args += optional_args;
			            optional_args = 0;

			            required_args += 1;
			        }
			        ArgType::Option => {
			        	 if seen_rest {
			                return Err("Rest<T> followed by an Option<T> argument")
			            }

			            optional_args += 1;
			        }
			        ArgType::Rest => {
			            seen_rest = true;
			        }
			        _ => () //unreachable, but we can't panic in const fns yet (todo)
			    }
			)*
		    
		    Ok((
		    	required_args,
		    	if seen_rest { usize::MAX } else { required_args + optional_args }
		    ))
		}
	);
}

arg_limits_fn!(arg_limits_0, 0;);
arg_limits_fn!(arg_limits_1, 1; 0);
arg_limits_fn!(arg_limits_2, 2; 0 1);
arg_limits_fn!(arg_limits_3, 3; 0 1 2);
arg_limits_fn!(arg_limits_4, 4; 0 1 2 3);
arg_limits_fn!(arg_limits_5, 5; 0 1 2 3 4);
arg_limits_fn!(arg_limits_6, 6; 0 1 2 3 4 5);
arg_limits_fn!(arg_limits_7, 7; 0 1 2 3 4 5 6);
arg_limits_fn!(arg_limits_8, 8; 0 1 2 3 4 5 6 7);

#[doc(hidden)]
pub struct TagArg;

#[doc(hidden)]
pub struct TagArgRef;

//an ugly workaround for rustc's tendency to fail to normalize associated types
//when they're accessed via a HRTB
pub trait OutputIntoVal<T>: Fn<T> {
	fn output_into_slot(output: <Self as FnOnce<T>>::Output) -> GResult<Slot>;
}
impl<T, F> OutputIntoVal<T> for F
where
	F: Fn<T>,
	<F as FnOnce<T>>::Output: IntoVal
{
	fn output_into_slot(output: <Self as FnOnce<T>>::Output) -> GResult<Slot> {
		output.into_slot()
	}
}

macro_rules! wrap_tuple_impls {
	(
		$arg_limits_fn:ident, $arg_count:literal;
		$($arg_t:ident $arg_trait:ident $arg_tag:ident),*;
		$($temp_name:ident),*
	) => (

		impl<$($arg_t,)* Ret, F> /*const*/
			CalculateArgLimits
			for
			Wrapper<($(($arg_t, $arg_tag),)*), Ret, F>
		where
			$(
				$arg_t: $arg_trait,
			)*
		{
			fn calculate_arg_limits() -> (usize, usize) where Self: Sized {
				$arg_limits_fn([$($arg_t::arg_type(),)*]).unwrap()
			}
		}

		#[allow(dead_code, unused_assignments, unused_mut, unused_variables)]
		impl<$($arg_t,)* Ret, F>
			WrappedCall
			for
			Wrapper<($(($arg_t, $arg_tag),)*), Ret, F>
		where
			$(
				$arg_t: $arg_trait,
			)*
			Ret: IntoVal,
			F: Fn($($arg_t,)*) -> Ret,
			F: for<'a> Fn(
				$(<<$arg_t as $arg_trait>::OutputCtor as Ctor<'a>>::Ty,)*
			) -> Ret,
			F: for<'a> OutputIntoVal<(
				$(<<$arg_t as $arg_trait>::OutputCtor as Ctor<'a>>::Ty,)*
			)>
		{
			fn arg_limits(&self) -> (usize, usize) {
				self.arg_limits
			}

			fn wrapped_call(&self, args: Ref<[Slot]>) -> GResult<Slot> {
				/*
				a dilemma: should we emit argument bounds-checks here, or store them
				in the RFn struct and check them before performing the call?

				advantages of checking here:
				- will probably increase bound-check elision in make_temp() and from_arg(),
				  iff the limits are constants
				- if we can make them fully const, comparisons against `usize` constants will
				  probably be a cycle or two cheaper than comparisons against fields

				disadvantages:
				- if we can't make the arg_limits an actual const, constant-folding might
				  fail, executing a large amount of code on every call
				- we could move these checks elsewhere to reduce monomorphization cost (presumably
				  only slightly?) and the initial cost of compiling the macro-generated impls

				for now, we perform the check here, but it's non-const. intend to switch the
				arg limits into constants asap, once the const_trait_impls feature no longer has 
				the incomplete_features warning (todo)
				*/

				if args.len() < self.arg_limits.0 {
					bail!(
						"too few arguments: received {}, expected at least {}", 
						args.len(),
						self.arg_limits.0
					)
				}

				if args.len() > self.arg_limits.1 {
					bail!(
						"too many arguments: received {}, expected no more than {}",
						args.len(),
						self.arg_limits.1
					)
				}

				let mut arg_i = 0;

				$(
					let mut $temp_name = $arg_t::make_temp(&args, arg_i)?;

					if $arg_t::arg_type() != ArgType::RGlobal {
						arg_i += 1;
					}
				)*

				drop(args);

				let output = (self.f)($(
					$arg_t::from_arg(&mut $temp_name)?
				),*);

				F::output_into_slot(output)
			}
		}
	);
}

/*
for processing arguments, we really need three blanket implementations: FromArg for all 
T: FromVal, FromArg for all &T where T: 'static, and FromArg for all &T where 
T: RGlobal + 'static. unfortunately, this isn't even possible with specialization, because 
rustc doesn't consider &T to be "more specialized" than T, and there's no way to prevent
the user from implementing FromVal for reference types.

we can achieve what we need by having two non-overlapping traits, one for reference arguments
and one for value arguments. however, rustc's trait system doesn't support any kind of
negative reasoning, and it might never be supported, because it causes serious trouble with api
stability. this means that we have no good way of saying "these two traits are disjoint, please 
fail at [compile time|runtime] if they're both implemented on the same type".

our very ugly trick to work around it...

	trait Api<T> { ... }

	struct TagA;
	struct TagB;

	impl<T: TraitA> Api<(T, TagA)> for T { ... }
	impl<T: TraitB> Api<(T, TagB)> for T { ... }

	fn api<T, F>(f: F) where F: Api<T> + Fn(T) { ... }

Api<(T, TagA)> and Api<(T, TagB)> don't overlap; they're distinct traits which happen to 
have a similar api, and similar behaviour for the purposes of type inference.

if the user passes in a function to api() with an argument which implements both TraitA and 
TraitB, type inference will fail; but for any argument which implements only one of those 
two traits, inference will succeed. we're careful to only implement FromArg and FromArgRef in
ways that won't overlap, unless the user implements FromVal for something very weird like
a static slice.

if Api<T>'s type parameter is replaced with a tuple of 2-tuples, this lets us blanket-implement 
for<T: TraitA, U: TraitB> Api<((T, TagA), (U, TagB))>, and then blanket-implement 
for<T: TraitB, U: TraitA> Api<((T, TagB), (U, TagA))>, and so on, so that we can support 
arbitrary argument lists.
	
we can infer for any combination of up to N arguments by using macros to implement the trait
((N+1)^2 - 1) times. we currently support eight arguments, which requires 511 implementations.
rustc handles it better than you might expect; it costs around 20 seconds on an incremental
`cargo check` when modifying the `glsp-engine` crate, and it seems to have zero cost on 
an incremental `cargo check` for a crate which uses glsp, when glsp itself hasn't been modified.

based on testing, switching from nested tuples ((A, B), (C, D)) to flat tuples (A, B, C, D) 
doesn't seem to affect compile time for `glsp-stdlib`. reducing the maximum number of arguments 
from 8 down to 4 also had no effect.
*/

macro_rules! forward_to_wrap_tuple_impls {
	(
		$($t:ident $i:literal $temp_name:ident $trait:ident $tag:ident,)*;
		$arg_count:literal $arg_limits_fn:ident
	) => (
		wrap_tuple_impls!(
			$arg_limits_fn, $arg_count;
			$($t $trait $tag),*;
			$($temp_name),*
		);
	);
}

macro_rules! recurse {
	(
		$first_t:ident $first_i:literal $first_temp:ident $first_fn:ident,
		$($rest_t:ident $rest_i:literal $rest_temp:ident $rest_fn:ident,)*;
		$($accum_t:ident $accum_i:literal $accum_temp:ident
			$accum_fn:ident $accum_trait:ident $accum_tag:ident,)*
	) => (
		forward_to_wrap_tuple_impls!(
			$($accum_t $accum_i $accum_temp $accum_trait $accum_tag,)*; $first_i $first_fn
		);

		recurse!(
			$($rest_t $rest_i $rest_temp $rest_fn,)*;
			$($accum_t $accum_i $accum_temp $accum_fn $accum_trait $accum_tag,)*
			$first_t $first_i $first_temp $first_fn FromArg TagArg,
		);
		recurse!(
			$($rest_t $rest_i $rest_temp $rest_fn,)*;
			$($accum_t $accum_i $accum_temp $accum_fn $accum_trait $accum_tag,)*
			$first_t $first_i $first_temp $first_fn FromArgRef TagArgRef,
		);
	);
	(; $($t:ident $i:literal $temp:ident $arg_limits_fn:ident $trait:ident $tag:ident,)*) => (
		forward_to_wrap_tuple_impls!(
			$($t $i $temp $trait $tag,)*; 8 arg_limits_8
		);
	);
}

recurse!(
	T0 0 temp_0 arg_limits_0,
	T1 1 temp_1 arg_limits_1,
	T2 2 temp_2 arg_limits_2,
	T3 3 temp_3 arg_limits_3,
	T4 4 temp_4 arg_limits_4,
	T5 5 temp_5 arg_limits_5,
	T6 6 temp_6 arg_limits_6,
	T7 7 temp_7 arg_limits_7,
	;
);


//-------------------------------------------------------------------------------------------------
// Callable, CallableOps, IntoCallArgs
//-------------------------------------------------------------------------------------------------

/**
A type-erased `callable`.

Because this type implements the [`CallableOps` trait](trait.CallableOps.html), you can call 
it directly, without needing to access the underlying types.
*/

#[derive(Clone, Debug)]
pub enum Callable {
	RFn(Root<RFn>),
	GFn(Root<GFn>),
	Class(Root<Class>)
}

/**
The `callable` abstract type.

[`glsp:call`](fn.call.html) can be used to call any type which implements this trait.

This trait is [sealed]. It's not possible to implement this trait for your own types.

[sealed]: https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed
*/

pub trait CallableOps: callable_ops_private::Sealed {
	#[doc(hidden)]
	fn receive_call(&self, arg_count: usize) -> GResult<Val>;

	///Returns this function's registered name, if any.
	fn name(&self) -> Option<Sym>;

	///Returns this function's minimum and maximum argument count.
	fn arg_limits(&self) -> (usize, Option<usize>);

	///Returns this function's minimum argument count.
	fn min_args(&self) -> usize {
		self.arg_limits().0
	}

	///Returns this function's maximum argument count, if any.
	fn max_args(&self) -> Option<usize> {
		self.arg_limits().1
	}
}

mod callable_ops_private {
	use crate::{class::Class, code::GFn, engine::RFn, gc::{Raw, Root}, wrap::Callable}; 

	pub trait Sealed { }

	impl Sealed for Callable { }
	impl Sealed for Root<RFn> { }
	impl Sealed for Raw<RFn> { }
	impl Sealed for Root<Class> { }
	impl Sealed for Raw<Class> { }
	impl Sealed for Root<GFn> { }
	impl Sealed for Raw<GFn> { }
}

impl CallableOps for Callable {
	#[inline]
	fn receive_call(&self, arg_count: usize) -> GResult<Val> {
		match *self {
			Callable::RFn(ref rfn_root) => {
				glsp::call_rfn(rfn_root, arg_count).map(|slot| slot.root())
			}
			Callable::GFn(ref gfn_root) => glsp::call_gfn(gfn_root, arg_count),
			Callable::Class(ref class_root) => Ok(Val::Obj(glsp::call_class(class_root, arg_count)?))
		}
	}

	fn arg_limits(&self) -> (usize, Option<usize>) {
		match *self {
			Callable::RFn(ref rfn_root) => rfn_root.arg_limits(),
			Callable::GFn(ref gfn_root) => gfn_root.arg_limits(),
			Callable::Class(ref class_root) => class_root.arg_limits()
		}
	}

	fn name(&self) -> Option<Sym> {
		match *self {
			Callable::RFn(ref rfn_root) => rfn_root.name(),
			Callable::GFn(ref gfn_root) => gfn_root.name(),
			Callable::Class(ref class_root) => class_root.name()
		}
	}
}

/**
A type which can be converted into the arguments to a function call.

It's not possible to implement this trait for your own types, but it's implemented for tuples,
slices, arrays, and references to the same, when their elements all implement 
[`IntoVal`](trait.IntoVal.html).

Functions like [`glsp:call`](fn.call.html) and [`Obj::call`](struct.Obj.html#method.call) are
generic over this trait:
	
	let push_rfn: RFn = glsp::global("push!");
	glsp::call(&push_rfn, (my_arr, 100i32))?;

Due to some limitations in Rust's type system, argument lists can be slightly awkward:

- It's not possible to pass in `&[]` or `[]` to represent an empty argument list. You should 
  use `&()` or `()` instead.
- `IntoVal` isn't implemented for references to references, so types like `&(&f32, &f32)`,
  `&[&Root<Arr>]` and `[&i32; 16]` won't be accepted. When working with references, prefer
  to use tuples, like `(&f32, &f32)`.
*/

pub trait IntoCallArgs: into_call_args_private::Sealed {
	fn arg_count(&self) -> usize;
	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()>;
}

mod into_call_args_private {
	use crate::{wrap::{IntoVal, Rest}};

	pub trait Sealed { }

	impl<'a, T> Sealed for &'a [T] where &'a T: IntoVal { }
	impl<'a, T> Sealed for &'a mut [T] where &'a mut T: IntoVal { }

	impl<T, const N: usize> Sealed for [T; N] where for<'a> &'a T: IntoVal { }
	impl<'a, T, const N: usize> Sealed for &'a [T; N] where &'a T: IntoVal { }
	impl<'a, T, const N: usize> Sealed for &'a mut [T; N] where &'a mut T: IntoVal { }

	impl<'a, T> Sealed for Rest<'a, T> where T: IntoVal { }
	impl<'r, 'a: 'r, T> Sealed for &'r Rest<'a, T> where &'r T: IntoVal { }
	impl<'r, 'a: 'r, T> Sealed for &'r mut Rest<'a, T> where &'r mut T: IntoVal { }

	impl Sealed for () { }
	impl<'a> Sealed for &'a () { }
	impl<'a> Sealed for &'a mut () { }
}

impl<'a, T> IntoCallArgs for &'a [T] where &'a T: IntoVal {
	fn arg_count(&self) -> usize {
		self.len()
	}

	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()> {
		let mut result = Ok(());
		dst.extend(self.iter().map(|item| {
			match item.into_slot() {
				Ok(slot) => slot,
				Err(err) => {
					if result.is_ok() {
						result = Err(err);
					}
					Slot::Nil
				}
			}
		}));
		result
	}
}

impl<'a, T> IntoCallArgs for &'a mut [T] where &'a mut T: IntoVal {
	fn arg_count(&self) -> usize {
		self.len()
	}

	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()> {
		let mut result = Ok(());
		dst.extend(self.iter_mut().map(|item| {
			match item.into_slot() {
				Ok(slot) => slot,
				Err(err) => {
					if result.is_ok() {
						result = Err(err);
					}
					Slot::Nil
				}
			}
		}));
		result
	}
}

impl<T, const N: usize> IntoCallArgs for [T; N] where for<'a> &'a T: IntoVal {
	fn arg_count(&self) -> usize {
		N
	}

	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()> {
		(&self[..]).into_call_args(dst)
	}
}

impl<'a, T, const N: usize> IntoCallArgs for &'a [T; N] where &'a T: IntoVal {
	fn arg_count(&self) -> usize {
		N
	}

	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()> {
		(&self[..]).into_call_args(dst)
	}
}

impl<'a, T, const N: usize> IntoCallArgs for &'a mut [T; N] where &'a mut T: IntoVal {
	fn arg_count(&self) -> usize {
		N
	}

	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()> {
		(&mut self[..]).into_call_args(dst)
	}
}

impl<'a, T> IntoCallArgs for Rest<'a, T> where T: IntoVal {
	fn arg_count(&self) -> usize {
		self.len()
	}

	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()> {
		let mut result = Ok(());
		dst.extend(self.into_iter().map(|item| {
			match item.into_slot() {
				Ok(slot) => slot,
				Err(err) => {
					if result.is_ok() {
						result = Err(err);
					}
					Slot::Nil
				}
			}
		}));
		result
	}
}

impl<'r, 'a: 'r, T> IntoCallArgs for &'r Rest<'a, T> where &'r T: IntoVal {
	fn arg_count(&self) -> usize {
		self.len()
	}

	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()> {
		let mut result = Ok(());
		dst.extend(self.into_iter().map(|item| {
			match item.into_slot() {
				Ok(slot) => slot,
				Err(err) => {
					if result.is_ok() {
						result = Err(err);
					}
					Slot::Nil
				}
			}
		}));
		result
	}
}

impl<'r, 'a: 'r, T> IntoCallArgs for &'r mut Rest<'a, T> where &'r mut T: IntoVal {
	fn arg_count(&self) -> usize {
		self.len()
	}

	fn into_call_args<E: Extend<Slot>>(self, dst: &mut E) -> GResult<()> {
		let mut result = Ok(());
		dst.extend(self.into_iter().map(|item| {
			match item.into_slot() {
				Ok(slot) => slot,
				Err(err) => {
					if result.is_ok() {
						result = Err(err);
					}
					Slot::Nil
				}
			}
		}));
		result
	}
}

impl IntoCallArgs for () {
	fn arg_count(&self) -> usize {
		0
	}
	
	fn into_call_args<E: Extend<Slot>>(self, _dst: &mut E) -> GResult<()> {
		Ok(())
	}
}

impl<'a> IntoCallArgs for &'a () {
	fn arg_count(&self) -> usize {
		0
	}
	
	fn into_call_args<E: Extend<Slot>>(self, _dst: &mut E) -> GResult<()> {
		Ok(())
	}
}

impl<'a> IntoCallArgs for &'a mut () {
	fn arg_count(&self) -> usize {
		0
	}
	
	fn into_call_args<E: Extend<Slot>>(self, _dst: &mut E) -> GResult<()> {
		Ok(())
	}
}

macro_rules! impl_into_call_args_tuple {
	($len:literal: $($t:ident $i:tt),+) => (
		impl<$($t),+> into_call_args_private::Sealed for ($($t,)+)
			where $( $t: IntoVal ),+ { }

		impl<'a, $($t),+> into_call_args_private::Sealed for &'a ($($t,)+)
			where $( &'a $t: IntoVal ),+ { }

		impl<'a, $($t),+> into_call_args_private::Sealed for &'a mut ($($t,)+)
			where $( &'a mut $t: IntoVal ),+ { }

		impl<$($t),+> IntoCallArgs for ($($t,)+)
		where 
			$( $t: IntoVal ),+ 
		{
			fn arg_count(&self) -> usize {
				$len
			}
			
			fn into_call_args<EE: Extend<Slot>>(self, dst: &mut EE) -> GResult<()> {
				let slots = [ $(
					(self.$i).into_slot()?
				),+ ];

				dst.extend(slots.iter().cloned());
				Ok(())
			}
		}

		impl<'a, $($t),+> IntoCallArgs for &'a ($($t,)+)
		where 
			$( &'a $t: IntoVal ),+ 
		{
			fn arg_count(&self) -> usize {
				$len
			}
			
			fn into_call_args<EE: Extend<Slot>>(self, dst: &mut EE) -> GResult<()> {
				let slots = [ $(
					(&self.$i).into_slot()?
				),+ ];

				dst.extend(slots.iter().cloned());
				Ok(())
			}
		}

		impl<'a, $($t),+> IntoCallArgs for &'a mut ($($t,)+)
		where 
			$( &'a mut $t: IntoVal ),+ 
		{
			fn arg_count(&self) -> usize {
				$len
			}
			
			fn into_call_args<EE: Extend<Slot>>(self, dst: &mut EE) -> GResult<()> {
				let slots = [ $(
					(&mut self.$i).into_slot()?
				),+ ];

				dst.extend(slots.iter().cloned());
				Ok(())
			}
		}
	);
}

impl_into_call_args_tuple!( 1: A 0);
impl_into_call_args_tuple!( 2: A 0, B 1);
impl_into_call_args_tuple!( 3: A 0, B 1, C 2);
impl_into_call_args_tuple!( 4: A 0, B 1, C 2, D 3);
impl_into_call_args_tuple!( 5: A 0, B 1, C 2, D 3, E 4);
impl_into_call_args_tuple!( 6: A 0, B 1, C 2, D 3, E 4, F 5);
impl_into_call_args_tuple!( 7: A 0, B 1, C 2, D 3, E 4, F 5, G 6);
impl_into_call_args_tuple!( 8: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7);
impl_into_call_args_tuple!( 9: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8);
impl_into_call_args_tuple!(10: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9);
impl_into_call_args_tuple!(11: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10);
impl_into_call_args_tuple!(12: A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7, I 8, J 9, K 10, L 11);