ender 0.5.2

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

//! A rust **EN**coding and **DE**coding library for writing custom protocols and file formats.
//!
//! It aims to be **intuitive**, **expandable** and **correct**.
//!
//! # Example
//!
//! ```rust
//! # use ender::*;
//! # use ender::io::*;
//! # #[derive(Debug, PartialEq, Encode, Decode)]
//! # #[ender(crate: ender)]
//! # struct Person {
//! # 	name: String,
//! #    age: u64,
//! #    height: f32,
//! #    eye_color: EyeColor,
//! # }
//! #
//! # #[derive(Debug, PartialEq, Encode, Decode)]
//! # #[ender(crate: ender)]
//! # enum EyeColor {
//! #    Brown,
//! #    Blue,
//! #    Green,
//! #    Gray,
//! #    Black
//! # }
//! #
//! # fn hidden() -> EncodingResult<()> {
//! let mut the_matrix = vec![0u8; 256];
//!
//! // Summon John into existence!
//! let john = Person {
//!     name: String::from("John"),
//!     age: 35,
//! 	height: 1.75,
//! 	eye_color: EyeColor::Brown
//! };
//!
//! // Encode John into the matrix
//! encode_with(SliceMut::new(&mut the_matrix), Context::default(), &john)?;
//!
//! // Bring him back
//! let john_2: Person = decode_with(Slice::new(&the_matrix), Context::default())?;
//!
//! // But is he really the same John?
//! assert_eq!(john, john_2);
//! # Ok(())
//! # }
//! # fn main() { hidden().unwrap() }
//! ```
//!
//! # Encoding format
//!
//! The encoding process aims at being **correct** and **unsurprising**.
//!
//!
//!
//! Ende supports a series of options, which can be changed *during*
//! the encoding/decoding process to get certain parts of a binary format
//! to be represented exactly how you want them.
//!
//! This can be done in a manual implementation as well as with the derive
//! macro, using the custom attributes provided.
//!
//! Certain types also support "flattening", which means omitting information
//! known from the context.
//! For instance, you can omit writing whether an `Option` is present if
//! that information is already stored somewhere else in the file format.
//!
//! For *integer primitives*, *usize*, and *enum variants* you can customize the endianness,
//! the numerical encoding (read: var-ints), the bit-width (how many bytes
//! does a `usize` or enum variant take up in your encoding format?),
//! the max-size (to prevent maliciously crafted binary formats to cause
//! unlimited-size allocations).
//!
//! ### Var-int format
//! - Fixed - not var-int, simply encode the number as-is
//! - Leb128
//! - Protobuf - both its zigzag and "wasteful" variants
//!
//! ### String formats
//! As for strings, currently length-prefixed, null-terminated (with and without
//! a maximum length) strings are supported, as well as the following encoding formats.
//! - Ascii
//! - Utf8
//! - Utf16
//! - Utf32
//! - Windows1252
//!
//! If you need a new var-int encoding or string encoding added, feel free
//! to open a PR!
//!
//! # Motivation
//!
//! One of the main reasons I made this library is because I found myself
//! needing more sophisticate macros and runtime flexibility for existing
//! binary formats.
//!
//! While for example [`bincode`](https://crates.io/crates/bincode) is perfectly
//! ok for many applications, `ender` was made with compatibility with existing
//! data formats in mind.
//!
//! For this very purpose, many internal details of the encoder are exposed
//! through settings or the derive macros themselves, for the purpose of fine-tuning
//! the data format exactly how you want it, while providing an easy-to-understand interface.
//!
//! # Deriving
//!
//! A big selling point of `ender` are its macros, which allow you to heavily
//! customize the codegen through a series of attributes.
//! To learn more about those, check out `DERIVE.md` in this crate's repository root.
//!
//! # MSRV
//!
//! This crate will always target the latest version of rust, in order
//! to get access to new features as soon as they are released and
//! update the code accordingly if beneficial.
//! Of course, breaking API changes will be accompanied by a major version
//! bump.
//!
//! # Future plans
//!
//! I plan on adding support for `async` io through a feature gate.

#[cfg(feature = "alloc")]
extern crate alloc;

use core::any::Any;
use core::fmt::Debug;
use core::hash::Hash;
use core::mem::replace;

use parse_display::Display;

/// # The Ender Derivonomicon
///
/// The following is a full and up-to-date breakdown of the `Encode` and `Decode` derive macros,
/// their attributes, how they work and all the possible ways you can modify the codegen.
///
/// ## Flags Essentials
///
/// All flags follow the following format:
/// `#[ender(flag1; flag2; flag2; ...)]`
///
/// The 2 special flags `en` and `de`, called Scope flags, can be used only at the beginning
/// of the list to indicate that all the flags in the attribute declaration only apply to the
/// encoding process (`en`) or the decoding process (`de`).
///
/// If neither of those flags are specified, then it is assumed that all the flags in the
/// attribute declaration apply to both encoding and decoding.
///
/// Whenever a flag is said to accept an `$expr`, this means any expression is accepted,
/// and that it will have access to an immutable reference to all the fields that have been
/// decoded so far (actually all the fields while encoding), but the `validate` flag additionally
/// provides a reference to the field it is applied on.<br>
/// If the fields are part of a tuple struct/variant, the references will be named `m{idx}` where `idx` are the
/// indexes of the tuple fields (E.G. `m0`, `m1`, ...), otherwise their names will match those of the fields themselves.
///
/// A seeking impl is an implementation of [`Encode`] or [`Decode`] that has an additional [`Seek`]
/// trait bound
/// When a flag is said to be a `seek` flag, it means that when used anywhere it will switch the
/// impl to a seeking impl.
///
/// A borrowing impl is an implementation of [`Encode`] or [`Decode`] that has a [`BorrowRead`]
/// trait bound rather than a [`Read`] one.
/// When a flag is said to be a `borrow` flag, it means that when used anywhere it will switch the
/// impl to a borrowing impl.
///
/// Borrowing and seeking impls can be combined.
///
/// The flags currently implemented are split into 5 groups:
/// # 1. Setting Modifiers
/// Setting-Modifier flags temporarily change certain settings of the encoder and can be applied
/// to Fields or Items (the whole struct or enum).<br>
/// Multiple can be specified at the same time, as long as they don't overlap.<br>
/// All setting modifiers follow the `$target: $mod1, $mod2, ...` pattern.
///
/// - Endianness modifiers: `big_endian`, `little_endian`
///   - Available targets:
///     - `num`
///     - `size`
///     - `variant`
///     - `string`
/// - Numerical encoding modifiers: `fixed`, `leb128`, `protobuf_wasteful`, `protobuf_zz`
///   - Available targets:
///     - `num`,
///     - `size`,
///     - `variant`
/// - Bit-width modifiers: `bit8`, `bit16`, `bit32`, `bit64`, `bit128`
///   - Available targets:
///     - `size`
///     - `variant`
/// - Max-size modifier: `max = $expr`
///   - Available targets:
///     - `size`
/// - String encoding modifier: `ascii`, `utf8`, `utf16`, `utf32`, `windows1252`
///   - Available targets:
///     - `string`
/// - String length encoding modifier: `len_prefix`, `null_term`, `null_term($max:expr)`
///   - Available targets:
///     - `string`
///     <br>
/// ### Example:
/// ```rust
/// # use ender::{Encode, Decode};
/// #[derive(Encode, Decode)]
/// # #[ender(crate: ender)]
/// /// The variants of this enum will be encoded in the little endian ordering,
/// /// using a fixed numerical encoding and a 32-bit width.
/// #[ender(variant: little_endian, fixed, bit32)]
/// enum MyEnum {
///     VariantA {
///         /// The length of this String will be encoded using big endian ordering,
///         /// fixed numerical encoding and 16-bit width, with a max length of 100
///         #[ender(size: big_endian, fixed, bit16, max = 100)]
///         field: String,
///         /// Encode this String with utf16 big endian, and prefix it with its length
///         #[ender(string: utf16, big_endian, len_prefix)]
///         utf_16: String,
///         /// Encode this String as an utf8 null-terminated string
///         #[ender(string: utf8, null_term)]
///         utf_8: String,
///         /// Encode this String as an utf8 null-terminated string with a fixed length of `15`
///         /// Exactly `15` bytes will be read in all cases, and the length of the string is given
///         /// by the first occurrence of a null byte or the `15` byte mark.
///         #[ender(string: utf8, null_term(15))]
///         max_15: String,
///     },
///     VariantB {
///         /// This number will be encoded using little endian ordering, and the
///         /// leb128 [NumEncoding][`ender::NumEncoding`]
///         #[ender(num: little_endian, leb128)]
///         field: u64,
///         #[ender(num: protobuf_zz)]
///         zigzag: i128,
/// 	},
/// }
/// ```
/// # 2. Stream Modifiers
/// Stream-Modifier flags temporarily change the underlying reader/writer, and can be applied
/// to Fields or Items.<br>
/// Note that the order in which stream modifiers are declared is very important:<br>
/// They are applied in the declaration order during encoding, but in the reverse order during
/// decoding, for consistency. However, the item-level modifiers take priority over the field-level
/// modifiers (see [ambiguous example](#ambiguous-example)).<br>
/// * `redir: $path(...)` - Uses the given path to find an encoding/decoding function which
/// alters the writer/reader and passes a modified encoder to a closure.<br>
/// This can be used to implement encryption, compression and other transformations of the
/// underlying stream, or even redirections to another stream.<br>
/// The implementation of a redir function can be non-trivial and the signature can be
/// cryptic, therefore it is recommended you only create your own stream transforms if you know what
/// you're doing, in which case you should take a look at the [facade module][`facade`].<br>
///     * If the scope is Encode, the path must be callable as `encode`.<br>
///     * If the scope is Decode, the path must be callable as `decode`.<br>
///     * If no scope is specified, the path must point to a module with encoding and decoding functions
/// with the same signatures as above.
/// * `ptr $seek: $expr` - This is a `seek` flag. Seeks to the location given by `$expr`
/// (which must be of type usize or isize) relative to `$seek` - which can be
/// `start`, `end` or `cur`rrent - before encoding/decoding this field, then seeks back to the
/// previous location.
/// ### Example:
/// ```rust
/// # use ender::{Encode, Decode};
/// # use ender::facade::fake::*;
/// #[derive(Encode, Decode)]
/// # #[ender(crate: ender)]
/// struct MyStruct {
///     secret_key: Vec<u8>,
///     iv: Vec<u8>,
///     /// While **encoding**, this field is compressed -> encrypted.
///     /// While **decoding**, this field is decrypted -> decompressed.
///     #[ender(redir: gzip(9))]
///     #[ender(redir: aes(iv, secret_key))]
///     super_secret_data: Vec<u8>,
///     file_pointer: usize,
///     /// Marks the current offset, seeks to `file_pointer` bytes from the start of the file,
///     /// encodes/decodes the field, then seeks back.
///     #[ender(ptr start: *file_pointer)]
///     apple_count: u64,
///     /// This field is effectively laid *right after* `file_pointer`
///     /// in the binary representation.
///     other_data: i32,
/// }
/// ```
/// ### Ambiguous example:
/// ```rust
/// # use ender::{Encode, Decode};
/// # use ender::facade::fake::*;
/// #[derive(Encode, Decode)]
/// # #[ender(crate: ender)]
/// /// Because of the priority rules of items over fields, this is ambiguous, see below
/// #[ender(redir: gzip(9))]
/// struct MyStruct {
///     /// While **encoding**, this field is encrypted -> compressed.
///     /// While **decoding**, this field is decompressed -> decrypted.
///     /// Because the "compressed" flag is declared before the "encrypted" flag, one might
///     /// think they are applied in that order. However, since the item-level flag takes priority,
///     /// it is applied *after* the field flag.
///     ///
///     /// According to your needs, this might not be what you want, so be careful when mixing
///     /// item-level and field-level stream modifiers.
///     #[ender(redir: aes(&[], &[]))]
///     super_secret_data: Vec<u8>,
/// }
/// ```
/// # 3. Function Modifiers
/// Function-Modifier flags change the function that is used to encode/decode a field or item.
/// * `serde: $crate` - Field will be serialized/deserialized with a serde compatibility layer.
/// For the `decode_in_place` implementation, `deserialize_in_place` will be used.<br>
/// Optionally, the serde crate name can be specified (useful if the serde crate was re-exported under
/// another name).
/// * `with: $path` - Uses the given path to find the encoding/decoding function.<br>
/// This prevents optimizations in the `decode_in_place` implementation because it cannot predict
/// if a specialized `decode_in_place` function exists in the module, so it will have to
/// decode and then assign. If your module contains a specialized `decode_in_place`, you must use
/// `with(in_place): $path` which actually expects the function to exist.
///     * If the scope is Encode, the path must be callable as
/// `fn<T: Write>(&V, &mut ender::Encoder<T>) -> EncodingResult<()>`
/// where `V` is the type of the field (the function is allowed to be generic over `V`).<br>
///     * If the scope is Decode, the path must be callable as
/// `fn<T: Read>(&mut ender::Encoder<T>) -> EncodingResult<V>`
/// where `V` is the type of the field (the function is allowed to be generic over `V`).<br>
///     * If no scope is specified, the path must point to a module with encoding and decoding functions
/// with the same signatures as above.<br>
/// * `with(in_place): $path` Uses the given path to find the encoding/decoding function, but
/// expects to find a dedicated `decode_in_place` function too, callable as
/// `fn<T: Read>(&mut V, &mut ender::Encoder<T>) -> EncodingResult<()>`
/// where `V` is the type of the field (the function is allowed to be generic over `V`).<br>
/// Differently from its counterpart, this modifier expects the path to be a module, even when
/// a scope flag is explicitly specified.<br>
/// ### Example:
/// ```rust
/// # use ender::{Encode, Decode};
/// # use ender::facade::fake::rsa;
/// # use uuid::Uuid;
/// # use person_encoder::Person;
/// #[derive(Encode, Decode)]
/// # #[ender(crate: ender)]
/// struct Friends {
/// 	/// Has Serialize/Deserialize implementations, but no Encode/Decode implementations.
///     /// A perfect fit for integrating with serde!
///     #[ender(serde)]
///     uuid: Uuid,
///     /// Here we demonstrate how the with flag changes based on whether a scope
///     /// is declared. This:
///     #[ender(with: person_encoder)]
///     friend1: Person,
///     /// ...is equivalent to this!
///     #[ender(en; with: person_encoder::encode)]
///     #[ender(de; with: person_encoder::decode)]
///     friend2: Person,
///     /// Not the smartest way to store a private key!
///     private_key: Vec<u8>,
///     public_key: Vec<u8>,
///     /// This block of data will be encrypted before being encoded using the public key,
///     /// and decrypted after being decoded using the private key.
///     #[ender(with: rsa(public_key, private_key))]
///     even_more_secret_data: Vec<u8>,
/// }
/// mod person_encoder {
/// #    use ender::io::{Write, Read};
/// #    use ender::{Encoder, EncodingResult, Encode};
/// #
/// #     pub struct Person {
/// #        name: String,
/// #        surname: String,
/// #        age: u32,
/// #     }
/// #
///      pub fn encode<T: Write>(person: &Person, encoder: &mut Encoder<T>) -> EncodingResult<()> {
///          /* ... */
/// #        person.name.encode(encoder)?;
/// #        person.surname.encode(encoder)?;
/// #        person.age.encode(encoder)?;
/// #        Ok(())
///      }
/// #
///      pub fn decode<T: Read>(encoder: &mut Encoder<T>) -> EncodingResult<Person> {
///          /* ... */
/// #        Ok(Person {
/// #            name: encoder.decode_value()?,
/// #            surname: encoder.decode_value()?,
/// #            age: encoder.decode_value()?,
/// #   	})
///      }
/// }
/// ```
/// # 4. Type Modifiers
/// Type-Modifier flags change the type of the value that's encoded, and change it back after
/// decoding it.
/// All of these currently block `decode_in_place` optimizations in generated code.<br>
/// * `as: $ty` - Converts the value of the field to `$ty` before encoding it
/// and back to the original field type after decoding it.<br>
/// The conversion is done through the `as` keyword.
/// * `into: $ty` - Converts the value of the field to `$ty` before encoding it
/// and back to the original field type after decoding it.<br>
/// The conversion is done through the `Into` trait.
/// * `from: $ty` - Converts the value of the field to `$ty` before encoding it
/// and back to the original field type after decoding it.<br>
/// The conversion is done through the `From` trait.
/// ### Example:
/// ```rust
/// # use ender::{Encode, Decode};
/// #
/// #[derive(Encode, Decode)]
/// # #[ender(crate: ender)]
/// struct Mountain {
///     /// Height is encoded as a `u16`, then decoded back to a `f64`.
///     /// These operations are performed using the `as` keyword.
///     #[ender(as: u16)]
///     height: f64,
///     /// Boulder is encoded as a `BigRock`, then decoded back to a `Boulder`.
///     /// This can be done because `BigRock` implements `From<Boulder>`, and
///     /// `Boulder` implements `From<BigRock>`.
///     #[ender(into: BigRock)]
///     boulder: Boulder,
/// }
///
/// /// Note: `BigRock` is required to implement `Encode` and `Decode`,
/// /// but `Boulder` is not.
/// #[derive(Encode, Decode)]
/// # #[ender(crate: ender)]
/// struct BigRock {
///     weight: f64
/// }
///
/// /* From<Boulder> and From<BigRock> impls here... */
/// # impl From<Boulder> for BigRock {
/// #    fn from(value: Boulder) -> Self {
/// #   	Self {
/// #            weight: value.weight
/// #   	}
/// #    }
/// # }
/// #
/// # #[derive(Clone)]
/// # struct Boulder {
/// #    weight: f64,
/// #    radius: f64
/// # }
/// #
/// # impl From<BigRock> for Boulder {
/// #    fn from(value: BigRock) -> Self {
/// #   	Self {
/// #   		weight: value.weight,
/// #           radius: 1.0
/// #   	}
/// #    }
/// # }
/// ```
/// # 5. Helpers
/// Helper flags change certain parameters or add conditions for when a field
/// or item should be encoded/decoded.<br>
/// * `crate: $crate` - Overwrites the default crate name which is assumed to be `ender`.
/// Can only be applied to items.
/// * `if: $expr` - The field will only be encoded/decoded if the given expression
/// evaluates to true, otherwise the default value is computed. In a `decode_in place`
/// implementation, if the expression evaluates to false the value is left untouched.
/// * `default: $expr` - Overrides the default fallback for when a value can't be
/// decoded, which is `Default::default()`
/// * `skip` - Will not encode/decode this field.
/// When decoding, computes the default value. In a `decode_in place` implementation the value is
/// left untouched.
/// * `validate: $expr, $format_string, $arg1, $arg2, $arg3, ...` - Before encoding/after decoding, returns an error if the
/// expression evaluates to false. The error message will use the given formatting (if present).
/// * `flatten: $expr` - Indicates that the length of the given field (for example
/// a Vec or HashMap) doesn't need to be encoded/decoded, because it is known from the context.
/// Can also be used with an `Option` in conjunction with the `if` flag and without the `$expr`
/// to indicate that the presence of an optional value is known from the context.
/// * `borrow: $lif1, $lif2, $lif3, ...` - This is a `borrow` flag. Indicates this field
/// should be decoded using its borrowing decode implementation, and allows you to optionally specify a
/// set of lifetimes to override those normally inferred by the macro. These lifetimes will be bound
/// to the lifetime of the encoder's data.
/// * `goto $seek: $expr` - This is a `seek` flag. Indicates a jump to a different stream position
/// before encoding this field or item.
/// $seek can be any of "start", "end" or "cur", while $expr must produce a value of
/// type usize or isize relative to $seek.<br>
/// If you need the stream position to be restored after encoding/decoding the field, see the
/// `ptr` *stream modifier`.
/// * `pos_tracker: $ident` - This is a `seek` flag. Stores the current stream position in a
/// variable with the given name.
/// Note that the position is stored *before* the `ptr` and `goto` flags, if any.
/// * `pull $temp as $ty: $var <= $expr` - Attempts to retrieve the `user` field from the context and
/// downcast it to `$ty`, early returning an error if it fails, and assigns it to the temporary variable `$temp`.
/// The `$expr` is executed and its value stored in a local variable `$val`.
/// This is useful for reading data from the context so that it is later available to other attributes.
/// This flag is applied *before* `push`
/// * `push $temp as $ty: $expr` - Attempts to retrieve the `user` field from the context and
/// downcast it to `$ty`, early returning an error if it fails, and assigns it to the temporary variable `$temp`.
/// The `$expr` is executed and its value ignored.
/// This is useful for writing data to the context.
/// * `seeking` - This is a `seek` flag. Does nothing, but simply forces a seeking impl to be used.
/// This can only be applied to the whole item, as it doesn't make sense on individual fields.
/// <br>
/// ### Example:
///
/// ```rust
/// # use std::borrow::Cow;
/// # use ender::{Encode, Decode};
/// # use uuid::Uuid;
/// /// Hehe >:3
/// extern crate ender as enderman;
///
/// #[derive(Encode, Decode)]
/// /// We specify the name of the re-exported ender crate.
/// #[ender(crate: enderman)]
/// /// We specify this should use a seeking impl
/// /// This is redundant of course, but we include it for completeness :P
/// #[ender(seeking)]
/// struct PersonEntry<'record> {
///   /// Will come in handy later
///   name_present: bool,
///   /// Similar to the previous example, but with the addition of the flatten flag!
///   /// We know a Uuid is always 16 bytes long, so we omit writing/reading that data.
///   #[ender(serde; flatten size: 16)]
///   uuid: Uuid,
///   /// Just the string version of the uuid, not present in the binary data.
///   /// Skip the Encoding step, and Decode it from the uuid.
///   #[ender(skip; default: uuid.to_string())]
///   uuid_string: String,
///   /// We know whether this is present from the context, therefore we don't write whether
///   /// the optional value is present, and when reading we assume it is.
///   /// Since the "if" flag is also present, the field will only be decoded if the expression
///   /// evaluates to true, making the previous operation safe
///   /// (no risk of decoding garbage data)
///   #[ender(flatten bool: true; if: * name_present)]
///   name: Option<String>,
///   /// Contains a file offset to the rest of the data
///   pointer_to_data: usize,
///   /// Go to the location in the specified file offset from this point onwards.
///   ///
///   /// This might be too long to clone from the decoder, so we borrow it instead.
///   /// Decode impl -> Cow::Owned -- (NOT YET - WILL WORK WHEN SPECIALIZATION IS STABILIZED)
///   /// BorrowDecode impl -> Cow::Borrowed
///   /// The macro will infer the borrow lifetime to be `'record`.
///   #[ender(goto start: *pointer_to_data; borrow)]
///   criminal_record: Cow<'record, str>,
///   /// Only present if the name is also present, but we want to provide a custom default!
///   #[ender(default: String::from("Smith"); if: * name_present)]
///   surname: String,
///   /// No-one allowed before 18!
///   #[ender(validate: * age >= 18, "User is too young: {}", age)]
///   age: u32,
///   /// This is temporary data, we don't care about including it in the binary format.
///   #[ender(skip; default: 100)]
///   health: u64,
/// }
/// ```
/// # Relationship between seek flags
///
/// ```
/// # use ender::{Decode, Encode};
/// #[derive(Encode, Decode)]
/// # #[ender(crate: ender)]
/// /// This is the same...
/// struct Ptr {
///     pointer: usize,
///     #[ender(ptr start: *pointer)]
///     data: /* ... */
/// #   (),
/// }
///
/// #[derive(Encode, Decode)]
/// # #[ender(crate: ender)]
/// /// As this!
/// struct Goto {
///     pointer: usize,
///     #[ender(pos_tracker: current)]
///     #[ender(goto start: *pointer)]
///     data: /* ... */
/// #   (),
///     #[ender(goto start: current)]
///     seek_back: (),
/// }
/// ```
#[cfg(feature = "derive")]
#[cfg_attr(feature = "unstable", doc(cfg(feature = "derive")))]
pub use ender_derive::{Decode, Encode};
pub use error::*;
pub use opaque::*;
pub use convenience::*;

use crate::io::{BorrowRead, Read, Seek, SeekFrom, SizeLimit, SizeTrack, Write, Zero};

#[cfg(test)]
mod test;

mod error;
pub mod facade;
mod impls;
pub mod io;
mod opaque;
#[cfg(feature = "serde")]
mod serde;
mod source;
mod windows1252;
mod convenience;

/// Controls the endianness of a numerical value. Endianness is just
/// the order in which the value's bytes are written.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, Display)]
pub enum Endianness {
    /// Least significant byte first
    LittleEndian,
    /// Most significant byte first
    #[default]
    BigEndian,
}

impl Endianness {
    /// Returns the native endianness for the target system.
    #[inline]
    pub const fn native() -> Self {
        #[cfg(target_endian = "little")]
        {
            Self::LittleEndian
        }
        #[cfg(target_endian = "big")]
        {
            Self::BigEndian
        }
    }
}

/// Controls the encoding of a numerical value. For instance, controls whether the numbers
/// are compressed through a var-int format or if the entire length of their value is encoded.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, Display)]
#[non_exhaustive]
pub enum NumEncoding {
    /// The value's bits are encoded as-is according to the [`Endianness`].
    #[default]
    Fixed,
    /// The value's bits are encoded according to the [ULEB128](https://en.wikipedia.org/wiki/LEB128#Unsigned_LEB128)
    /// (Little Endian Base 128) standard if unsigned, or [LEB128](https://en.wikipedia.org/wiki/LEB128#Signed_LEB128)
    /// standard if signed.<br>As the name suggests, the bytes are encoded in little endian order,
    /// ignoring the [`Endianness`].
    Leb128,
    /// The value's bits are encoded according to
    /// [Protobuf's varint encoding](https://protobuf.dev/programming-guides/encoding/),
    /// where unsigned values are encoded in the same way as [Leb128][`NumEncoding::Leb128`],
    /// and signed values are encoded as a reinterpret-cast of the bits to unsigned,
    /// possibly wasting all the var-int length to encode the leading 1s.<br>
    /// This encoding method is not ideal to encode negative numbers and is provided merely for
    /// compatibility concerns.<br>
    /// The bytes are encoded in little endian order, ignoring the [`Endianness`].
    ProtobufWasteful,
    /// The value's bits are encoded according to
    /// [Protobuf's varint encoding](https://protobuf.dev/programming-guides/encoding/),
    /// where unsigned values are encoded in the same way as [Leb128][`NumEncoding::Leb128`],
    /// and signed values are encoded as an unsigned value with its least significant bit
    /// carrying the sign.<br>
    /// The bytes are encoded in little endian order, ignoring the [`Endianness`].
    ProtobufZigzag,
}

impl NumEncoding {
    /// Determines whether a slice encoded with this encoding can be directly borrowed.
    ///
    /// Currently only returns true with the [`Fixed`][`NumEncoding::Fixed`] variant
    #[inline]
    pub const fn borrowable(&self) -> bool {
        match self {
            NumEncoding::Fixed => true,
            _ => false,
        }
    }
}

/// How many bits a size or enum variant will occupy in the binary format. If the value
/// contains more bits, they will be trimmed (lost), so change this value with care
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Debug, Default, Display)]
pub enum BitWidth {
    /// Max 8 bits per value
    #[display("8Bit")]
    Bit8,
    /// Max 16 bits per value
    #[display("16Bit")]
    Bit16,
    /// Max 32 bits per value
    #[display("32Bit")]
    Bit32,
    /// Max 64 bits per value
    #[display("64Bit")]
    #[default]
    Bit64,
    /// Max 128 bits per value
    #[display("128Bit")]
    Bit128,
}

impl BitWidth {
    /// Returns the native bit-width of the [`usize`] and [`isize`] types for the target system.
    ///
    /// [`usize`]: prim@usize
    /// [`isize`]: prim@isize
    #[inline]
    pub const fn native() -> Self {
        #[cfg(target_pointer_width = "64")]
        {
            Self::Bit64
        }
        #[cfg(target_pointer_width = "32")]
        {
            Self::Bit32
        }
        #[cfg(target_pointer_width = "16")]
        {
            Self::Bit16
        }
    }

    /// Returns the number of bits represented by this bit-width.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ender::BitWidth;
    /// let sixteen = BitWidth::Bit16;
    ///
    /// assert_eq!(sixteen.bits(), 16);
    /// ```
    #[inline]
    pub const fn bits(&self) -> usize {
        match self {
            BitWidth::Bit8 => 8,
            BitWidth::Bit16 => 16,
            BitWidth::Bit32 => 32,
            BitWidth::Bit64 => 64,
            BitWidth::Bit128 => 128,
        }
    }

    /// Returns the number of bytes represented by this bit-width.
    ///
    /// # Example
    ///
    /// ```rust
    /// use ender::BitWidth;
    /// let eight_bits = BitWidth::Bit8;
    /// let thirtytwo_bits = BitWidth::Bit32;
    ///
    /// assert_eq!(eight_bits.bytes(), 1);
    /// assert_eq!(thirtytwo_bits.bytes(), 4);
    /// ```
    #[inline]
    pub const fn bytes(&self) -> usize {
        match self {
            BitWidth::Bit8 => 1,
            BitWidth::Bit16 => 2,
            BitWidth::Bit32 => 4,
            BitWidth::Bit64 => 8,
            BitWidth::Bit128 => 16,
        }
    }
}

/// The encoding method use for the length of a string.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, Display)]
pub enum StrLen {
    /// The length of the string is stored before its contents as an `usize`.
    #[default]
    LengthPrefixed,
    /// The length of the string is obtained by finding the first occurrence of `n`
    /// number of null bytes, where `n` is the length in bytes of one code unit for
    /// the given string encoding (see [`StrEncoding::bytes`]).
    NullTerminated,
    /// Like [`StrLen::NullTerminated`], but the string always occupies `n` bytes,
    /// where the last bytes are filled with null-bytes if the length of the string after
    /// being encoded is less than `n` bytes.
    #[display("NullTerminatedFixed({0})")]
    NullTerminatedFixed(usize),
}

/// The encoding method used for strings and chars.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, Display)]
#[non_exhaustive]
pub enum StrEncoding {
    Ascii,
    /// See [UTF-8](https://en.wikipedia.org/wiki/UTF-8)
    #[default]
    Utf8,
    /// See [UTF-16](https://en.wikipedia.org/wiki/UTF-16)
    Utf16,
    /// See [UTF-32](https://en.wikipedia.org/wiki/UTF-32)
    Utf32,
    Windows1252,
}

impl StrEncoding {
    /// Returns the number of bytes of each **code unit** for this encoding.
    #[inline]
    pub const fn bytes(&self) -> usize {
        match self {
            StrEncoding::Ascii => 1,
            StrEncoding::Utf8 => 1,
            StrEncoding::Utf16 => 2,
            StrEncoding::Utf32 => 4,
            StrEncoding::Windows1252 => 1,
        }
    }
}

/// Controls the binary representation of numbers (different from sizes and enum variants).
/// Specifically, controls the [`Endianness`] and [`NumEncoding`].
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[display("endianness = {endianness}, encoding = {num_encoding}")]
pub struct NumRepr {
    pub endianness: Endianness,
    pub num_encoding: NumEncoding,
}

impl NumRepr {
    /// Returns the default numerical representation: little endian with fixed encoding
    #[inline]
    pub const fn new() -> Self {
        Self {
            endianness: Endianness::LittleEndian,
            num_encoding: NumEncoding::Fixed,
        }
    }

    /// Sets the **endianness**, then returns self.
    #[inline]
    pub const fn endianness(mut self, endiannes: Endianness) -> Self {
        self.endianness = endiannes;
        self
    }

    /// Sets the **numerical encoding**, then returns self.
    #[inline]
    pub const fn num_encoding(mut self, num_encoding: NumEncoding) -> Self {
        self.num_encoding = num_encoding;
        self
    }
}

impl Default for NumRepr {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// Controls the binary representation of sizes.
/// Specifically, controls the [`Endianness`], the [`NumEncoding`], the [`BitWidth`],
/// and the greatest encodable/decodable size before an error is thrown
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[display("endianness = {endianness} , encoding = {num_encoding}, bit_width = {width}, max_size = {max_size}")]
pub struct SizeRepr {
    pub endianness: Endianness,
    pub num_encoding: NumEncoding,
    pub width: BitWidth,
    pub max_size: usize,
}

impl SizeRepr {
    /// Returns the default size representation: little endian, fixed encoding, 64 bit width,
    /// and the max size set to `usize::MAX`
    #[inline]
    pub const fn new() -> Self {
        Self {
            endianness: Endianness::LittleEndian,
            num_encoding: NumEncoding::Fixed,
            width: BitWidth::Bit64,
            max_size: usize::MAX,
        }
    }

    /// Sets the **endianness**, then returns self.
    #[inline]
    pub const fn endianness(mut self, endiannes: Endianness) -> Self {
        self.endianness = endiannes;
        self
    }

    /// Sets the **numerical encoding**, then returns self.
    #[inline]
    pub const fn num_encoding(mut self, num_encoding: NumEncoding) -> Self {
        self.num_encoding = num_encoding;
        self
    }

    /// Sets the **bit width**, then returns self.
    #[inline]
    pub const fn bit_width(mut self, bit_width: BitWidth) -> Self {
        self.width = bit_width;
        self
    }

    /// Sets the **max size**, then returns self.
    #[inline]
    pub const fn max_size(mut self, max_size: usize) -> Self {
        self.max_size = max_size;
        self
    }
}

impl Default for SizeRepr {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// Controls the binary representation of enum variants.
/// Specifically, controls the [`Endianness`], the [`NumEncoding`], and the [`BitWidth`].
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[display("endianness = {endianness} , encoding = {num_encoding}, bit_width = {width}")]
pub struct VariantRepr {
    pub endianness: Endianness,
    pub num_encoding: NumEncoding,
    pub width: BitWidth,
}

impl VariantRepr {
    /// Returns the default variant representation: little endian, fixed encoding and 32 bit width
    #[inline]
    pub const fn new() -> Self {
        Self {
            endianness: Endianness::LittleEndian,
            num_encoding: NumEncoding::Fixed,
            width: BitWidth::Bit32,
        }
    }

    /// Sets the **endianness**, then returns self.
    #[inline]
    pub const fn endianness(mut self, endiannes: Endianness) -> Self {
        self.endianness = endiannes;
        self
    }

    /// Sets the **numerical encoding**, then returns self.
    #[inline]
    pub const fn num_encoding(mut self, num_encoding: NumEncoding) -> Self {
        self.num_encoding = num_encoding;
        self
    }

    /// Sets the **bit width**, then returns self.
    #[inline]
    pub const fn bit_width(mut self, bit_width: BitWidth) -> Self {
        self.width = bit_width;
        self
    }
}

impl Default for VariantRepr {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// Controls the binary representation of strings.
/// Specifically, controls the [`StrEncoding`] of strings and chars and the [`Endianness`]
/// in which the encoded bytes are ordered.
///
/// Keep in mind not all encodings support null terminated strings, because
/// the encoding format may have the capability to contain nulls.<br>
/// In such cases, the encoding process will produce an error in case the encoded string contains
/// null characters, and the end of the string is encoded as a sequence of nulls of the appropriate
/// length (1 byte for UTF-8 and ASCII, 2 bytes for UTF-16, 4 bytes for UTF-32)
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[display("encoding = {encoding}, endianness = {endianness}, len = {len}")]
pub struct StringRepr {
    pub encoding: StrEncoding,
    pub endianness: Endianness,
    pub len: StrLen,
}

impl StringRepr {
    /// Returns the default string representation: utf-8, length-prefixed, little_endian
    #[inline]
    pub const fn new() -> Self {
        Self {
            encoding: StrEncoding::Utf8,
            endianness: Endianness::LittleEndian,
            len: StrLen::LengthPrefixed,
        }
    }

    /// Sets the **string encoding**, then returns self.
    #[inline]
    pub const fn str_encoding(mut self, str_encoding: StrEncoding) -> Self {
        self.encoding = str_encoding;
        self
    }
    
    /// Sets the **endianness**, then returns self.
    #[inline]
    pub const fn endianness(mut self, endiannes: Endianness) -> Self {
        self.endianness = endiannes;
        self
    }

    /// Sets the **string length encoding**, then returns self.
    #[inline]
    pub const fn len_encoding(mut self, len_encoding: StrLen) -> Self {
        self.len = len_encoding;
        self
    }
}

impl Default for StringRepr {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// An aggregation of [`NumRepr`], [`SizeRepr`], [`VariantRepr`], [`StringRepr`]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[display("num_repr = ({num_repr}), size_repr = ({size_repr}), variant_repr = ({variant_repr}), string_repr = ({string_repr})")]
pub struct BinSettings {
    pub num_repr: NumRepr,
    pub size_repr: SizeRepr,
    pub variant_repr: VariantRepr,
    pub string_repr: StringRepr,
}

impl BinSettings {
    /// Returns the default options containing the default for each representation.
    /// See: [`NumRepr::new`], [`SizeRepr::new`], [`VariantRepr::new`], [`StringRepr::new`]
    #[inline]
    pub const fn new() -> Self {
        Self {
            num_repr: NumRepr::new(),
            size_repr: SizeRepr::new(),
            variant_repr: VariantRepr::new(),
            string_repr: StringRepr::new(),
        }
    }

    /// Sets the **number** representation settings, then returns self.
    #[inline]
    pub const fn num_repr(mut self, num_repr: NumRepr) -> Self {
        self.num_repr = num_repr;
        self
    }

    /// Sets the **size** representation settings, then returns self.
    #[inline]
    pub const fn size_repr(mut self, size_repr: SizeRepr) -> Self {
        self.size_repr = size_repr;
        self
    }

    /// Sets the **variant** representation settings, then returns self.
    #[inline]
    pub const fn variant_repr(mut self, variant_repr: VariantRepr) -> Self {
        self.variant_repr = variant_repr;
        self
    }

    /// Sets the **string** representation settings, then returns self.
    #[inline]
    pub const fn string_repr(mut self, string_repr: StringRepr) -> Self {
        self.string_repr = string_repr;
        self
    }
}

impl Default for BinSettings {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// The state of the encoder, including its options and a `flatten` state variable
#[derive(Copy, Clone, Debug, Default)]
#[non_exhaustive]
pub struct Context<'a> {
    /// User provided data. This can be purposed for storing any kind of data,
    /// like cryptographic keys that are unknown to the data structures but known
    /// at a higher level.
    pub user: Option<&'a dyn Any>,
    /// The actual settings, which determine the numerical representations and the string
    /// representations.
    ///
    /// Implementations of [`Encode`] and [`Decode`] are required to
    /// preserve the state of the settings, even though they are allowed to temporarily modify it.
    ///
    /// In case of an error occurring, no guarantee is made about the state of the settings:
    /// for this reason it's good practice to store a copy of the settings somewhere.
    pub settings: BinSettings,
    /// The `bool` flatten state variable.
    ///
    /// When present, for `Option`, `Result` and any `bool` it indicates,
    /// while **Encoding** not to write the value,
    /// and while **Decoding** it contains the boolean value itself
    /// (it won't be read from the stream).
    pub bool_flatten: Option<bool>,
    /// The Variant flatten state variable.
    ///
    /// When present, for any enum it indicates,
    /// while **Encoding** not to write the value of the discriminant,
    /// and while **Decoding** it contains the discriminant value itself
    /// (it won't be read from the stream).
    pub variant_flatten: Option<Opaque>,
    /// The `usize` flatten state variable.
    ///
    /// When present, for `Vec`, `HashMap` and other data
    /// structures with a length it indicates while **Encoding** not to write said length,
    /// and while **Decoding** it contains the length itself
    /// (it won't be read from the stream).
    pub size_flatten: Option<usize>,
}

impl<'a> Context<'a> {
    /// Constructs the default encoder state. Options will be set to default, flatten to None.
    #[inline]
    pub const fn new() -> Self {
        Self {
            user: None,
            settings: BinSettings::new(),
            bool_flatten: None,
            variant_flatten: None,
            size_flatten: None,
        }
    }

    /// Replaces the settings with `settings`, then returns self.
    #[inline]
    pub const fn settings(mut self, settings: BinSettings) -> Self {
        self.settings = settings;
        self
    }

    /// Replaces the user data with `data`, then returns self.
    #[inline]
    pub const fn user_data<'b>(self, data: &'b dyn Any) -> Context<'b> {
        let this = Context {
            user: Some(data),
            settings: self.settings,
            bool_flatten: self.bool_flatten,
            variant_flatten: self.variant_flatten,
            size_flatten: self.size_flatten,
        };
        this
    }

    /// Replaces the bool flatten state variable with `Some(value)`, then returns self.
    #[inline]
    pub const fn bool_flatten(mut self, value: bool) -> Self {
        self.bool_flatten = Some(value);
        self
    }

    /// Replaces the variant flatten state variable with `Some(value)`, then returns self.
    /// 
    /// **Beware of providing an [`Opaque`] with an integer literal without a specific type!**
    /// 
    /// By default, rust will infer `i32` as the type, thus it will be converted to a *Signed*
    /// enum variant value, and you will get a (bad) surprise when you try to then encode/decode
    /// an enum that uses *Unsigned* variant discriminants (most enums).
    /// 
    /// How to avoid this?
    /// - Write the type in the num literal E.G. `Opaque::from(3u32)` or `Opaque::from(3 as u32)`
    /// - Better yet, use an explicit opaque value ([`Opaque::signed`], [`Opaque::unsigned`])
    #[inline]
    pub const fn variant_flatten(mut self, value: Opaque) -> Self
    {
        self.variant_flatten = Some(value);
        self
    }

    /// Replaces the size flatten state variable with `Some(value)`, then returns self.
    #[inline]
    pub const fn size_flatten(mut self, value: usize) -> Self {
        self.size_flatten = Some(value);
        self
    }
    
    /// Just like [`Self::new`] but uses the given settings instead of the default.
    #[inline]
    pub fn with_settings(settings: BinSettings) -> Self {
        Self {
            user: None,
            settings,
            bool_flatten: None,
            variant_flatten: None,
            size_flatten: None,
        }
    }

    /// Just like [`Self::new`] but uses the given settings instead of the default
    /// and the given user data.
    #[inline]
    pub fn with_user_data(settings: BinSettings, data: &'a dyn Any) -> Self {
        Self {
            user: Some(data),
            settings,
            bool_flatten: None,
            variant_flatten: None,
            size_flatten: None,
        }
    }

    /// Resets the context to its defaults, then overwrites the options with the given options.
    #[inline]
    pub fn reset(&mut self, options: BinSettings) {
        self.settings = options;
        self.bool_flatten = None;
        self.variant_flatten = None;
        self.size_flatten = None;
    }

    /// Returns the state of the [`bool`] flatten variable, consuming it.
    #[inline]
    pub fn consume_bool_flatten(&mut self) -> Option<bool> {
        replace(&mut self.bool_flatten, None)
    }

    /// Returns the state of the Variant flatten variable, consuming it.
    #[inline]
    pub fn consume_variant_flatten(&mut self) -> Option<Opaque> {
        replace(&mut self.variant_flatten, None)
    }

    /// Returns the state of the [`usize`] flatten variable, consuming it.
    #[inline]
    pub fn consume_size_flatten(&mut self) -> Option<usize> {
        replace(&mut self.size_flatten, None)
    }
}

/// The base type for encoding/decoding. Wraps a stream, and a [`Context`].<br>
/// It's recommended to wrap the stream in a [`std::io::BufReader`] or [`std::io::BufWriter`],
/// because many small write and read calls will be made.
#[derive(Clone)]
#[non_exhaustive]
pub struct Encoder<'a, T> {
    /// The underlying stream
    pub stream: T,
    /// The state
    pub ctxt: Context<'a>,
    /// The stack
    #[cfg(feature = "debug")]
    pub stack: source::Stack,
}

macro_rules! debug_fn {
    ($fn_name:ident, $variant_name:ident ( $ty:ty )) => {
        #[inline]
        pub fn $fn_name<F, R>(&mut self, f: F, s: $ty) -> EncodingResult<R>
        where
            F: FnOnce(&mut Encoder<T>) -> EncodingResult<R>,
        {
            #[cfg(feature = "debug")]
            {
                #[cfg(feature = "alloc")]
                {
                    self.stack.frames.push(source::Frame::$variant_name(s));
                    let r = f(self)?;
                    self.stack.frames.pop();
                    Ok(r)
                }
                #[cfg(not(feature = "alloc"))]
                {
                    let last_frame = self.stack.last_frame;
                    self.stack.last_frame = source::Frame::$variant_name(s);
                    let r = f(self)?;
                    self.stack.last_frame = last_frame;
                    Ok(r)
                }
            }
            #[cfg(not(feature = "debug"))]
            {
                let _ = s;
                f(self)
            }
        }
    };
}

impl<'a, T> Encoder<'a, T> {
    /// Wraps the given stream and state.
    #[inline]
    pub fn new(stream: T, ctxt: Context<'a>) -> Self {
        Self {
            stream,
            ctxt,
            #[cfg(feature = "debug")]
            stack: source::Stack::new(),
        }
    }

    /// Replaces the underlying stream with the new one, returning the previous value
    #[inline]
    pub fn swap_stream(&mut self, new: T) -> T {
        replace(&mut self.stream, new)
    }

    /// Retrieves the user data and attempts to cast it to the given concrete type,
    /// returning a validation error in case the types don't match or no user data is stored.
    #[inline]
    pub fn user_data<U: Any>(&self) -> EncodingResult<&U> {
        self.ctxt
            .user
            .ok_or(val_error!("User-data requested, but none is present"))
            .and_then(|x| {
                x.downcast_ref().ok_or(val_error!(
                    "User-data doesnt match the requested concrete type of {}",
                    core::any::type_name::<U>()
                ))
            })
    }

    debug_fn!(with_item, Item(&'static str));
    debug_fn!(with_variant, Variant(&'static str));
    debug_fn!(with_field, Field(&'static str));
    debug_fn!(with_index, Index(usize));
}

impl<T: Write> Encoder<'_, T> {
    /// Method for convenience.
    ///
    /// Encodes a value using `self` as the encoder.
    ///
    /// This method is not magic - it is literally defined as `value.encode(self)`
    #[inline]
    pub fn encode_value<V: Encode<T>>(&mut self, value: V) -> EncodingResult<()> {
        value.encode(self)
    }
}

impl<T: Read> Encoder<'_, T> {
    /// Method for convenience.
    ///
    /// Decodes a value using `self` as the decoder.
    ///
    /// This method is not magic - it is literally defined as `V::decode(self)`
    #[inline]
    pub fn decode_value<V: Decode<T>>(&mut self) -> EncodingResult<V> {
        V::decode(self)
    }
}

impl<'a, T> Encoder<'a, T> {
    #[inline]
    pub fn finish(self) -> (T, Context<'a>) {
        (self.stream, self.ctxt)
    }
}

macro_rules! make_write_fns {
    (
	    type $uty:ty {
		    pub u_write: $u_write:ident,
		    pub u_write_direct: $u_write_direct:ident,
		    priv uleb128_encode: $uleb128_encode:ident
		    $(,)?
	    },
	    type $ity:ty {
		    pub i_write: $i_write:ident,
		    pub i_write_direct: $i_write_direct:ident,
		    priv leb128_encode: $leb128_encode:ident
		    $(,)?
	    }$(,)?
    ) => {
        #[doc = "Encodes a `"]
	    #[doc = stringify!($uty)]
	    #[doc = "` to the underlying stream using ULEB-128 encoding"]
        #[inline]
	    fn $uleb128_encode(&mut self, value: $uty) -> EncodingResult<()> {
		    let mut shifted = value;
	        let mut more = true;
	        while more {
		        let mut byte: u8 = shifted as u8 & 0b01111111;
		        shifted >>= 7;

		        // Is the next shifted value worth writing?
		        if shifted != 0 {
			        byte |= 0b10000000;
		        } else {
			        more = false;
		        }
		        self.write_byte(byte)?;
			}
		    Ok(())
	    }

	    #[doc = "Encodes a `"]
	    #[doc = stringify!($uty)]
	    #[doc = "` to the underlying stream, according to the endianness and numerical encoding in the encoder's state"]
        #[inline]
	    pub fn $u_write(&mut self, value: $uty) -> EncodingResult<()> {
		    self.$u_write_direct(value, self.ctxt.settings.num_repr.num_encoding, self.ctxt.settings.num_repr.endianness)
	    }

        #[doc = "Encodes a `"]
	    #[doc = stringify!($uty)]
	    #[doc = "` to the underlying stream, according to the endianness and numerical passed as parameters"]
        #[inline]
        pub fn $u_write_direct(&mut self, value: $uty, num_encoding: NumEncoding, endianness: Endianness) -> EncodingResult<()> {
	        match num_encoding {
		        NumEncoding::Fixed => {
			        let bytes: [u8; core::mem::size_of::<$uty>()] = match endianness {
			            Endianness::BigEndian => value.to_be_bytes(),
			            Endianness::LittleEndian => value.to_le_bytes()
		            };
		            self.stream.write(&bytes)?;
		        },
		        NumEncoding::Leb128 | NumEncoding::ProtobufWasteful | NumEncoding::ProtobufZigzag => {
			        self.$uleb128_encode(value)?;
		        }
	        }
            Ok(())
        }

        #[doc = "Encodes a `"]
	    #[doc = stringify!($ity)]
	    #[doc = "` to the underlying stream using LEB-128 encoding"]
        #[inline]
	    fn $leb128_encode(&mut self, value: $ity) -> EncodingResult<()> {
		        let mut shifted = value;
		        let mut more = true;
		        while more {
			        let mut byte = shifted as u8 & 0b0111_1111;
			        shifted >>= 7;

			        // Is the next shifted value worth writing?
			        let neg = (byte & 0b0100_0000) != 0;
			        if (neg && shifted != -1) || (!neg && shifted != 0) {
				        byte |= 0b1000_0000;
			        } else {
				        more = false;
			        }
			        self.write_byte(byte)?;
				}
		        Ok(())
	        }

	    #[doc = "Encodes a `"]
	    #[doc = stringify!($ity)]
	    #[doc = "` to the underlying stream, according to the endianness and numerical encoding in the encoder's state"]
        #[inline]
	    pub fn $i_write(&mut self, value: $ity) -> EncodingResult<()> {
		    self.$i_write_direct(value, self.ctxt.settings.num_repr.num_encoding, self.ctxt.settings.num_repr.endianness)
	    }

	    #[doc = "Encodes a `"]
	    #[doc = stringify!($ity)]
	    #[doc = "` to the underlying stream, according to the endianness and numerical encoding passed as parameters"]
        #[inline]
        pub fn $i_write_direct(&mut self, value: $ity, num_encoding: NumEncoding, endianness: Endianness) -> EncodingResult<()> {
		    match num_encoding {
		        NumEncoding::Fixed => {
			        let bytes: [u8; core::mem::size_of::<$ity>()] = match endianness {
			            Endianness::BigEndian => value.to_be_bytes(),
			            Endianness::LittleEndian => value.to_le_bytes()
		            };
		            self.stream.write(&bytes)?;
		        },
		        NumEncoding::Leb128 => {
			        self.$leb128_encode(value)?;
		        },
		        NumEncoding::ProtobufWasteful => {
			        let unsigned = <$uty>::from_ne_bytes(value.to_ne_bytes());
			        self.$uleb128_encode(unsigned)?;
		        }
			    NumEncoding::ProtobufZigzag => {
			        let shifted = (value << 1) ^ (value >> (<$ity>::BITS - 1));
			        let unsigned = <$uty>::from_ne_bytes(shifted.to_ne_bytes());
			        self.$uleb128_encode(unsigned)?;
		        }
	        }
            Ok(())
        }
    };
}

impl<T: Write> Encoder<'_, T> {
    make_write_fns! {
        type u8 {
            pub u_write: write_u8,
            pub u_write_direct: write_u8_with,
            priv uleb128_encode: uleb128_encode_u8,
        },
        type i8 {
            pub i_write: write_i8,
            pub i_write_direct: write_i8_with,
            priv leb128_encode: leb128_encode_i8,
        },
    }
    make_write_fns! {
        type u16 {
            pub u_write: write_u16,
            pub u_write_direct: write_u16_with,
            priv uleb128_encode: uleb128_encode_u16,
        },
        type i16 {
            pub i_write: write_i16,
            pub i_write_direct: write_i16_with,
            priv leb128_encode: leb128_encode_i16,
        },
    }
    make_write_fns! {
        type u32 {
            pub u_write: write_u32,
            pub u_write_direct: write_u32_with,
            priv uleb128_encode: uleb128_encode_u32,
        },
        type i32 {
            pub i_write: write_i32,
            pub i_write_direct: write_i32_with,
            priv leb128_encode: leb128_encode_i32,
        },
    }
    make_write_fns! {
        type u64 {
            pub u_write: write_u64,
            pub u_write_direct: write_u64_with,
            priv uleb128_encode: uleb128_encode_u64,
        },
        type i64 {
            pub i_write: write_i64,
            pub i_write_direct: write_i64_with,
            priv leb128_encode: leb128_encode_i64,
        },
    }
    make_write_fns! {
        type u128 {
            pub u_write: write_u128,
            pub u_write_direct: write_u128_with,
            priv uleb128_encode: uleb128_encode_u128,
        },
        type i128 {
            pub i_write: write_i128,
            pub i_write_direct: write_i128_with,
            priv leb128_encode: leb128_encode_i128,
        },
    }

    /// Encodes an `usize`.
    ///
    /// If the size flatten variable is set to `Some`,
    /// this function checks that the value matches but then returns
    /// immediately without writing, otherwise it will encode the given `usize`
    /// to the underlying stream according to the endianness, numerical
    /// encoding and bit-width in the encoder's state, with an additional
    /// check that the value does not exceed the max size.
    #[inline]
    pub fn write_usize(&mut self, value: usize) -> EncodingResult<()> {
        if let Some(size) = self.ctxt.consume_size_flatten() {
            if size != value {
                return Err(EncodingError::FlattenError(FlattenError::LenMismatch {
                    expected: size,
                    got: value,
                }));
            }
            Ok(())
        } else {
            if value > self.ctxt.settings.size_repr.max_size {
                return Err(EncodingError::MaxSizeExceeded {
                    max: self.ctxt.settings.size_repr.max_size,
                    requested: value,
                });
            }
            let encoding = self.ctxt.settings.size_repr.num_encoding;
            let endianness = self.ctxt.settings.size_repr.endianness;

            // `Opaque` already converts conversion errors to `EncodingError`
            let opaque = Opaque::from(value);

            match self.ctxt.settings.size_repr.width {
                BitWidth::Bit8 => self.write_u8_with(opaque.try_into()?, encoding, endianness),
                BitWidth::Bit16 => self.write_u16_with(opaque.try_into()?, encoding, endianness),
                BitWidth::Bit32 => self.write_u32_with(opaque.try_into()?, encoding, endianness),
                BitWidth::Bit64 => self.write_u64_with(opaque.try_into()?, encoding, endianness),
                BitWidth::Bit128 => self.write_u128_with(opaque.try_into()?, encoding, endianness),
            }
        }
    }

    /// Encodes a `isize` to the underlying stream, according to the endianness,
    /// numerical encoding and bit-width in the encoder's state.
    #[inline]
    pub fn write_isize(&mut self, value: isize) -> EncodingResult<()> {
        let encoding = self.ctxt.settings.size_repr.num_encoding;
        let endianness = self.ctxt.settings.size_repr.endianness;

        // `Opaque` already converts conversion errors to `EncodingError`
        let opaque = Opaque::from(value);

        match self.ctxt.settings.size_repr.width {
            BitWidth::Bit8 => self.write_i8_with(opaque.try_into()?, encoding, endianness),
            BitWidth::Bit16 => self.write_i16_with(opaque.try_into()?, encoding, endianness),
            BitWidth::Bit32 => self.write_i32_with(opaque.try_into()?, encoding, endianness),
            BitWidth::Bit64 => self.write_i64_with(opaque.try_into()?, encoding, endianness),
            BitWidth::Bit128 => self.write_i128_with(opaque.try_into()?, encoding, endianness),
        }
    }

    /// Encodes an unsigned `Variant`.
    ///
    /// If the `Variant` flatten variable is set to `Some`,
    /// this function checks that the value matches but then returns
    /// immediately without writing, otherwise it will encode the given `Variant`
    /// to the underlying stream according to the endianness, numerical
    /// encoding and bit-width in the encoder's state.
    #[inline]
    #[allow(private_bounds)]
    pub fn write_uvariant<V>(&mut self, value: V) -> EncodingResult<()>
    where
        Opaque: From<V>,
        V: Sign<Sign = Unsigned>,
    {
        let value = Opaque::from(value);
        if let Some(variant) = self.ctxt.consume_variant_flatten() {
            if value != variant {
                return Err(FlattenError::VariantMismatch {
                    expected: variant,
                    got: value,
                }
                .into());
            }
            Ok(())
        } else {
            let width = self.ctxt.settings.variant_repr.width;
            let encoding = self.ctxt.settings.variant_repr.num_encoding;
            let endianness = self.ctxt.settings.variant_repr.endianness;
            match width {
                BitWidth::Bit8 => self.write_u8_with(value.try_into()?, encoding, endianness),
                BitWidth::Bit16 => self.write_u16_with(value.try_into()?, encoding, endianness),
                BitWidth::Bit32 => self.write_u32_with(value.try_into()?, encoding, endianness),
                BitWidth::Bit64 => self.write_u64_with(value.try_into()?, encoding, endianness),
                BitWidth::Bit128 => self.write_u128_with(value.try_into()?, encoding, endianness),
            }
        }
    }

    /// Encodes a signed `Variant`.
    ///
    /// If the `Variant` flatten variable is set to `Some`,
    /// this function checks that the value matches but then returns
    /// immediately without writing, otherwise it will encode the given `Variant`
    /// to the underlying stream according to the endianness, numerical
    /// encoding and bit-width in the encoder's state.
    #[inline]
    #[allow(private_bounds)]
    pub fn write_ivariant<V>(&mut self, value: V) -> EncodingResult<()>
    where
        Opaque: From<V>,
        V: Sign<Sign = Signed>,
    {
        let value = Opaque::from(value);
        if let Some(variant) = self.ctxt.consume_variant_flatten() {
            if value != variant {
                return Err(FlattenError::VariantMismatch {
                    expected: variant,
                    got: value,
                }
                .into());
            }
            Ok(())
        } else {
            let width = self.ctxt.settings.variant_repr.width;
            let encoding = self.ctxt.settings.variant_repr.num_encoding;
            let endianness = self.ctxt.settings.variant_repr.endianness;
            match width {
                BitWidth::Bit8 => self.write_i8_with(value.try_into()?, encoding, endianness),
                BitWidth::Bit16 => self.write_i16_with(value.try_into()?, encoding, endianness),
                BitWidth::Bit32 => self.write_i32_with(value.try_into()?, encoding, endianness),
                BitWidth::Bit64 => self.write_i64_with(value.try_into()?, encoding, endianness),
                BitWidth::Bit128 => self.write_i128_with(value.try_into()?, encoding, endianness),
            }
        }
    }

    /// Encodes a boolean value.
    ///
    /// It is guaranteed that, if `value` is `true`, a single u8 will be written to the
    /// underlying stream with the value `1`, and if `value` is `false`, with a value of `0`.
    ///
    /// If the `bool` flatten variable is set to `Some`,
    /// this function checks that the value matches but then returns
    /// immediately without writing, otherwise it will encode the given `bool`
    /// as described above.
    #[inline]
    pub fn write_bool(&mut self, value: bool) -> EncodingResult<()> {
        if let Some(boolean) = self.ctxt.consume_bool_flatten() {
            if boolean != value {
                return Err(FlattenError::BoolMismatch {
                    expected: boolean,
                    got: value,
                }
                .into());
            }
            Ok(())
        } else {
            self.write_byte(if value { 1 } else { 0 })
        }
    }

    /// Encodes a `char` to the underlying stream, according to the endianness and string encoding
    /// in the encoder's state.
    #[inline]
    pub fn write_char(&mut self, value: char) -> EncodingResult<()> {
        self.write_char_with(
            value,
            self.ctxt.settings.string_repr.encoding,
            self.ctxt.settings.string_repr.endianness
        )
    }

    /// Encodes a `char` to the underlying stream, according to the endianness and string encoding
    /// passed as parameters.
    #[inline]
    pub fn write_char_with(&mut self, value: char, encoding: StrEncoding, endianness: Endianness) -> EncodingResult<()> {
            match encoding {
                StrEncoding::Ascii => {
                    if !value.is_ascii() {
                        return Err(StringError::InvalidChar.into());
                    }

                    self.write_byte(value as u8)?;
                }
                StrEncoding::Utf8 => {
                    let mut buf = [0u8; 4];
                    let len = value.encode_utf8(&mut buf).len();

                    self.write_bytes(&buf[..len])?;
                }
                StrEncoding::Utf16 => {
                    let mut buf = [0u16; 2];
                    let len = value.encode_utf16(&mut buf).len();

                    for block in buf[..len].iter() {
                        self.write_u16_with(*block, NumEncoding::Fixed, endianness)?;
                    }
                }
                StrEncoding::Utf32 => {
                    self.write_u32_with(value as u32, NumEncoding::Fixed, endianness)?;
                }
                StrEncoding::Windows1252 => {
                    let encoded = windows1252::dec_to_enc(value);
                    if let Some(encoded) = encoded {
                        self.write_byte(encoded)?;
                    } else {
                        return Err(StringError::InvalidChar.into());
                    }
                }
            }
        Ok(())
    }

    /// Encodes a `f32` to the underlying stream, ignoring the numeric encoding but respecting
    /// the endianness. Equivalent of `Self::write_u32(value.to_bits())` with the numeric
    /// encoding set to Fixed
    #[inline]
    pub fn write_f32(&mut self, value: f32) -> EncodingResult<()> {
        self.write_u32_with(
            value.to_bits(),
            NumEncoding::Fixed,
            self.ctxt.settings.num_repr.endianness,
        )
    }

    /// Encodes a `f64` to the underlying stream, ignoring the numeric encoding but respecting
    /// the endianness. Equivalent of `Self::write_u64(value.to_bits())` with the numeric
    /// encoding set to Fixed
    #[inline]
    pub fn write_f64(&mut self, value: f64) -> EncodingResult<()> {
        self.write_u64_with(
            value.to_bits(),
            NumEncoding::Fixed,
            self.ctxt.settings.num_repr.endianness,
        )
    }

    /// Encodes a string to the underlying stream, according to the endianness,
    /// string encoding and string-length encoding in the encoder's state.
    /// Anything whose chars can be iterated over is considered a string.
    ///
    /// # Example
    ///
    /// ```
    /// # use ender::{Context, Encoder};
    /// # use ender::io::Zero;
    ///
    /// let mut encoder = Encoder::new(Zero, Context::new());
    /// encoder.write_str("Hello, world!".chars()).unwrap();
    /// ```
    #[inline]
    pub fn write_str<S>(&mut self, string: S) -> EncodingResult<()>
    where
        S: IntoIterator<Item = char, IntoIter: Clone>,
    {
        self.write_str_with(
            string,
            self.ctxt.settings.string_repr.encoding,
            self.ctxt.settings.string_repr.endianness,
            self.ctxt.settings.string_repr.len
        )
    }

    /// Encodes a string to the underlying stream, according to the endianness,
    /// string encoding and string-length encoding passed as parameters.
    /// Anything whose chars can be iterated over is considered a string.
    ///
    /// # Example
    ///
    /// ```
    /// # use ender::{Context, Encoder, Endianness, StrEncoding, StrLen};
    /// # use ender::io::Zero;
    ///
    /// let mut encoder = Encoder::new(Zero, Context::new());
    /// encoder.write_str_with(
    ///     "Goodbye, world :(".chars(),
    ///     StrEncoding::Utf16,
    ///     Endianness::BigEndian,
    ///     StrLen::NullTerminated
    /// ).unwrap();
    /// ```
    pub fn write_str_with<S>(&mut self, string: S, encoding: StrEncoding, endianness: Endianness, len_encoding: StrLen) -> EncodingResult<()>
    where
        S: IntoIterator<Item = char, IntoIter: Clone>,
    {
        let chars = string.into_iter();

        match len_encoding {
            StrLen::LengthPrefixed => {
                // We don't know the length of the string in advance

                // Create a fake encoder that simply keeps track of the length
                let mut sz_encoder = Encoder::new(SizeTrack::new(Zero), self.ctxt);
                for ch in chars.clone() {
                    sz_encoder.write_char_with(ch, encoding, endianness)?;
                }
                let size = sz_encoder.finish().0.size_written();

                // Now encode the length and the string data
                self.write_usize(size)?;
                for ch in chars {
                    self.write_char_with(ch, encoding, endianness)?;
                }
            }
            StrLen::NullTerminated => {
                for ch in chars {
                    self.write_char_with(ch, encoding, endianness)?;
                }
                self.write_char_with('\0', encoding, endianness)?;
            }
            StrLen::NullTerminatedFixed(max) => {
                let mut capped = Encoder::new(SizeLimit::new(&mut self.stream, max, 0), self.ctxt);
                for ch in chars {
                    match capped.write_char_with(ch, encoding, endianness) {
                        Err(EncodingError::UnexpectedEnd) => {
                            Err(EncodingError::StringError(StringError::TooLong))
                        }
                        any => any,
                    }?;
                }

                // Fill the rest with zeroes
                for _ in 0..capped.stream.remaining_writable() {
                    self.write_byte(0)?;
                }
            }
        }

        Ok(())
    }

    /// Writes a single byte to the underlying stream as-is.
    #[inline]
    pub fn write_byte(&mut self, byte: u8) -> EncodingResult<()> {
        self.stream.write(&[byte])
    }

    /// Writes the given slice to the underlying stream as-is.
    #[inline]
    pub fn write_bytes(&mut self, bytes: &[u8]) -> EncodingResult<()> {
        self.stream.write(bytes)
    }
}

macro_rules! make_read_fns {
    (
	    type $uty:ty {
		    pub u_read: $u_read:ident,
		    pub u_read_direct: $u_read_direct:ident,
		    priv uleb128_decode: $uleb128_decode:ident
		    $(,)?
	    },
	    type $ity:ty {
		    pub i_read: $i_read:ident,
		    pub i_read_direct: $i_read_direct:ident,
		    priv leb128_decode: $leb128_decode:ident
		    $(,)?
	    }
	    $(,)?
    ) => {
        #[doc = "Decodes a `"]
	    #[doc = stringify!($uty)]
	    #[doc = "` from the underlying stream using ULEB-128 decoding"]
        #[inline]
	    fn $uleb128_decode(&mut self) -> EncodingResult<$uty> {
			    let mut result: $uty = 0;
		        let mut shift: u8 = 0;
		        loop {
			        if shift >= <$uty>::BITS as u8 {
				        return Err(EncodingError::VarIntError);
			        }

		            let byte = self.read_byte()?;
			        result |= (byte & 0b0111_1111) as $uty << shift;
			        shift += 7;

			        if (byte & 0b1000_0000) == 0 {
				        break;
			        }
				}
		        Ok(result)
		    }

	    #[doc = "Decodes a `"]
	    #[doc = stringify!($uty)]
	    #[doc = "` from the underlying stream, according to the endianness and numerical encoding in the encoder's state"]
        #[inline]
	    pub fn $u_read(&mut self) -> EncodingResult<$uty> {
		    self.$u_read_direct(self.ctxt.settings.num_repr.num_encoding, self.ctxt.settings.num_repr.endianness)
	    }

	    #[doc = "Decodes a `"]
	    #[doc = stringify!($uty)]
	    #[doc = "` from the underlying stream, according to the endianness and numerical passed as parameters"]
        #[inline]
        pub fn $u_read_direct(&mut self, num_encoding: NumEncoding, endianness: Endianness) -> EncodingResult<$uty> {
		    Ok(match num_encoding {
		        NumEncoding::Fixed => {
			        let mut bytes: [u8; core::mem::size_of::<$uty>()] = [0u8; core::mem::size_of::<$uty>()];
		            self.stream.read(&mut bytes)?;

		            match endianness {
			            Endianness::BigEndian => <$uty>::from_be_bytes(bytes),
			            Endianness::LittleEndian => <$uty>::from_le_bytes(bytes)
		            }
		        }
		        NumEncoding::Leb128 | NumEncoding::ProtobufWasteful | NumEncoding::ProtobufZigzag => {
			        self.$uleb128_decode()?
		        }
	        })
        }

        #[doc = "Decodes a `"]
	    #[doc = stringify!($ity)]
	    #[doc = "` from the underlying stream using LEB-128 decoding"]
        #[inline]
        fn $leb128_decode(&mut self) -> EncodingResult<$ity> {
            let mut result: $ity = 0;
            let mut byte;
            let mut shift: u8 = 0;
            loop {
                if shift >= <$ity>::BITS as u8 {
                    return Err(EncodingError::VarIntError);
                }

                byte = self.read_byte()?;
                result |= (byte & 0b0111_1111) as $ity << shift;
                shift += 7;

                if (byte & 0b1000_0000) == 0 {
                    break;
                }
            }

            if shift < <$ity>::BITS as u8 && (byte & 0b0100_0000) != 0 {
                result |= (!0 << shift);
            }

            Ok(result)
        }

	    #[doc = "Decodes a `"]
	    #[doc = stringify!($ity)]
	    #[doc = "` from the underlying stream, according to the endianness and numerical encoding in the encoder's context"]
        #[inline]
	    pub fn $i_read(&mut self) -> EncodingResult<$ity> {
		    self.$i_read_direct(self.ctxt.settings.num_repr.num_encoding, self.ctxt.settings.num_repr.endianness)
	    }

	    #[doc = "Decodes a `"]
	    #[doc = stringify!($ity)]
	    #[doc = "` from the underlying stream, according to the endianness and numerical encoding passed as parameters"]
        #[inline]
        pub fn $i_read_direct(&mut self, num_encoding: NumEncoding, endianness: Endianness) -> EncodingResult<$ity> {
	        Ok(match num_encoding {
		        NumEncoding::Fixed => {
			        let mut bytes: [u8; core::mem::size_of::<$ity>()] = [0u8; core::mem::size_of::<$ity>()];
		            self.stream.read(&mut bytes)?;

		            match endianness {
			            Endianness::BigEndian => <$ity>::from_be_bytes(bytes),
			            Endianness::LittleEndian => <$ity>::from_le_bytes(bytes)
		            }
		        }
		        NumEncoding::Leb128 => {
			        self.$leb128_decode()?
		        }
		        NumEncoding::ProtobufWasteful => {
			        let unsigned = self.$uleb128_decode()?;
			        <$ity>::from_ne_bytes(unsigned.to_ne_bytes())
		        }
		        NumEncoding::ProtobufZigzag => {
			        let unsigned = self.$uleb128_decode()?;
			        let neg = (unsigned & 1) != 0;
			        let transformed = if neg {
				        !(unsigned >> 1)
			        } else {
				        unsigned >> 1
			        };

			        <$ity>::from_ne_bytes(transformed.to_ne_bytes())
		        }
	        })
        }
    };
}

impl<'user, T: Read> Encoder<'user, T> {
    make_read_fns! {
        type u8 {
            pub u_read: read_u8,
            pub u_read_direct: read_u8_with,
            priv uleb128_decode: uleb128_decode_u8,
        },
        type i8 {
            pub i_read: read_i8,
            pub i_read_direct: read_i8_with,
            priv leb128_decode: leb128_decode_i8,
        },
    }
    make_read_fns! {
        type u16 {
            pub u_read: read_u16,
            pub u_read_direct: read_u16_with,
            priv uleb128_decode: uleb128_decode_u16,
        },
        type i16 {
            pub i_read: read_i16,
            pub i_read_direct: read_i16_with,
            priv leb128_decode: leb128_decode_i16,
        },
    }
    make_read_fns! {
        type u32 {
            pub u_read: read_u32,
            pub u_read_direct: read_u32_with,
            priv uleb128_decode: uleb128_decode_u32,
        },
        type i32 {
            pub i_read: read_i32,
            pub i_read_direct: read_i32_with,
            priv leb128_decode: leb128_decode_i32,
        },
    }
    make_read_fns! {
        type u64 {
            pub u_read: read_u64,
            pub u_read_direct: read_u64_with,
            priv uleb128_decode: uleb128_decode_u64,
        },
        type i64 {
            pub i_read: read_i64,
            pub i_read_direct: read_i64_with,
            priv leb128_decode: leb128_decode_i64,
        },
    }
    make_read_fns! {
        type u128 {
            pub u_read: read_u128,
            pub u_read_direct: read_u128_with,
            priv uleb128_decode: uleb128_decode_u128,
        },
        type i128 {
            pub i_read: read_i128,
            pub i_read_direct: read_i128_with,
            priv leb128_decode: leb128_decode_i128,
        },
    }

    /// Decodes an `usize`.
    ///
    /// If the `usize` flatten variable is set to `Some`, this function
    /// will return its value without reading, otherwise it will decode an `usize`
    /// from the underlying stream, according to the endianness, numerical encoding
    /// and bit-width in the encoder's state, with an additional check that the size
    /// does not exceed the max size.
    #[inline]
    pub fn read_usize(&mut self) -> EncodingResult<usize> {
        if let Some(size) = self.ctxt.consume_size_flatten() {
            Ok(size)
        } else {
            let encoding = self.ctxt.settings.size_repr.num_encoding;
            let endianness = self.ctxt.settings.size_repr.endianness;
            let value = match self.ctxt.settings.size_repr.width {
                BitWidth::Bit8 => Opaque::from(self.read_u8_with(encoding, endianness)?),
                BitWidth::Bit16 => Opaque::from(self.read_u16_with(encoding, endianness)?),
                BitWidth::Bit32 => Opaque::from(self.read_u32_with(encoding, endianness)?),
                BitWidth::Bit64 => Opaque::from(self.read_u64_with(encoding, endianness)?),
                BitWidth::Bit128 => Opaque::from(self.read_u128_with(encoding, endianness)?),
            }
            .try_into()?;

            if value > self.ctxt.settings.size_repr.max_size {
                return Err(EncodingError::MaxSizeExceeded {
                    max: self.ctxt.settings.size_repr.max_size,
                    requested: value,
                });
            }
            Ok(value)
        }
    }

    /// Decodes a `isize` from the underlying stream, according to the endianness,
    /// numerical encoding and bit-width in the encoder's state
    #[inline]
    pub fn read_isize(&mut self) -> EncodingResult<isize> {
        let encoding = self.ctxt.settings.size_repr.num_encoding;
        let endianness = self.ctxt.settings.size_repr.endianness;
        match self.ctxt.settings.size_repr.width {
            BitWidth::Bit8 => Opaque::from(self.read_i8_with(encoding, endianness)?),
            BitWidth::Bit16 => Opaque::from(self.read_i16_with(encoding, endianness)?),
            BitWidth::Bit32 => Opaque::from(self.read_i32_with(encoding, endianness)?),
            BitWidth::Bit64 => Opaque::from(self.read_i64_with(encoding, endianness)?),
            BitWidth::Bit128 => Opaque::from(self.read_i128_with(encoding, endianness)?),
        }
        .try_into()
    }

    /// Decodes an unsigned `Variant`.
    ///
    /// If the `Variant` flatten variable is set to `Some`, this function
    /// will return its value without reading, otherwise it will decode a `Variant`
    /// from the underlying stream, according to the endianness, numerical encoding
    /// and bit-width in the encoder's state.
    #[inline]
    #[allow(private_bounds)]
    pub fn read_uvariant<V>(&mut self) -> EncodingResult<V>
    where
        V: Sign<Sign = Unsigned>,
        Opaque: TryInto<V, Error = EncodingError>,
    {
        if let Some(variant) = self.ctxt.consume_variant_flatten() {
            variant.try_into()
        } else {
            let width = self.ctxt.settings.variant_repr.width;
            let encoding = self.ctxt.settings.variant_repr.num_encoding;
            let endianness = self.ctxt.settings.variant_repr.endianness;

            match width {
                BitWidth::Bit8 => Opaque::from(self.read_u8_with(encoding, endianness)?),
                BitWidth::Bit16 => Opaque::from(self.read_u16_with(encoding, endianness)?),
                BitWidth::Bit32 => Opaque::from(self.read_u32_with(encoding, endianness)?),
                BitWidth::Bit64 => Opaque::from(self.read_u64_with(encoding, endianness)?),
                BitWidth::Bit128 => Opaque::from(self.read_u128_with(encoding, endianness)?),
            }
            .try_into()
        }
    }

    /// Decodes a signed `Variant`.
    ///
    /// If the `Variant` flatten variable is set to `Some`, this function
    /// will return its value without reading, otherwise it will decode a `Variant`
    /// from the underlying stream, according to the endianness, numerical encoding
    /// and bit-width in the encoder's state.
    #[inline]
    #[allow(private_bounds)]
    pub fn read_ivariant<V>(&mut self) -> EncodingResult<V>
    where
        V: Sign<Sign = Signed>,
        Opaque: TryInto<V, Error = EncodingError>,
    {
        if let Some(variant) = self.ctxt.consume_variant_flatten() {
            variant.try_into()
        } else {
            let width = self.ctxt.settings.variant_repr.width;
            let encoding = self.ctxt.settings.variant_repr.num_encoding;
            let endianness = self.ctxt.settings.variant_repr.endianness;

            match width {
                BitWidth::Bit8 => Opaque::from(self.read_i8_with(encoding, endianness)?),
                BitWidth::Bit16 => Opaque::from(self.read_i16_with(encoding, endianness)?),
                BitWidth::Bit32 => Opaque::from(self.read_i32_with(encoding, endianness)?),
                BitWidth::Bit64 => Opaque::from(self.read_i64_with(encoding, endianness)?),
                BitWidth::Bit128 => Opaque::from(self.read_i128_with(encoding, endianness)?),
            }
            .try_into()
        }
    }

    /// Decodes a boolean value.
    ///
    /// It is guaranteed that, one `u8` is read from the underlying stream and, if
    /// it's equal to `1`, `true` is returned, if it's equal to `0`, `false` is returned,
    /// for any other value an [`InvalidBool`][`EncodingError::InvalidBool`]
    /// error will be returned.
    ///
    /// If the `bool` flatten variable is set to `Some`,
    /// then its value is returned without reading,
    /// otherwise the boolean is decoded as described above.
    #[inline]
    pub fn read_bool(&mut self) -> EncodingResult<bool> {
        if let Some(boolean) = self.ctxt.consume_bool_flatten() {
            Ok(boolean)
        } else {
            match self.read_byte()? {
                0 => Ok(false),
                1 => Ok(true),
                _ => Err(EncodingError::InvalidBool),
            }
        }
    }

    /// Decodes a `char` from the underlying stream, according to the endianness and string encoding
    /// in the encoder's state.
    #[inline]
    pub fn read_char(&mut self) -> EncodingResult<char> {
        self.read_char_with(
            self.ctxt.settings.string_repr.encoding,
            self.ctxt.settings.string_repr.endianness
        )
    }

    /// Decodes a `char` from the underlying stream, according to the endianness and string encoding
    /// passed as parameters.
    #[inline]
    fn read_char_with(&mut self, encoding: StrEncoding, endianness: Endianness) -> EncodingResult<char> {
        match encoding {
            StrEncoding::Ascii => {
                let ch = self.read_byte()?;
                if !ch.is_ascii() {
                    return Err(StringError::InvalidChar.into());
                }
                let ch = char::from_u32(ch as u32);

                // PANIC SAFETY
                //
                // We check that the character is ascii before converting it to a char
                // and if it is, we return *BEFORE* converting it, so `ch` is ALWAYS
                // Some at this point
                Ok(ch.unwrap())
            }
            StrEncoding::Utf8 => {
                // See https://en.wikipedia.org/wiki/UTF-8#Encoding
                let mut buf = self.read_byte()?;
                let (add, rshift) = if buf.leading_ones() == 0 {
                    (0usize, 1u32)
                } else {
                    let leading = buf.leading_ones();
                    if leading == 1 || leading > 4 {
                        // The first byte was either a continuation byte
                        // or forward declared more than 3 continuation bytes
                        return Err(StringError::InvalidChar.into());
                    }
                    (leading as usize - 1, leading + 1)
                };

                let mut ch: u32 = ((u8::MAX >> rshift) & buf) as u32;

                let mut shift = 0;
                for _ in 0..add {
                    buf = self.read_byte()?;

                    if buf.leading_ones() != 1 {
                        // This byte was not a continuation byte, but we expected it to be
                        return Err(StringError::InvalidChar.into());
                    }

                    shift += 6;
                    ch = (ch << shift) | ((buf & 0b0011_1111) as u32);
                }

                Ok(char::from_u32(ch).ok_or(
                    EncodingError::StringError(StringError::InvalidChar),
                )?)
            }
            StrEncoding::Utf16 => {
                // See https://en.wikipedia.org/wiki/UTF-16#Code_points_from_U+010000_to_U+10FFFF
                let buf = self.read_u16_with(NumEncoding::Fixed, endianness)?;
                let ch;

                // This is a high surrogate
                if 0xD800 <= buf && buf <= 0xDBFF {
                    let high_surrogate = buf;
                    let low_surrogate = self.read_u16_with(NumEncoding::Fixed, endianness)?;

                    if !(0xDC00 <= low_surrogate && low_surrogate <= 0xDFFF) {
                        // First character was in the high surrogate range,
                        // but the second character wasn't in the low surrogate range
                        return Err(StringError::InvalidChar.into());
                    }

                    const LOW_TEN_BITS: u16 = 0b0000_0011_1111_1111;

                    let high_bits = ((high_surrogate - 0xD800) & LOW_TEN_BITS) as u32;
                    let low_bits = ((low_surrogate - 0xDC00) & LOW_TEN_BITS) as u32;

                    ch = (high_bits << 10) | low_bits;
                } else if 0xDC00 <= buf && buf <= 0xDFFF {
                    // First character was in the low surrogate range
                    return Err(StringError::InvalidChar.into());
                } else {
                    ch = buf as u32;
                }

                Ok(char::from_u32(ch).ok_or(
                    EncodingError::StringError(StringError::InvalidChar),
                )?)
            }
            StrEncoding::Utf32 => {
                let buf = self.read_u32_with(NumEncoding::Fixed, endianness)?;

                Ok(char::from_u32(buf).ok_or(
                    EncodingError::StringError(StringError::InvalidChar),
                )?)
            }
            StrEncoding::Windows1252 => {
                let buf = self.read_byte()?;

                if let Some(ch) = windows1252::enc_to_dec(buf) {
                    Ok(ch)
                } else {
                    Err(EncodingError::StringError(StringError::InvalidChar))
                }
            }
        }
    }

    /// Decodes a `f32` from the underlying stream, ignoring the numeric encoding but respecting
    /// the endianness. Equivalent of `f32::from_bits(self.read_u32())` with the numeric
    /// encoding set to [`NumEncoding::Fixed`].
    #[inline]
    pub fn read_f32(&mut self) -> EncodingResult<f32> {
        Ok(f32::from_bits(self.read_u32_with(
            NumEncoding::Fixed,
            self.ctxt.settings.num_repr.endianness,
        )?))
    }

    /// Decodes a `f64` from the underlying stream, ignoring the numeric encoding but respecting
    /// the endianness. Equivalent of `f64::from_bits(self.read_u64())` with the numeric
    /// encoding set to [`NumEncoding::Fixed`].
    #[inline]
    pub fn read_f64(&mut self) -> EncodingResult<f64> {
        Ok(f64::from_bits(self.read_u64_with(
            NumEncoding::Fixed,
            self.ctxt.settings.num_repr.endianness,
        )?))
    }

    /// Decodes a String from the underlying stream, according to the endianness,
    /// and string encoding in the encoder's state.
    #[inline]
    pub fn read_str<S>(&mut self) -> EncodingResult<S>
    where
        S: FromIterator<char>,
    {
        self.read_str_iter()?.into_iter().collect()
    }

    /// Decodes a String from the underlying stream, according to the endianness,
    /// and string encoding passed as parameters.
    #[inline]
    pub fn read_str_with<S>(&mut self, encoding: StrEncoding, endianness: Endianness, len_encoding: StrLen) -> EncodingResult<S>
    where
        S: FromIterator<char>
    {
        self.read_str_iter_with(encoding, endianness, len_encoding)?.into_iter().collect()
    }

    /// Returns an iterator which can be used to decode a String from the underlying stream.
    /// The iterator acts according to the endianness, and string encoding in the encoder's state.
    ///
    /// Even if the iterator is not used, this method may read some data from the stream to determine
    /// the length of the string.
    ///
    /// If the iterator returns `None` or `Some(Err)`, calling `next` again will most likely lead to
    /// logic errors, and it should simply be dropped instead.
    ///
    /// Dropping the iterator before you reach the end or an error is returned will leave the stream
    /// in a broken state, and successive reads will see the unread parts of the string.
    #[inline]
    pub fn read_str_iter<'a>(&'a mut self) -> EncodingResult<impl IntoIterator<Item = EncodingResult<char>> + use<'a, 'user, T>>
    {
        self.read_str_iter_with(
            self.ctxt.settings.string_repr.encoding,
            self.ctxt.settings.string_repr.endianness,
            self.ctxt.settings.string_repr.len
        )
    }

    /// Returns an iterator which can be used to decode a String from the underlying stream.
    /// The iterator acts according to the endianness, and string encoding passed as parameters.
    ///
    /// Even if the iterator is not used, this method may read some data from the stream to determine
    /// the length of the string.
    ///
    /// If the iterator returns `None` or `Some(Err)`, calling `next` again will most likely lead to
    /// logic errors, and it should simply be dropped instead.
    ///
    /// Dropping the iterator before you reach the end or an error is returned will leave the stream
    /// in a broken state, and successive reads will see the unread parts of the string.
    #[inline]
    pub fn read_str_iter_with<'a>(&'a mut self, encoding: StrEncoding, endianness: Endianness, len_encoding: StrLen) -> EncodingResult<impl IntoIterator<Item = EncodingResult<char>> + use<'a, 'user, T>>
    {
        struct LenPrefixCharIter<'iter, 'user, T: Read> {
            encoder: Encoder<'user, SizeLimit<&'iter mut T>>,
            encoding: StrEncoding,
            endianness: Endianness,
        }

        impl<T: Read> Iterator for LenPrefixCharIter<'_, '_, T> {
            type Item = EncodingResult<char>;
            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                if self.encoder.stream.remaining_readable() == 0 {
                    // We expect 0 more bytes
                    // This means we can end the iterator by returning None
                    return None;
                };
                
                Some(self.encoder.read_char_with(self.encoding, self.endianness))
            }

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                (0, Some(self.encoder.stream.remaining_readable()))
            }
        }

        struct NullTermCharIter<'iter, 'user, T: Read> {
            encoder: &'iter mut Encoder<'user, T>,
            encoding: StrEncoding,
            endianness: Endianness,
        }

        impl<T: Read> Iterator for NullTermCharIter<'_, '_, T> {
            type Item = EncodingResult<char>;

            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                match self.encoder.read_char_with(self.encoding, self.endianness) {
                    Ok('\0') => {
                        // Null character found
                        None // => STOP!
                    }
                    Ok(x) => {
                        // Just a char
                        Some(Ok(x)) // ==> Continue
                    }
                    Err(x) => {
                        // An unrelated error occurred
                        Some(Err(x)) // ==> "Continue" but actually early return an error
                    }
                }
            }
        }

        struct NullTermFixedLengthCharIter<'iter, 'user, T: Read> {
            encoder: Encoder<'user, SizeLimit<&'iter mut T>>,
            encoding: StrEncoding,
            endianness: Endianness
        }

        impl<T: Read> Iterator for NullTermFixedLengthCharIter<'_, '_, T> {
            type Item = EncodingResult<char>;

            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                if self.encoder.stream.remaining_readable() == 0 {
                    // We reached the maximum amount of bytes we can read
                    // Simply end the iterator
                    return None;
                }
                match self.encoder.read_char_with(self.encoding, self.endianness) {
                    Ok('\0') => {
                        // Null character found
                        // Read all the remaining nulls
                        for _ in 0..self.encoder.stream.remaining_readable() {
                            let z = match self.encoder.read_byte() {
                                Ok(z) => z,
                                Err(err) => return Some(Err(err)),
                            };
                            if z != 0 {
                                return Some(Err(EncodingError::StringError(
                                    StringError::MissingNull,
                                )));
                            }
                        }

                        None // => STOP!
                    }
                    Ok(x) => {
                        // Just a char
                        Some(Ok(x)) // ==> Continue
                    }
                    Err(x) => {
                        // An unrelated error occurred
                        Some(Err(x)) // ==> "Continue" but actually early return an error
                    }
                }
            }

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                (0, Some(self.encoder.stream.remaining_readable()))
            }
        }

        enum IteratorDecider<'iter, 'user, T: Read> {
            LenPrefix(LenPrefixCharIter<'iter, 'user, T>),
            NullTerm(NullTermCharIter<'iter, 'user, T>),
            NullTermFixedLength(NullTermFixedLengthCharIter<'iter, 'user, T>)
        }

        impl<T: Read> Iterator for IteratorDecider<'_, '_, T> {
            type Item = EncodingResult<char>;

            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                match self {
                    IteratorDecider::LenPrefix(x) => x.next(),
                    IteratorDecider::NullTerm(x) => x.next(),
                    IteratorDecider::NullTermFixedLength(x) => x.next(),
                }
            }

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                match self {
                    IteratorDecider::LenPrefix(x) => x.size_hint(),
                    IteratorDecider::NullTerm(x) => x.size_hint(),
                    IteratorDecider::NullTermFixedLength(x) => x.size_hint(),
                }
            }
        }

        match len_encoding {
            StrLen::LengthPrefixed => {
                let length = self.read_usize()?;
                let iter = LenPrefixCharIter {
                    encoder: Encoder::new(SizeLimit::new(&mut self.stream, 0, length), self.ctxt),
                    encoding,
                    endianness
                };

                Ok(IteratorDecider::LenPrefix(iter))
            }
            StrLen::NullTerminated => {
                let iter = NullTermCharIter {
                    encoder: self,
                    encoding,
                    endianness
                };
                Ok(IteratorDecider::NullTerm(iter))
            }
            StrLen::NullTerminatedFixed(max) => {
                let iter = NullTermFixedLengthCharIter {
                    encoder: Encoder::new(SizeLimit::new(&mut self.stream, 0, max), self.ctxt),
                    encoding,
                    endianness
                };
                Ok(IteratorDecider::NullTermFixedLength(iter))
            }
        }
    }

    /// Reads a single byte from the stream.
    #[inline]
    pub fn read_byte(&mut self) -> EncodingResult<u8> {
        let mut buf = [0u8; 1];
        self.stream.read(&mut buf)?;
        Ok(buf[0])
    }

    /// Reads `buf.len()` bytes from the stream to the buffer as-is.
    #[inline]
    pub fn read_bytes(&mut self, buf: &mut [u8]) -> EncodingResult<()> {
        self.stream.read(buf)
    }
}

macro_rules! make_borrow_slice_fn {
    ($name:ident -> $ty:ty) => {
        #[doc = "Borrows a `"]
        #[doc = stringify!($ty)]
        #[doc = "` slice of `length` length from the encoder, checking"]
        #[doc = "that the [`Endianness`] and alignment match those of the system"]
        #[doc = "and that the [`NumEncoding`] is [`borrowable`][`NumEncoding::borrowable`]"]
        #[inline]
        pub fn $name(
            &mut self,
            length: usize,
            num_encoding: NumEncoding,
            endianness: Endianness,
        ) -> EncodingResult<&'data [$ty]> {
            // Assert the num encoding is borrowable
            if !num_encoding.borrowable() {
                return Err(EncodingError::BorrowError(
                    BorrowError::NonBorrowableNumEncoding { num_encoding },
                ));
            }

            // Assert the endianness matches, else we would be borrowing garbage-looking data.
            if endianness != Endianness::native() {
                return Err(EncodingError::BorrowError(
                    BorrowError::EndiannessMismatch {
                        found: endianness,
                        system: Endianness::native(),
                    },
                ));
            }

            const BYTES: usize = core::mem::size_of::<$ty>();

            let u8_slice: &[u8] = self.stream.borrow_read(length * BYTES)?;

            // Depending on the alignment of the target system, this might fail.
            let conv: &[$ty] = bytemuck::try_cast_slice(u8_slice)
                .map_err(|_| EncodingError::BorrowError(BorrowError::AlignmentMismatch))?;
            Ok(conv)
        }
    };
}

impl<'data, T: BorrowRead<'data>> Encoder<'_, T> {
    /// Returns a reference to `len` bytes in the stream, without advancing it.
    ///
    /// This forwards the call to [`BorrowRead::peek`], meaning multiple calls
    /// produce the same output.
    ///
    /// # Example
    ///
    /// ```
    /// use ender::{Context, Encoder};
    /// use ender::io::Slice;
    ///
    /// let slice = [0, 7, 15, 42, 2];
    /// let encoder = Encoder::new(Slice::new(&slice), Context::new());
    ///
    /// let first_peek = encoder.peek_bytes(3).unwrap();
    /// let second_peek = encoder.peek_bytes(3).unwrap();
    ///
    /// assert_eq!(first_peek, second_peek);
    /// ```
    #[inline]
    pub fn peek_bytes(&self, len: usize) -> EncodingResult<&'data [u8]> {
        self.stream.peek(len)
    }

    /// Borrows a `u8` slice of length `length` from the encoder,
    /// without performing any additional checks.
    #[inline]
    pub fn borrow_byte_slice(&mut self, len: usize) -> EncodingResult<&'data [u8]> {
        self.stream.borrow_read(len)
    }

    /// Borrows a `u8` slice of length `length` from the encoder,
    /// checking that the [`NumEncoding`] is [`borrowable`][`NumEncoding::borrowable`].
    #[inline]
    pub fn borrow_u8_slice(
        &mut self,
        len: usize,
        num_encoding: NumEncoding,
    ) -> EncodingResult<&'data [u8]> {
        // Assert the num encoding is borrowable
        if !num_encoding.borrowable() {
            return Err(EncodingError::BorrowError(
                BorrowError::NonBorrowableNumEncoding { num_encoding },
            ));
        }

        self.stream.borrow_read(len)
    }

    make_borrow_slice_fn!(borrow_u16_slice -> u16);
    make_borrow_slice_fn!(borrow_u32_slice -> u32);
    make_borrow_slice_fn!(borrow_u64_slice -> u64);
    make_borrow_slice_fn!(borrow_u128_slice -> u128);

    /// Borrows a `u8` slice of length `length` from the encoder,
    /// checking that the [`NumEncoding`] is [`borrowable`][`NumEncoding::borrowable`].
    #[inline]
    pub fn borrow_i8_slice(
        &mut self,
        len: usize,
        num_encoding: NumEncoding,
    ) -> EncodingResult<&'data [i8]> {
        // Assert the num encoding is borrowable
        if !num_encoding.borrowable() {
            return Err(EncodingError::BorrowError(
                BorrowError::NonBorrowableNumEncoding { num_encoding },
            ));
        }

        let u8_slice: &[u8] = self.stream.borrow_read(len)?;
        Ok(bytemuck::try_cast_slice(u8_slice).map_err(|_| BorrowError::AlignmentMismatch)?)
    }

    make_borrow_slice_fn!(borrow_i16_slice -> i16);
    make_borrow_slice_fn!(borrow_i32_slice -> i32);
    make_borrow_slice_fn!(borrow_i64_slice -> i64);
    make_borrow_slice_fn!(borrow_i128_slice -> i128);

    make_borrow_slice_fn!(borrow_f32_slice -> f32);
    make_borrow_slice_fn!(borrow_f64_slice -> f64);

    /// Borrows a `usize` slice of length `length` from the encoder.
    ///
    /// Checks that the [`Endianness`] and [`BitWidth`] match those of the target system,
    /// and that the [`NumEncoding`] is [`borrowable`][`NumEncoding::borrowable`]
    #[inline]
    pub fn borrow_usize_slice(
        &mut self,
        len: usize,
        num_encoding: NumEncoding,
        endianness: Endianness,
        bit_width: BitWidth,
    ) -> EncodingResult<&'data [usize]> {
        // Assert the num encoding is borrowable
        if !num_encoding.borrowable() {
            return Err(EncodingError::BorrowError(
                BorrowError::NonBorrowableNumEncoding { num_encoding },
            ));
        }

        // If the system endianness doesn't match, we would be borrowing
        // garbage-looking data
        if endianness != Endianness::native() {
            return Err(EncodingError::BorrowError(
                BorrowError::EndiannessMismatch {
                    found: endianness,
                    system: Endianness::native(),
                },
            ));
        }

        // Again, if the system bit width doesn't match, we would be borrowing a different
        // number of bytes than what the user expects
        if bit_width != BitWidth::native() {
            return Err(EncodingError::BorrowError(BorrowError::BitWidthMismatch {
                found: bit_width,
                system: BitWidth::native(),
            }));
        }

        let u8_slice = self.stream.borrow_read(bit_width.bytes() * len)?;

        // Depending on the alignment of the target system, this might fail.
        let conv: &[usize] = bytemuck::try_cast_slice(u8_slice)
            .map_err(|_| EncodingError::BorrowError(BorrowError::AlignmentMismatch))?;

        // Check that none of the elements exceed the max size
        for &elem in conv {
            let max = self.ctxt.settings.size_repr.max_size;
            if elem > max {
                return Err(EncodingError::MaxSizeExceeded {
                    requested: elem,
                    max,
                });
            }
        }

        Ok(conv)
    }

    /// Borrows a `isize` slice of length `length` from the encoder.
    ///
    /// Checks that the [`Endianness`] and [`BitWidth`] match those of the target system,
    /// and that the [`NumEncoding`] is [`borrowable`][`NumEncoding::borrowable`]
    #[inline]
    pub fn borrow_isize_slice(
        &mut self,
        len: usize,
        num_encoding: NumEncoding,
        endianness: Endianness,
        bit_width: BitWidth,
    ) -> EncodingResult<&'data [isize]> {
        // Assert the num encoding is borrowable
        if !num_encoding.borrowable() {
            return Err(EncodingError::BorrowError(
                BorrowError::NonBorrowableNumEncoding { num_encoding },
            ));
        }

        // If the system endianness doesn't match, we would be borrowing
        // garbage-looking data
        if endianness != Endianness::native() {
            return Err(EncodingError::BorrowError(
                BorrowError::EndiannessMismatch {
                    found: endianness,
                    system: Endianness::native(),
                },
            ));
        }

        // Again, if the system bit width doesn't match, we would be borrowing a different
        // number of bytes than what the user expects
        if bit_width != BitWidth::native() {
            return Err(EncodingError::BorrowError(BorrowError::BitWidthMismatch {
                found: bit_width,
                system: BitWidth::native(),
            }));
        }

        let u8_slice = self.stream.borrow_read(bit_width.bytes() * len)?;

        // Depending on the alignment of the target system, this might fail.
        let conv: &[isize] = bytemuck::try_cast_slice(u8_slice)
            .map_err(|_| EncodingError::BorrowError(BorrowError::AlignmentMismatch))?;
        Ok(conv)
    }
}

impl<T: Seek> Encoder<'_, T> {
    /// Returns the current stream position as a byte offset from the start.
    #[inline]
    pub fn stream_position(&mut self) -> EncodingResult<usize> {
        self.stream.seek(SeekFrom::POSITION)
    }

    /// Performs a seek operation on the underlying stream using the given `seek`
    /// argument.
    #[inline]
    pub fn seek(&mut self, seek: SeekFrom) -> EncodingResult<usize> {
        self.stream.seek(seek)
    }

    /// Performs a seek operation on the underlying stream using the given `seek`
    /// argument, calls the closure, then seeks back to the original position.
    // Notice the `StreamModifier` signature
    #[inline]
    pub fn with_seek<F, R>(&mut self, f: F, seek: SeekFrom) -> EncodingResult<R>
    where
        F: FnOnce(&mut Encoder<T>) -> EncodingResult<R>,
    {
        // Track the current position
        let prev = self.stream_position()? as isize;

        // Magic fn!
        let ret = f(self);

        // Seek to the desired location, and track the location now
        let cur = self.stream.seek(seek)? as isize;
        // Find the difference
        let diff = prev - cur;

        // Now we can seek even on streams that don't support seeking from the Start or End
        self.stream.seek(SeekFrom::Current(diff))?;
        ret
    }
}

/// A binary data structure specification which can be **encoded** into its binary representation.
///
/// Implementations that need to **seek** should implement for `W: Write + Seek`.
///
/// You should keep your implementation as general as possible and avoid
/// implementing for a `R = ConcreteType` if possible
pub trait Encode<W: Write> {
    /// Encodes `self` into its binary format.
    ///
    /// Calling `encode` multiple times on the same value without
    /// changing the encoder settings or the value itself in-between calls should produce
    /// the same output.
    ///
    /// If the result is Ok,
    /// implementations should guarantee that the state of the encoder
    /// is preserved. If the result is Err,
    /// no guarantees are made about the state of the encoder,
    /// and users should reset it before reuse.
    fn encode(&self, encoder: &mut Encoder<W>) -> EncodingResult<()>;
}

/// A binary data structure specification which can be **decoded** from its binary representation.
///
/// Implementations that need to **seek** should implement for `R: Read + Seek`,
/// while those that need to **borrow** should implement for `R: BorrowRead<'data>`.
///
/// If you need both, use `R: BorrowRead<'data> + Seek`
///
/// You should keep your implementation as general as possible and avoid
/// implementing for a `R = ConcreteType` if possible
///
/// # Note about lifetimes
///
/// An implementation of this trait where your type uses the same lifetime as the decoder
/// (the `'data` lifetime) will greatly limit the possible usages of the implementation.
///
/// Instead, prefer giving your type a different lifetime and make the `'data` lifetime depend on it.
///
/// ### Correct:
/// ```ignore
/// impl<'data: 'a + 'b + ..., 'a, 'b, ...> Decode<BorrowRead<'data>> for Thing<'a, 'b, ...> { ... }
/// ```
///
/// ### Misleading:
/// ```ignore
/// impl<'data> Decode<BorrowRead<'data>> for Thing<'data, 'data, ...> { ... }
/// ```
pub trait Decode<R: Read>: Sized {
    /// Decodes `Self` from its binary format.
    ///
    /// Calling `decode` multiple times without changing the
    /// encoder settings or the underlying binary data in-between calls should produce
    /// the same output.
    ///
    /// If the result is Ok,
    /// implementations should guarantee that the state of the encoder
    /// is preserved. If the result is Err,
    /// no guarantees are made about the state of the encoder,
    /// and users should reset it before reuse.
    fn decode(decoder: &mut Encoder<R>) -> EncodingResult<Self>;

    /// Decodes `Self` from its binary format.
    ///
    /// See [`Decode::decode`] for more details.
    ///
    /// Doesn't create a new `Self`, but rather updates the existing data within it.
    /// This is useful for preserving resources for example when decoding a struct continuously in
    /// a loop. If you wish to implement this function you should recursively call
    /// [`Decode::decode_in_place`] for the contents of your type whenever possible and
    /// [`Decode::decode`] when it isn't.
    fn decode_in_place(&mut self, decoder: &mut Encoder<R>) -> EncodingResult<()> {
        *self = Self::decode(decoder)?;
        Ok(())
    }
}