tree_by_path 1.0.3

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

// { Documentation
/// The easiest way to construct a Node is by using its associated function Node::new, immediately
/// passing its cargo. As a tree consists of only Nodes, this first Node is a tree having one Node.
///
/// Afterwards, other Nodes can be added to the tree, either by passing a cargo, or else by
/// creating another Node (with or without subnodes) and adding it as a child to the first or any
/// other node of the tree :
/// ```
/// use tree_by_path::{Node, TraverseAction};
///
/// // Creating a first node.
/// let mut n = Node::new('a');
///
/// // n is a one-node tree now. Let's add a child node by passing a cargo.
/// let root_path = n.get_first_path();
/// n.add_cargo_under_path(&root_path, 'b').expect(
///     "It should always be possible to add a node under the root node");
///
/// // Let's create another small tree having cargoes of the same type.
/// let mut n2 = Node::new('c');
/// n2.add_cargo_under_path(&root_path, 'd').expect(
///     "It should always be possible to add a node under the root node");
///
/// // Now we add the second tree's root node as a child to the first tree's root node :
/// n.add_node_under_path(&root_path, n2).expect(
///     "It should always be possible to add a node under the root node");
///
/// // Concatenating all of the tree's nodes' cargoes :
/// let concatenated = n.traverse(
///     String::new(),
///     |accum, current_node, _path| {
///         accum.push(current_node.cargo);
///         TraverseAction::Continue
///     }
/// );
///
/// assert_eq!("abcd".to_string(), concatenated);
/// 
/// ```
// }
#[derive(Debug, PartialEq)]
pub struct Node<C> {
    pub cargo: C,
    pub children: Vec<Node<C>>,
}

/// If `Node<C>::swap_cargo` succeeds, it returns<br />
/// `Ok<(node_parameter_moved_back_out, previous_cargo)>`.<br />
/// Otherwise, it returns<br />
/// `Err<(PathError, node_parameter_moved_back_out, new_cargo_moved_back_out)>`.
pub type CargoSwapResult<C> = Result<(Node<C>, C), (PathError, Node<C>, C)>;

impl<C> Node<C> {
    // { Documentation
    /// Creates a new node and immediately also a new tree having a single (root) node.
    ///
    /// The cargo has to be passed immediately. If you need a tree with optional cargoes
    /// in the nodes, create a node having an `Option<Whatever>` as cargo.
    ///
    /// Any node, with its subnodes, can  always be attached as a child to a node of a tree having nodes of the
    /// same cargo type. (See the `add_node_*` methods.)
    // }
    pub fn new(cargo: C) -> Node<C> {
        Node {
            cargo,
            children: Vec::<Node<C>>::new(),
        }
    }

    // { Documentation
    /// Returns `Vec::<usize>::new()`, as this is always the root node's path.
    // }
    pub fn get_first_path(&self) -> Vec<usize> {
        vec![]
    }

    // { Documentation
    /// Returns the path or address of the last subnode of the node on which this method is called.
    // }
    pub fn get_last_path(&self) -> Vec<usize> {
        let mut nd = self;
        let mut result_path = Vec::<usize>::new();
        let mut child_count: usize;

        loop {
            child_count = nd.children.len();

            if child_count == 0 {
                break;
            } else {
                child_count -= 1;
                nd = &nd.children[child_count];
                result_path.push(child_count);
            }
        }

        result_path
    }

    // { Documentation
    /// Returns the path or address of the next child node after the child node having the path passed as
    /// an argument.
    ///
    /// This next child node can be its first child, or its next sibling if it hasn't any children,
    /// or its parent's next sibling if it's the last child of its parent node, or even its
    /// grandparent, etc.
    ///
    /// If there is no next node (when the path passed as argument is the path of the tree's last
    /// node), a `PathError::RequestedPathNotAvailable` is returned.
    /// 
    /// Note that `Node.traverse` internally uses this method.
    ///
    /// Example :
    ///
    /// ```
    /// use tree_by_path::Node;
    ///
    /// let mut n = Node::new("Brussels".to_string());
    /// let root_path = Vec::<usize>::new();
    ///
    /// let ghent_path = n.add_cargo_under_path(&root_path, "Ghent".to_string()).expect(
    ///     "Error adding node under root node");
    ///
    /// let antwerp_path = n.add_cargo_under_path(&root_path, "Antwerp".to_string()).expect(
    ///     "Error adding node under root node");
    ///
    /// assert_eq!(Ok(antwerp_path.clone()), n.get_next_path(&ghent_path));
    /// ```
    // }
    pub fn get_next_path(&self, path: &[usize]) -> Result<Vec<usize>, PathError> {
        let mut result_path = path.to_owned();

        match self.borrow_node(&result_path) {
            Err(err) => Err(err),
            Ok(nd) => {
                if !nd.children.is_empty() {
                    result_path.push(0);
                    Ok(result_path)
                } else {
                    // Repeat this logic in a loop in order to find the next sibling
                    // of the path node or of a parent that has one.

                    let mut next_node_index: usize;

                    loop {
                        if result_path.is_empty() {
                            break Err(PathError::RequestedPathNotAvailable);
                        }

                        next_node_index = result_path.pop().unwrap() + 1;

                        match self.borrow_node(&result_path) {
                            Err(err) => break Err(err),
                            Ok(parent) => {
                                if next_node_index < parent.children.len() {
                                    result_path.push(next_node_index);
                                    break Ok(result_path);
                                }
                            },
                        }
                    }
                }
            },
        }
    }

    // { Documentation
    /// Returns the path or address of the previous child node before the child node having the path passed as
    /// an argument.
    ///
    /// If there is no previous node (when the path passed as argument is the path of the tree's
    /// first node), a `PathError::RequestedPathNotAvailable` is returned.
    ///
    /// Note that `Node.traverse_back` internally uses this method.
    // }
    pub fn get_previous_path(&self, path: &Vec<usize>) -> Result<Vec<usize>, PathError> {
        if path.is_empty() {
            return Err(PathError::RequestedPathNotAvailable);
        }

        let mut result_path = path.clone();

        match self.borrow_node(&result_path) {
            Err(err) => Err(err),
            Ok(_) => {
                let input_index = result_path.pop().unwrap();
                let mut children_count: usize;

                if input_index > 0 {
                    result_path.push(input_index - 1);

                    // Descend do deepest last grandchild of this node.
                    loop {
                        match self.borrow_node(&result_path) {
                            Err(err) => break Err(err),
                            Ok(nd) => {
                                    children_count = nd.children.len();

                                    if children_count > 0 {
                                        result_path.push(children_count -1);
                                    } else {
                                        break Ok(result_path);
                                    }
                            },
                        }
                    }
                } else  {
                    // Return parent's path.
                    Ok(result_path)
                }
            },
        }
    }

    // { Documentation
    /// Looks for the tree's child node having the given path,<br />
    /// adds a new child node under it having the given cargo,<br />
    /// and returns a Result being either<br />
    /// `   Ok(path_of_added_node)`<br />
    /// or else<br />
    /// `   Err((PathError, cargo_not_having_been_moved_into_tree))`.
    ///
    /// Example :
    /// ```
    /// use tree_by_path::{Node, PathError};
    ///
    /// let mut n = Node::new(0u8);
    /// let mut result: Result<Vec<usize>, (PathError, u8)>;
    ///
    /// result = n.add_cargo_under_path(&vec![], 1);
    /// assert!(result.is_ok());
    /// assert_eq!(vec![0], result.unwrap());
    ///
    /// result = n.add_cargo_under_path(&vec![], 2);
    /// assert!(result.is_ok());
    /// assert_eq!(vec![1], result.unwrap());
    /// assert_eq!(&2, n.borrow_cargo(&vec![1]).unwrap());
    ///
    /// result = n.add_cargo_under_path(&vec![0], 3);
    /// assert!(result.is_ok());
    /// assert_eq!(vec![0, 0], result.unwrap());
    ///
    /// result = n.add_cargo_under_path(&vec![0], 4);
    /// assert!(result.is_ok());
    /// assert_eq!(vec![0, 1], result.unwrap());
    ///
    /// let borrowed = n.borrow_cargo(&vec![0, 1]);
    /// assert!(borrowed.is_ok());
    /// assert_eq!(&4, borrowed.unwrap());
    ///
    /// result = n.add_cargo_under_path(&vec![50], 99);
    /// assert!(result.is_err());
    /// assert_eq!((PathError::InputPathNotFound,99), result.unwrap_err());
    ///
    /// result = n.add_cargo_under_path(&vec![0, 1, 1], 99);
    /// assert!(result.is_err());
    /// assert_eq!((PathError::InputPathNotFound,99), result.unwrap_err());
    /// ```
    // }
    pub fn add_cargo_under_path(&mut self, path: &Vec<usize>, cargo: C) -> Result<Vec<usize>, (PathError, C)> {
        let mut result_path = path.clone();
        let borrowed = self.borrow_mut_node(path);

        match borrowed {
            Err(err) => Err((err, cargo)),
            Ok(nd) =>  {
                let ix = nd.children.len();
                result_path.push(ix);
                nd.children.push(Node::new(cargo));

                Ok(result_path)
            },
        }
    }

    // { Documentation
    /// Looks for the tree's child node having the given path,<br />
    /// adds a new child node after it,<br />
    /// and returns a Result being either<br />
    /// `   Ok(path_of_added_node)`<br />
    /// or else<br />
    /// `   Err((PathError, cargo_not_having_been_moved_into_tree))`.
    ///
    /// Note that trying to add a node after the root node cannot but result in<br />
    /// `Err((PathError::InputPathNotFitForOperation, cargo_not_having_been_moved_into_tree))`,<br />
    /// as a tree's root node can't have siblings.
    // }
    pub fn add_cargo_after_path(&mut self, path: &Vec<usize>, cargo: C) -> Result<Vec<usize>, (PathError, C)> {
        if path.is_empty() {
            return Err((PathError::InputPathNotFitForOperation, cargo));
        }

        let mut result_path = path.clone();
        let last_path = result_path.pop().unwrap() + 1;

        let borrowed = self.borrow_mut_node(&result_path);

        match borrowed {
            Err(err) => Err((err, cargo)),
            Ok(nd) =>  {
                if last_path > nd.children.len() {
                    Err((PathError::RequestedPathNotAvailable, cargo))
                } else {
                    nd.children.insert(last_path, Node::new(cargo));
                    result_path.push(last_path);

                    Ok(result_path)
                }
            },
        }
    }

    // { Documentation
    /// Looks for the tree's child node having the given path,<br />
    /// adds a new child node before it,<br />
    /// and returns a Result being either<br />
    /// `   Ok(path_of_added_node)`<br />
    /// or else<br />
    /// `   Err((PathError, cargo_not_having_been_moved_into_tree))`.
    ///
    /// Note that trying to add a node before the root node cannot but result in<br />
    /// `Err((PathError::InputPathNotFitForOperation, cargo_not_having_been_moved_into_tree))`,<br />
    /// as a tree's root node can't have siblings.
    // }
    pub fn add_cargo_before_path(&mut self, path: &Vec<usize>, cargo: C) -> Result<Vec<usize>, (PathError, C)> {
        if path.is_empty() {
            return Err((PathError::InputPathNotFitForOperation, cargo));
        }

        let mut result_path = path.clone();
        let last_path = result_path.pop().unwrap();

        let borrowed = self.borrow_mut_node(&result_path);

        match borrowed {
            Err(err) => Err((err, cargo)),
            Ok(nd) =>  {
                if last_path > nd.children.len() {
                    Err((PathError::InputPathNotFound, cargo))
                } else {
                    nd.children.insert(last_path, Node::new(cargo));
                    result_path.push(last_path);

                    Ok(result_path)
                }
            },
        }
    }

    // { Documentation
    /// Looks for the tree's child node having the given path,<br />
    /// tries and removes it<br />
    /// and returns either<br />
    /// `   Ok(removed_node_with_all_its_children)`<br />
    /// or<br />
    /// `   Err(PathError)`.
    ///
    /// Trying and removing the root node amounts to trying and removing a node from itself, which
    /// cannot but result in an `Err(PathError::InputPathNotFitForOperation)`:
    ///
    /// ```
    /// use tree_by_path::{Node, PathError};
    ///
    /// let mut n = Node::new(("Jane Kirby", "CEO"));
    /// let root_path = n.get_first_path();
    ///
    /// let cfo_path = n.add_cargo_under_path(
    ///     &root_path,
    ///     ("Rob Delsing", "CFO")
    /// ).unwrap();
    ///
    /// let account_path = n.add_cargo_under_path(
    ///     &cfo_path,
    ///     ("Pierre Lévèque", "Head of Accounting")
    /// ).unwrap();
    ///
    /// let cio_path = n.add_cargo_under_path(
    ///     &root_path,
    ///     ("Dilshat Ahmetova", "CFO")
    /// ).unwrap();
    ///
    /// let mut result = n.extract_node(&root_path);
    /// assert_eq!(Err(PathError::InputPathNotFitForOperation), result);
    ///
    /// result = n.extract_node(&cfo_path);
    /// match result {
    ///     Err(_) => panic!("Failed to extract an existing child node."),
    ///     Ok(cfo_tree) => {
    ///         assert_eq!("Pierre Lévèque", cfo_tree.borrow_cargo(&vec![0]).unwrap().0)
    ///     },
    /// }
    ///
    /// assert_eq!("Dilshat Ahmetova", n.borrow_cargo(&vec![0]).unwrap().0)
    /// ```
    // }
    pub fn extract_node(&mut self, path: &Vec<usize>) -> Result<Node<C>, PathError> {
        if path.is_empty() {
            return Err(PathError::InputPathNotFitForOperation);
        }

        let mut parent_path = path.clone();
        let last_index = parent_path.pop().unwrap();
        let parent_result = self.borrow_mut_node(&parent_path);

        match parent_result {
            Err(err) => Err(err),
            Ok(parent) => {
                if parent.children.len() <= last_index {
                    Err(PathError::InputPathNotFound)
                } else {
                    Ok(parent.children.remove(last_index))
                }
            },
        }
    }

    // { Documentation
    /// Looks for the tree's child node having the given path,<br />
    /// adds the given node under it,<br />
    /// and returns a Result being either<br />
    /// `   Ok(path_of_added_node)`<br />
    /// or else<br />
    /// `   Err((PathError, node_not_having_been_moved_into_tree))`.
    ///
    /// Example :
    /// ```
    /// use tree_by_path::{Node, PathError};
    ///
    /// let mut n = Node::new(0u8);
    /// let mut result: Result<Vec<usize>, (PathError, Node<u8>)>;
    ///
    /// result = n.add_node_under_path(&vec![], Node::new(1));
    /// assert!(result.is_ok());
    /// assert_eq!(vec![0], result.unwrap());
    ///
    /// result = n.add_node_under_path(&vec![], Node::new(2));
    /// assert!(result.is_ok());
    /// assert_eq!(vec![1], result.unwrap());
    /// assert_eq!(&2, n.borrow_cargo(&vec![1]).unwrap());
    ///
    /// result = n.add_node_under_path(&vec![0], Node::new(3));
    /// assert!(result.is_ok());
    /// assert_eq!(vec![0, 0], result.unwrap());
    ///
    /// result = n.add_node_under_path(&vec![0], Node::new(4));
    /// assert!(result.is_ok());
    /// assert_eq!(vec![0, 1], result.unwrap());
    ///
    /// let borrowed = n.borrow_cargo(&vec![0, 1]);
    /// assert!(borrowed.is_ok());
    /// assert_eq!(&4, borrowed.unwrap());
    ///
    /// result = n.add_node_under_path(&vec![50], Node::new(99));
    /// assert!(result.is_err());
    /// assert_eq!((PathError::InputPathNotFound, Node::new(99)), result.unwrap_err());
    ///
    /// result = n.add_node_under_path(&vec![0, 1, 1], Node::new(99));
    /// assert!(result.is_err());
    /// assert_eq!((PathError::InputPathNotFound, Node::new(99)), result.unwrap_err());
    /// ```
    // }
    pub fn add_node_under_path(&mut self, path: &Vec<usize>, node: Node<C>) -> Result<Vec<usize>, (PathError, Node<C>)> {
        let mut result_path = path.clone();
        let borrowed = self.borrow_mut_node(path);

        match borrowed {
            Err(err) => Err((err, node)),
            Ok(nd) =>  {
                let ix = nd.children.len();
                result_path.push(ix);
                nd.children.push(node);

                Ok(result_path)
            },
        }
    }

    // { Documentation
    /// Looks for the tree's child node having the given path,<br />
    /// adds the given node after it,<br />
    /// and returns a Result being either<br />
    /// `   Ok(path_of_added_node)`<br />
    /// or else<br />
    /// `   Err((PathError, node_not_having_been_moved_into_tree))`.
    ///
    /// Note that trying to add a node after the root node cannot but result in<br />
    /// `Err((PathError::InputPathNotFitForOperation, node_not_having_been_moved_into_tree))`,<br />
    /// as a tree's root node can't have siblings.
    // }
    pub fn add_node_after_path(&mut self, path: &Vec<usize>, node: Node<C>) -> Result<Vec<usize>, (PathError, Node<C>)> {
        if path.is_empty() {
            return Err((PathError::InputPathNotFitForOperation, node));
        }

        let mut result_path = path.clone();
        let last_path = result_path.pop().unwrap() + 1;

        let borrowed = self.borrow_mut_node(&result_path);

        match borrowed {
            Err(err) => Err((err, node)),
            Ok(nd) =>  {
                if last_path > nd.children.len() {
                    Err((PathError::RequestedPathNotAvailable, node))
                } else {
                    nd.children.insert(last_path, node);
                    result_path.push(last_path);

                    Ok(result_path)
                }
            },
        }
    }

    // { Documentation
    /// Looks for the tree's child node having the given path,<br />
    /// adds the given node before it,<br />
    /// and returns a Result being either<br />
    /// `   Ok(path_of_added_node)`<br />
    /// or else<br />
    /// `   Err((PathError, node_not_having_been_moved_into_tree))`.
    ///
    /// Note that trying to add a node before the root node cannot but result in<br />
    /// `Err((PathError::InputPathNotFitForOperation, node_not_having_been_moved_into_tree))`,<br />
    /// as a tree's root node can't have siblings.
    // }
    pub fn add_node_before_path(&mut self, path: &Vec<usize>, node: Node<C>) -> Result<Vec<usize>, (PathError, Node<C>)> {
        if path.is_empty() {
            return Err((PathError::InputPathNotFitForOperation, node));
        }

        let mut result_path = path.clone();
        let last_path = result_path.pop().unwrap();

        let borrowed = self.borrow_mut_node(&result_path);

        match borrowed {
            Err(err) => Err((err, node)),
            Ok(nd) =>  {
                if last_path > nd.children.len() {
                    Err((PathError::InputPathNotFound, node))
                } else {
                    nd.children.insert(last_path, node);
                    result_path.push(last_path);

                    Ok(result_path)
                }
            },
        }
    }

    // { Documentation
    /// Looks for the node having the given path,<br />
    /// replaces it (with all its child nodes !) by the given node (with all its child nodes),<br />
    /// and returns either<br />
    /// `   Ok(node_that_was_replaced)`<br />
    /// or else<br />
    /// `   Err((PathError, node_not_having_been_moved_into_tree)`.
    ///
    /// Note that trying and replacing the root node will result in a<br />
    /// `Err((PathError::InputPathNotFitForOperation, node))`.
    ///
    /// But even if replacing a tree's child node is not possible, it is possible to replace its
    /// cargo.
    // }
    pub fn swap_node(&mut self, path: &Vec<usize>, node: Node<C>) -> Result<Node<C>, (PathError, Node<C>)> {
        if path.is_empty() {
            return Err((PathError::InputPathNotFitForOperation, node));
        }

        let mut parent_path = path.clone();
        let last_path = parent_path.pop().unwrap();

        let borrowed = self.borrow_mut_node(&parent_path);

        match borrowed {
            Err(err) => Err((err, node)),
            Ok(nd) =>  {
                if last_path >= nd.children.len() {
                    Err((PathError::InputPathNotFound, node))
                } else {
                    let removed_node = nd.children.remove(last_path);
                    nd.children.insert(last_path, node);

                    Ok(removed_node)
                }
            },
        }
    }

    // { Documentation
    /// Returns a `Result` having either
    /// - a non-mutable reference to the cargo of the child node having the given path<br />
    /// or else
    /// - a `PathError`.
    ///
    /// Example:
    /// ```
    /// use tree_by_path::Node;
    ///
    /// let mut n = Node::new("Athens");
    /// let root_path = Vec::<usize>::new();
    /// let mut added_path: Vec<usize>;
    ///
    /// added_path = n.add_cargo_under_path(&root_path, "Thessaloniki").unwrap();
    /// added_path = n.add_cargo_under_path(&added_path, "Kilkis").unwrap();
    ///
    /// added_path = n.add_cargo_under_path(&root_path, "Kavala").unwrap();
    /// added_path = n.add_cargo_under_path(&added_path, "Moustheni").unwrap();
    ///
    /// let borrow_result = n.borrow_cargo(&vec![1, 0]);
    ///
    /// assert!(borrow_result.is_ok());
    /// assert_eq!(&"Moustheni", borrow_result.unwrap());
    /// ```
    // }
    pub fn borrow_cargo(&self, path: &Vec<usize>) -> Result<&C, PathError> {
        let borrowed = self.borrow_node(path);

        match borrowed {
            Ok(nd) => Ok(&nd.cargo),
            Err(err) => Err(err),
        }
    }

    // { Documentation
    /// Returns a `Result` having either
    /// - a mutable reference to the cargo of the child node having the given path<br />
    /// or else
    /// - a `PathError`.
    ///
    /// Note : an easier way to change a (child) node's cargo is the set_cargo method.
    ///
    /// Example:
    /// ```
    /// use tree_by_path::Node;
    ///
    /// let mut n = Node::new("Greece");
    /// let root_path = Vec::<usize>::new();
    /// let mut added_path: Vec<usize>;
    ///
    /// added_path = n.add_cargo_under_path(&root_path, "Thessaloniki").unwrap();
    /// added_path = n.add_cargo_under_path(&added_path, "Kilkis").unwrap();
    ///
    /// added_path = n.add_cargo_under_path(&root_path, "Kavala").unwrap();
    /// added_path = n.add_cargo_under_path(&added_path, "Moustheni").unwrap();
    ///
    /// let mut borrow_result = n.borrow_mut_cargo(&vec![1, 0]);
    /// assert!(borrow_result.is_ok());
    /// let borrowed = borrow_result.unwrap();
    ///
    /// *borrowed = "Kokkinochori";
    /// 
    /// borrow_result = n.borrow_mut_cargo(&vec![1, 0]);
    /// assert!(borrow_result.is_ok());
    /// assert_eq!(&"Kokkinochori", borrow_result.unwrap());
    /// ```
    // }
    pub fn borrow_mut_cargo(&mut self, path: &Vec<usize>) -> Result<&mut C, PathError> {
        let borrowed = self.borrow_mut_node(path);

        match borrowed {
            Ok(nd) => Ok(&mut nd.cargo),
            Err(err) => Err(err),
        }
    }

    // { Documentation
    /// Returns a `Result` having either
    /// - ()<br />
    /// or else
    /// - a tuple `(PathError, cargo_not_having_been_moved_into_the_tree)`.
    ///
    /// Note that the cargo previously held by the targeted node is lost after a call to `set_cargo`.
    /// In order to retrieve the previously held cargo, use the `swap_cargo` method which, however, is a more
    /// expensive operation.
    ///
    /// Example:
    /// ```
    /// use tree_by_path::Node;
    ///
    /// let mut n = Node::new('a');
    /// n.add_cargo_under_path(&vec![], 'b').unwrap();
    /// n.add_cargo_under_path(&vec![], 'c').unwrap();
    ///
    /// let result = n.set_cargo(&vec![1], 'z');
    ///
    /// assert!(result.is_ok());
    /// assert_eq!(&'z', n.borrow_cargo(&vec![1]).unwrap());
    /// ```
    // }
    pub fn set_cargo(&mut self, path: &Vec<usize>, cargo: C) -> Result<(), (PathError, C)> {
        match self.borrow_mut_cargo(path) {
            Ok(borrowed_cargo) => {
                *borrowed_cargo = cargo;
                Ok(())
            },
            Err(err) => Err((err, cargo)),
        }
    }

    // { Documentation
    /// Unlike method `swap_node`, `swap_cargo`
    /// - is an associated function, to be called as `Node::swap_cargo(...)`;
    /// - takes a `Node<C>` as a parameter, instead of a mutable reference;
    /// - allows one to swap the cargo on a root node, instead of only on subnodes.
    ///
    /// The latter is also the reason why `swap_cargo` needs the target node to be moved into its
    /// first parameter: swapping the cargo on a root node involves replacing that node with
    /// another one.
    ///
    /// This also means that, if you want to swap the cargo on a node, you'll have to retrieve it from
    /// the function's return value afterwards, or it will be dropped :
    /// ```
    /// use tree_by_path::{Node, PathError, TraverseAction};
    ///
    /// #[derive(Debug)]
    /// struct NoCopy {
    ///     value: u8,
    /// }
    /// 
    /// let mut n = Node::new(NoCopy{value: 10});
    /// let added_path = n.add_cargo_under_path(&vec![], NoCopy{value: 1}).unwrap();
    /// n.add_cargo_under_path(&vec![], NoCopy{value: 7}).unwrap();
    ///
    /// n.add_cargo_under_path(&added_path, NoCopy{value: 2}).unwrap();
    /// n.add_cargo_under_path(&added_path, NoCopy{value: 3}).unwrap();
    ///
    /// let mut total = n.traverse(
    ///     0u8,
    ///     |accum, nd, _path| {
    ///         *accum += nd.cargo.value;
    ///         TraverseAction::Continue
    ///     }
    /// );
    ///
    /// assert_eq!(23u8, total);
    ///
    /// // Moving our root node n into swap_cargo's first parameter.
    /// let swap_result = Node::swap_cargo(n, &vec![], NoCopy{value: 30u8});
    ///
    /// assert!(swap_result.is_ok());
    ///
    /// let old_cargo: NoCopy;
    ///
    /// // Getting our tree with root node n back.
    /// (n, old_cargo) = swap_result.unwrap();
    ///
    /// assert_eq!(10, old_cargo.value);
    ///
    /// total = n.traverse(
    ///     0u8,
    ///     |accum, nd, _path| {
    ///         *accum += nd.cargo.value;
    ///         TraverseAction::Continue
    ///     }
    /// );
    ///
    /// assert_eq!(43u8, total);
    ///
    /// ```
    ///
    /// If ever the swap operation would fail, the node passed as first parameter is not lost and
    /// can be retrieved from the `Err(...)` result :
    ///
    /// ```
    /// use tree_by_path::{Node, PathError};
    ///
    /// let mut n = Node::new('g');
    /// n.add_cargo_under_path(&vec![], 'o').unwrap();
    ///
    /// // Moving root node n into swap_cargo's first parameter ...
    /// let result = Node::swap_cargo(n, &vec![5], 'a');
    ///
    /// assert!(result.is_err());
    /// 
    /// // ... and getting it back from the function's return value.
    /// let (err, n, new_cargo) = result.unwrap_err();
    /// assert_eq!(&'g', n.borrow_cargo(&vec![]).unwrap());
    ///
    /// assert_eq!(PathError::InputPathNotFound, err);
    ///
    /// // Besides, neither the new cargo not having been swapped is lost.
    /// assert_eq!('a', new_cargo);
    /// ```
    // }
    pub fn swap_cargo(mut tree_root: Node<C>, path: &Vec<usize>, cargo: C) -> CargoSwapResult<C> {

        fn transfer_children<C>(source: &mut Node<C>, target: &mut Node<C>) -> Result<(), PathError> {
            let mut child: Node<C>;

            while !source.children.is_empty() {
                child = match source.children.pop() {
                    Some(ch) => ch,
                    None => return Err(
                        PathError::ProcessError(
                            "Node.swap_cargo failed to pop() child nodes from old node.".to_string()
                        )
                    ),
                };

                target.children.insert(0, child);
            }

            Ok(())
        }

        let mut new_node = Node::new(cargo);
        let is_swap_from_root = path.is_empty();

        if is_swap_from_root {
            match transfer_children(&mut tree_root, &mut new_node) {
                Ok(_) => (),
                Err(err) => return Err((err, tree_root, new_node.cargo)),
            }

            Ok((new_node, tree_root.cargo))
        } else {
            let old_node = match tree_root.borrow_mut_node(path) {
                Ok(node) => node,
                Err(err) => return Err((err, tree_root, new_node.cargo)),
            };
            
            match transfer_children(old_node, &mut new_node) {
                Ok(_) => (),
                Err(err) => return Err((err, tree_root, new_node.cargo)),
            }

            match tree_root.swap_node(path, new_node) {
                Ok(extracted_node) => Ok((tree_root, extracted_node.cargo)),
                Err((err, new_nd)) => Err((err, tree_root, new_nd.cargo)),
            }
        }
    }

    // { Documentation
    /// Checks if a node has a subnode having the given path.
    ///
    /// You'll rarely want to call this method: all other methods accepting an input path return a
    /// `Result::Err(...)` which will return cargo and nodes not inserted or swapped if the given
    /// path is nonexistent or not fit for the operation.
    ///
    /// In most cases, if you use this method to prevent a subsequent method call to fail, you'll
    /// just be doubling the processing involved in retrieving the node the path points at.
    // }
    pub fn has_path(&self, path: &Vec<usize>) -> bool {
        self.borrow_node(path).is_ok()
    }

    //{ Documentation
    /// Returns a `NodeIterator<'it, C> : std::iter::Iterator` which allows iteration over immutable references to a tree's
    /// cargoes.
    ///
    /// Note that use of the methods traverse and traverse_back is usually faster and more
    /// flexible.
    //}
    pub fn iter(&self) -> NodeIterator<C> {
        NodeIterator::new(self)
    }

    //{ Documentation
    /// `traverse` receives two arguments :
    /// - an initial value for an accumulator;
    /// - a callback function or closure.
    /// `traverse` passes a node and all of its child nodes to this callback closure or function.
    /// 
    /// The callback closure receives three parameters :
    /// - a mutable reference to the accumulated value;
    /// - an immutable reference to the node passed to it;
    /// - an immutable reference to the passed node's address or path.
    ///
    /// Unlike `std::iter::Iterator.fold`, the callback closure doesn't return the accumulated value,
    /// but a TraverseAction variant.
    ///
    /// (As the accumulated value is mutable, it can be manipulated by the callback closure.)
    ///
    /// Example : totalizing the numerical cargo of all nodes until the total reaches a certain
    /// value:
    ///
    /// ```
    /// use tree_by_path::{Node, TraverseAction};
    ///
    /// let mut n = Node::new(0);
    /// n.add_cargo_under_path(&vec![], 1).unwrap();
    /// n.add_cargo_under_path(&vec![], 2).unwrap();
    /// n.add_cargo_under_path(&vec![1], 3).unwrap();
    /// n.add_cargo_under_path(&vec![1], 4).unwrap();
    /// n.add_cargo_under_path(&vec![], 5).unwrap();
    ///
    /// let outcome = n.traverse(
    ///     0,
    ///     |acc, nd, _path| {
    ///         *acc += nd.cargo;
    ///
    ///         if *acc <= 5 { TraverseAction::Continue } else { TraverseAction::Stop }
    ///     }
    /// );
    ///
    /// assert_eq!(6, outcome);
    /// ```
    //}
    pub fn traverse<Accum, CallBack>(&self, mut init: Accum, mut call_back: CallBack) -> Accum
    where CallBack: FnMut(&mut Accum, &Node<C>, &Vec<usize>) -> TraverseAction {
        let mut current_path = self.get_first_path();
        let mut current_node: &Node<C>;

        loop {
            current_node = self.borrow_node(&current_path)
                .expect("While traversing, borrowing a node should never yield Result::Err.");

            match (call_back)(&mut init, current_node, &current_path) {
                TraverseAction::Continue => (),
                TraverseAction::Stop => break,
                TraverseAction::SkipChildren => {
                    let mut last_child_index: usize;

                    // Go to last child of last child of ... of current node
                    // thus effectively causing all children of current node
                    // to be skipped.
                    while !current_node.children.is_empty() {
                        last_child_index = current_node.children.len() - 1;
                        current_path.push(last_child_index);
                        current_node = &current_node.children[last_child_index];
                    }
                },
            }

            current_path = match self.get_next_path(&current_path) {
                Ok(p) => p,
                _ => break,
            }
        }

        init
    }

    //{ Documentation
    /// `traverse_mut` receives two arguments :
    /// - an initial value for an accumulator;
    /// - a callback function or closure.
    /// 'traverse_mut' passes a node and all of its child nodes to this callback closure or function.
    /// 
    /// The callback closure receives three parameters :
    /// - a mutable reference to the accumulated value;
    /// - a mutable reference to the node passed to it;
    /// - a reference to the passed node's address or path.
    ///
    /// Unlike `std::iter::Iterator.fold`, the callback closure doesn't return the accumulated value,
    /// but a TraverseAction variant.
    ///
    /// (As the accumulated value is mutable, it can be manipulated by the callback closure.)
    ///
    /// Example : increasing the value of every node with 1:
    ///
    /// ```
    /// use tree_by_path::{Node, TraverseAction};
    ///
    /// let mut n = Node::new(0);
    /// n.add_cargo_under_path(&vec![], 1).unwrap();
    /// n.add_cargo_under_path(&vec![], 2).unwrap();
    /// n.add_cargo_under_path(&vec![1], 3).unwrap();
    /// n.add_cargo_under_path(&vec![1], 4).unwrap();
    /// n.add_cargo_under_path(&vec![], 5).unwrap();
    ///
    /// let changed_nodes = n.traverse_mut(
    ///     0,
    ///     |changed, nd, _path| {
    ///         nd.cargo += 1;
    ///         *changed += 1;
    ///
    ///         TraverseAction::Continue
    ///     }
    /// );
    ///
    /// assert_eq!(6, changed_nodes);
    ///
    /// let outcome = n.traverse_mut(
    ///     0,
    ///     |acc, nd, _path| {
    ///         *acc += nd.cargo;
    ///
    ///         TraverseAction::Continue
    ///     }
    /// );
    ///
    /// assert_eq!(21, outcome);
    /// ```
    //}
    pub fn traverse_mut<Accum, CallBack>(&mut self, mut init: Accum, mut call_back: CallBack) -> Accum
    where CallBack: FnMut(&mut Accum, &mut Node<C>, &Vec<usize>) -> TraverseAction {
        let mut current_path = self.get_first_path();
        let mut current_node: &mut Node<C>;

        loop {
            current_node = self.borrow_mut_node(&current_path)
                .expect("While traversing, borrowing a node should never yield Result::Err.");

            match (call_back)(&mut init, current_node, &current_path) {
                TraverseAction::Continue => (),
                TraverseAction::Stop => break,
                TraverseAction::SkipChildren => {
                    let mut last_child_index: usize;

                    // Go to last child of last child of ... of current node
                    // thus effectively causing all children of current node
                    // to be skipped.
                    while !current_node.children.is_empty() {
                        last_child_index = current_node.children.len() - 1;
                        current_path.push(last_child_index);
                        current_node = &mut current_node.children[last_child_index];
                    }
                },
            }

            current_path = match self.get_next_path(&current_path) {
                Ok(p) => p,
                _ => break,
            }
        }

        init
    }

    //{ Documentation
    /// `traverse_back` acts in a similar way as the `traverse` method, except
    /// - the nodes are visited in exactly the inverse order that `traverse` would visit them -
    ///     this means that a node's children are visited before their parent node,
    ///     and the root node is visited last.
    /// - `TraverseAction::SkipChildren` causes a node's parent to be visited next, and preceding
    ///     siblings to be skipped.
    //}
    pub fn traverse_back<Accum, CallBack>(&self, mut init: Accum, mut call_back: CallBack) -> Accum
    where CallBack: FnMut(&mut Accum, &Node<C>, &Vec<usize>) -> TraverseAction {
        let mut current_path = self.get_last_path();
        let mut current_node: &Node<C>;

        loop {
            current_node = self.borrow_node(&current_path)
                .expect("While traversing, borrowing a node should never yield Result::Err.");

            match (call_back)(&mut init, current_node, &current_path) {
                TraverseAction::Continue => (),
                TraverseAction::Stop => break,

                // One can't skip all of the current node's children
                // when traversing backward,
                // because these children are traversed first.
                // But one can ask to ignore other siblings and jump to the parent.
                TraverseAction::SkipChildren =>  {
                    let path_len = current_path.len();

                    if path_len > 0 {
                        let current_index = current_path[path_len - 1];

                        if current_index > 0 {
                            current_path[path_len - 1] = 0;
                        }
                    }
                },
            }

            current_path = match self.get_previous_path(&current_path) {
                Ok(p) => p,
                _ => break,
            }
        }

        init
    }

    //{ Documentation
    /// `traverse_mut_back` acts in a similar way as the `traverse_mut` method, except
    /// - the nodes are visited in exactly the inverse order that `traverse_mut` would visit them -
    ///     this means that a node's children are visited before their parent node,
    ///     and the root node is visited last.
    /// - `TraverseAction::SkipChildren` causes a node's parent to be visited next, and preceding
    ///     siblings to be skipped.
    //}
    pub fn traverse_mut_back<Accum, CallBack>(&mut self, mut init: Accum, mut call_back: CallBack) -> Accum
    where CallBack: FnMut(&mut Accum, &mut Node<C>, &Vec<usize>) -> TraverseAction {
        let mut current_path = self.get_last_path();
        let mut current_node: &mut Node<C>;

        loop {
            current_node = self.borrow_mut_node(&current_path)
                .expect("While traversing, borrowing a node should never yield Result::Err.");

            /*
            if !(call_back)(&mut init, current_node, &current_path) {
                break;
            }
            */
            match (call_back)(&mut init, current_node, &current_path) {
                TraverseAction::Continue => (),
                TraverseAction::Stop => break,

                // One can't skip all of the current node's children
                // when traversing backward,
                // because these children are traversed first.
                // But one can ask to ignore other siblings and jump to the parent.
                TraverseAction::SkipChildren =>  {
                    let path_len = current_path.len();

                    if path_len > 0 {
                        let current_index = current_path[path_len - 1];

                        if current_index > 0 {
                            current_path[path_len - 1] = 0;
                        }
                    }
                },
            }

            current_path = match self.get_previous_path(&current_path) {
                Ok(p) => p,
                _ => break,
            }
        }

        init
    }

    pub fn borrow_node(&self, path: &Vec<usize>) -> Result<&Node<C>, PathError> {
        let mut pathix = 0usize;
        let mut current_ix: usize;
        let path_len = path.len();
        let mut nd: &Node<C> = self;

        while pathix < path_len {
            current_ix = path[pathix];

            if current_ix < nd.children.len() {
                nd = &nd.children[current_ix];
            } else {
                return Err(PathError::InputPathNotFound);
            }

            pathix += 1;
        }

        Ok(nd)
    }

    pub fn borrow_mut_node(&mut self, path: &Vec<usize>) -> Result<&mut Node<C>, PathError> {
        let mut pathix = 0usize;
        let mut current_ix: usize;
        let path_len = path.len();
        let mut nd: &mut Node<C> = self;

        while pathix < path_len {
            current_ix = path[pathix];

            if current_ix < nd.children.len() {
                nd = &mut nd.children[current_ix];
            } else {
                return Err(PathError::InputPathNotFound);
            }

            pathix += 1;
        }

        Ok(nd)
    }
}

impl<C> Clone for Node<C>
where C: Clone {
    fn clone(&self) -> Self {
        Node {
            cargo: self.cargo.clone(),
            children: self.children.clone(),
        }
    }
}

//{ Documentation
/// A `TraverseAction` variant is expected as the return value of the callback closure or function
/// passed to `Node`'s `traverse(_mut)(_back)` methods.
//}
pub enum TraverseAction {
    /// Tells the traverse* methods to continue traversing the tree.
    Continue,

    /// Tells the traverse* methods to skip all children of the node being handled by the callback
    /// closure.
    /// The traverse(_mut)_back method, who visits a node's children first, will jump immediately to the
    /// parent of the node being handled, thus skipping all preceding siblings.
    SkipChildren,

    /// Tells the traverse* methods to stop the tree traversal, so no more nodes will be visited.
    Stop,
}

#[derive(PartialEq, Debug)]
pub enum PathError {
    /// Means that the path passed to a Node method doesn't exist.
    InputPathNotFound,

    /// Means that, even if the path passed to a Node method does exist, the operation still can't
    /// be performed. E.g.: no node can be added to a tree after the root node (only under it).
    InputPathNotFitForOperation,

    /// Means that the output path that would be returned by a Node method doesn't exist. E.g.:
    /// calling `Node.get_next_path(&the_path_of_the_last_node)`
    RequestedPathNotAvailable,

    /// Means that an unforeseen error occurred. In theory, this should never happen.<br />
    /// (In theory, the difference between theory and practice is extremely small.<br />
    /// In practice, however ...)
    ProcessError(String),
}

    //{ Documentation
    /// `NodeIterator<'it, C> : std::iter::Iterator` allows iteration over immutable references to a tree's
    /// cargoes.
    ///
    /// Note that use of the traverse and traverse_back methods is faster and allows for more flexibility, like
    /// - skipping a node's children,
    /// - prematurely stopping the iteration,
    /// - inspecting the nodes' path,
    /// - changing a node's cargo,
    /// - changing a node's children collection.
    ///
    /// The order the nodes are iterated over by the `next` and `next_back` methods is the same as the
    /// order used by the `Node.traverse` and `Node.traverse_back` methods.
    //}
pub struct NodeIterator<'it, C> {
    root: &'it Node<C>,
    current_path: Vec<usize>,
    is_fresh: bool,
    back_current_path: Vec<usize>,
    back_is_fresh: bool,
}

impl<'it, C> NodeIterator<'it, C> {
    pub fn new(root: &'it Node<C>) -> NodeIterator<'it, C> {
        NodeIterator {
            root,
            current_path: Vec::<usize>::new(),
            is_fresh: true,
            back_current_path: root.get_last_path(),
            back_is_fresh: true,
        }
    }

    fn get_freshness(&mut self, is_forward: bool) -> bool {
        if is_forward {self.is_fresh} else {self.back_is_fresh}
    }

    fn set_unfresh(&mut self, is_forward: bool) {
        if is_forward {
            self.is_fresh = false;
        } else {
            self.back_is_fresh = false;
        }
    }

    fn set_next_path(&mut self, is_forward:bool) -> bool {
        let mut has_next = true;

        if self.get_freshness(is_forward) {
            self.set_unfresh(is_forward);
        } else { 
            let next_path_result: Result<Vec<usize>, PathError> = if is_forward {
                self.root.get_next_path(&self.current_path)
            } else {
                self.root.get_previous_path(&self.back_current_path)
            };

            match next_path_result {
                Ok(next_path) => {
                    let no_next_back_crossing = if is_forward {
                        next_path < self.back_current_path
                    } else {
                        next_path > self.current_path
                    };

                    if (self.get_freshness(!is_forward)) || no_next_back_crossing {
                        if is_forward {
                            self.current_path = next_path;
                        } else {
                            self.back_current_path = next_path;
                        }
                    } else {
                        has_next = false;
                    }
                },
                _ => has_next = false,
            }
        }

        has_next
    }
}

impl<'it, C> Iterator for NodeIterator<'it, C> {
    type Item = &'it C;

    fn next(&mut self) -> Option<Self::Item> {
        let has_next = self.set_next_path(true);

        match has_next {
            true => Some(self.root.borrow_cargo(&self.current_path).unwrap()),
            false => None,
        }
    }
}

impl<'it, C> DoubleEndedIterator for NodeIterator<'it, C> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let has_next = self.set_next_path(false);

        match has_next {
            true => Some(self.root.borrow_cargo(&self.back_current_path).unwrap()),
            false => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn node_new() {
        let n = Node::new(4u8);

        assert_eq!(4u8, n.cargo);
        assert_eq!(0, n.children.len());
    }

    #[test]
    fn node_borrow_node() {
        let mut n = Node::new(0u8);
        n.children.push(Node::new(1u8));
        n.children.push(Node::new(2u8));
        n.children[1].children.push(Node::new(3u8));
        n.children[1].children.push(Node::new(4u8));
        n.children.push(Node::new(5u8));

        let mut borrowed: Result<&Node<u8>, PathError>;

        borrowed = n.borrow_node(&Vec::<usize>::new());
        assert!(borrowed.is_ok());
        assert_eq!(0, borrowed.unwrap().cargo);

        borrowed = n.borrow_node(&vec![1]);
        assert!(borrowed.is_ok());
        assert_eq!(2, borrowed.unwrap().cargo);

        borrowed = n.borrow_node(&vec![2]);
        assert!(borrowed.is_ok());
        assert_eq!(5, borrowed.unwrap().cargo);

        borrowed = n.borrow_node(&vec![3]);
        assert!(borrowed.is_err());

        borrowed = n.borrow_node(&vec![1, 0]);
        assert!(borrowed.is_ok());
        assert_eq!(3, borrowed.unwrap().cargo);

        borrowed = n.borrow_node(&vec![1, 1]);
        assert!(borrowed.is_ok());
        assert_eq!(4, borrowed.unwrap().cargo);

        borrowed = n.borrow_node(&vec![1, 2]);
        assert!(borrowed.is_err());

        borrowed = n.borrow_node(&vec![1, 1, 0]);
        assert!(borrowed.is_err());
    }

    #[test]
    fn node_borrow_mut_node() {
        let mut n = Node::new(0u8);
        n.children.push(Node::new(1u8));
        n.children.push(Node::new(2u8));
        n.children[1].children.push(Node::new(3u8));
        n.children[1].children.push(Node::new(4u8));
        n.children.push(Node::new(5u8));

        let mut borrowed: Result<&mut Node<u8>, PathError>;

        borrowed = n.borrow_mut_node(&Vec::<usize>::new());
        assert!(borrowed.is_ok());
        assert_eq!(0, borrowed.unwrap().cargo);

        borrowed = n.borrow_mut_node(&vec![1]);
        assert!(borrowed.is_ok());
        assert_eq!(2, borrowed.unwrap().cargo);

        borrowed = n.borrow_mut_node(&vec![2]);
        assert!(borrowed.is_ok());
        assert_eq!(5, borrowed.unwrap().cargo);

        borrowed = n.borrow_mut_node(&vec![3]);
        assert!(borrowed.is_err());

        borrowed = n.borrow_mut_node(&vec![1, 0]);
        assert!(borrowed.is_ok());
        assert_eq!(3, borrowed.unwrap().cargo);

        borrowed = n.borrow_mut_node(&vec![1, 1]);
        assert!(borrowed.is_ok());
        assert_eq!(4, borrowed.unwrap().cargo);

        borrowed = n.borrow_mut_node(&vec![1, 2]);
        assert!(borrowed.is_err());

        borrowed = n.borrow_mut_node(&vec![1, 1, 0]);
        assert!(borrowed.is_err());
    }

    #[test]
    fn node_borrow_cargo() {
        let mut n = Node::new(0u8);
        n.children.push(Node::new(1u8));
        n.children.push(Node::new(2u8));
        n.children[1].children.push(Node::new(3u8));
        n.children[1].children.push(Node::new(4u8));
        n.children.push(Node::new(5u8));

        let mut borrowed: Result<&u8, PathError>;

        borrowed = n.borrow_cargo(&Vec::<usize>::new());
        assert!(borrowed.is_ok());
        assert_eq!(&0, borrowed.unwrap());

        borrowed = n.borrow_cargo(&vec![1]);
        assert!(borrowed.is_ok());
        assert_eq!(&2, borrowed.unwrap());

        borrowed = n.borrow_cargo(&vec![2]);
        assert!(borrowed.is_ok());
        assert_eq!(&5, borrowed.unwrap());

        borrowed = n.borrow_cargo(&vec![3]);
        assert!(borrowed.is_err());

        borrowed = n.borrow_cargo(&vec![1, 0]);
        assert!(borrowed.is_ok());
        assert_eq!(&3, borrowed.unwrap());

        borrowed = n.borrow_cargo(&vec![1, 1]);
        assert!(borrowed.is_ok());
        assert_eq!(&4, borrowed.unwrap());

        borrowed = n.borrow_cargo(&vec![1, 2]);
        assert!(borrowed.is_err());

        borrowed = n.borrow_cargo(&vec![1, 1, 0]);
        assert!(borrowed.is_err());
    }

    #[test]
    fn node_borrow_mut_cargo() {
        let mut n = Node::new(0u8);
        n.children.push(Node::new(1u8));
        n.children.push(Node::new(2u8));
        n.children[1].children.push(Node::new(3u8));
        n.children[1].children.push(Node::new(4u8));
        n.children.push(Node::new(5u8));

        let mut borrowed: Result<&mut u8, PathError>;

        // Check if we can read twice.
        for _i in 0..2 {
            borrowed = n.borrow_mut_cargo(&Vec::<usize>::new());
            assert!(borrowed.is_ok());
            assert_eq!(&mut 0, borrowed.unwrap());
        }

        borrowed = n.borrow_mut_cargo(&vec![1]);
        assert!(borrowed.is_ok());
        assert_eq!(&mut 2, borrowed.unwrap());

        borrowed = n.borrow_mut_cargo(&vec![2]);
        assert!(borrowed.is_ok());
        assert_eq!(&mut 5, borrowed.unwrap());

        borrowed = n.borrow_mut_cargo(&vec![3]);
        assert!(borrowed.is_err());

        borrowed = n.borrow_mut_cargo(&vec![1, 0]);
        assert!(borrowed.is_ok());
        assert_eq!(&mut 3, borrowed.unwrap());

        // Check if we can change the cargo.
        borrowed = n.borrow_mut_cargo(&vec![1, 1]);
        assert!(borrowed.is_ok());
        let unwrapped = borrowed.unwrap();
        assert_eq!(&mut 4, unwrapped);
        *unwrapped = 40;
        borrowed = n.borrow_mut_cargo(&vec![1, 1]);
        assert!(borrowed.is_ok());
        assert_eq!(&mut 40, borrowed.unwrap());

        borrowed = n.borrow_mut_cargo(&vec![1, 2]);
        assert!(borrowed.is_err());

        borrowed = n.borrow_mut_cargo(&vec![1, 1, 0]);
        assert!(borrowed.is_err());
    }

    #[test]
    fn node_add_cargo_under_path() {
        let mut n = Node::new(0u8);
        let mut result: Result<Vec<usize>, (PathError, u8)>;

        result = n.add_cargo_under_path(&vec![], 1);
        assert!(result.is_ok());
        assert_eq!(vec![0], result.unwrap());

        result = n.add_cargo_under_path(&vec![], 2);
        assert!(result.is_ok());
        assert_eq!(vec![1], result.unwrap());
        assert_eq!(&2, n.borrow_cargo(&vec![1]).unwrap());

        result = n.add_cargo_under_path(&vec![0], 3);
        assert!(result.is_ok());
        assert_eq!(vec![0, 0], result.unwrap());

        result = n.add_cargo_under_path(&vec![0], 4);
        assert!(result.is_ok());
        assert_eq!(vec![0, 1], result.unwrap());

        let borrowed = n.borrow_cargo(&vec![0, 1]);
        assert!(borrowed.is_ok());
        assert_eq!(&4, borrowed.unwrap());

        result = n.add_cargo_under_path(&vec![50], 99);
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFound,99), result.unwrap_err());

        result = n.add_cargo_under_path(&vec![0, 1, 1], 99);
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFound,99), result.unwrap_err());
    }

    #[test]
    fn node_add_cargo_after_empty() {
        let mut n = Node::new(0i8);
        let result: Result<Vec<usize>, (PathError, i8)>;

        result = n.add_cargo_after_path(&vec![], -38);
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFitForOperation, -38), result.unwrap_err());
    }

    #[test]
    fn node_add_cargo_after_path() {
        let mut n = Node::new(0i8);
        let mut result: Result<Vec<usize>, (PathError, i8)>;
        let mut result_path: Vec<usize>;

        result = n.add_cargo_after_path(&vec![0], 1);
        assert!(result.is_err());
        assert_eq!((PathError::RequestedPathNotAvailable, 1), result.unwrap_err());

        n.add_cargo_under_path(&vec![], 1).unwrap();

        result = n.add_cargo_after_path(&vec![0], 2);
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![1], result_path);
        assert_eq!(&2, n.borrow_cargo(&result_path).unwrap());

        result = n.add_cargo_after_path(&vec![0], 3);
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![1], result_path);
        assert_eq!(&3, n.borrow_cargo(&result_path).unwrap());
        assert_eq!(&2, n.borrow_cargo(&vec![2]).unwrap());
    }

    #[test]
    fn node_add_cargo_before_empty() {
        let mut n = Node::new(0i8);
        let result: Result<Vec<usize>, (PathError, i8)>;

        result = n.add_cargo_before_path(&vec![], -38);
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFitForOperation,-38), result.unwrap_err());
    }

    #[test]
    fn node_add_cargo_before_path() {
        let mut n = Node::new(0i8);
        let mut result: Result<Vec<usize>, (PathError, i8)>;
        let mut result_path: Vec<usize>;

        result = n.add_cargo_before_path(&vec![0], 1);
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![0], result_path);
        assert_eq!(&1, n.borrow_cargo(&result_path).unwrap());

        result = n.add_cargo_before_path(&vec![0], 2);
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![0], result_path);
        assert_eq!(&2, n.borrow_cargo(&result_path).unwrap());
        assert_eq!(&1, n.borrow_cargo(&vec![1]).unwrap());

        result = n.add_cargo_before_path(&vec![1], 3);
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![1], result_path);
        assert_eq!(&3, n.borrow_cargo(&result_path).unwrap());
        assert_eq!(&1, n.borrow_cargo(&vec![2]).unwrap());

        result = n.add_cargo_before_path(&vec![0, 0], 4);
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![0, 0], result_path);
        assert_eq!(&4, n.borrow_cargo(&result_path).unwrap());

        result = n.add_cargo_before_path(&vec![2, 1], 5);
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFound,5), result.unwrap_err());
    }

    #[test]
    fn node_get_next_on_lone_root() {
        let n = Node::new(0u8);
        assert_eq!(Err(PathError::RequestedPathNotAvailable), n.get_next_path(&vec![]));
    }

    #[test]
    fn node_get_next_input_not_found() {
        let n = Node::new(0u8);
        assert_eq!(Err(PathError::InputPathNotFound), n.get_next_path(&vec![5]));
    }

    #[test]
    fn node_get_next_from_last() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        assert_eq!(Err(PathError::RequestedPathNotAvailable), n.get_next_path(&vec![1]));
    }

    #[test]
    #[should_panic]
    fn node_get_next_from_last_unwrap() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        let path_result = n.get_next_path(&vec![1]);
        path_result.unwrap();
    }

    #[test]
    fn node_get_next_next_sibling() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();

        let path_result = n.get_next_path(&vec![0]);
        assert!(path_result.is_ok());
        let next_path = path_result.unwrap();
        assert_eq!(vec![1], next_path);
        assert_eq!(&2, n.borrow_cargo(&next_path).unwrap());
    }

    #[test]
    fn node_get_next_root_to_child() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        assert_eq!(Ok(vec![0]), n.get_next_path(&vec![]));
    }

    #[test]
    fn node_get_next_non_root_to_child() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_after_path(&vec![1], 4).unwrap();
        assert_eq!(Ok(vec![1, 0]), n.get_next_path(&vec![1]));
    }

    #[test]
    fn node_get_next_non_child_to_parents_sibling() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_after_path(&vec![1], 4).unwrap();
        assert_eq!(Ok(vec![2]), n.get_next_path(&vec![1, 0]));
    }

    #[test]
    fn node_get_next_non_child_to_grandparents_sibling() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_under_path(&vec![1, 0], 4).unwrap();
        n.add_cargo_after_path(&vec![1], 5).unwrap();
        assert_eq!(Ok(vec![2]), n.get_next_path(&vec![1, 0, 0]));
    }

    #[test]
    fn node_get_next_from_last_is_grandchild() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_under_path(&vec![1, 0], 4).unwrap();
        assert_eq!(Err(PathError::RequestedPathNotAvailable), n.get_next_path(&vec![1, 0, 0]));
    }

    #[test]
    fn node_get_previous_from_unexistent() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_under_path(&vec![1, 0], 4).unwrap();
        assert_eq!(Err(PathError::InputPathNotFound), n.get_next_path(&vec![3, 0, 0]));
    }

    #[test]
    fn node_get_previous_from_root() {
        let n = Node::new(String::from("aaa"));
        assert_eq!(Err(PathError::RequestedPathNotAvailable), n.get_previous_path(&vec![]));
    }

    #[test]
    fn node_get_previous() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_after_path(&vec![0], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_under_path(&vec![1, 0], 4).unwrap();
        n.add_cargo_after_path(&vec![1], 5).unwrap();
        n.add_cargo_after_path(&vec![2], 6).unwrap();
        n.add_cargo_after_path(&vec![1, 0], 50).unwrap();
        n.add_cargo_under_path(&vec![1, 1], 51).unwrap();
        n.add_cargo_after_path(&vec![1, 1, 0], 52).unwrap();

        /*
         *  0
         *  |
         *  1--2----------5--6
         *     |
         *     3--50
         *     |  |
         *     4  51--52
        */

        let mut path_result: Result<Vec<usize>, PathError>;
        let mut previous: Vec<usize>;

        // Descend to last grandchild under previous sibling.
        path_result = n.get_previous_path(&vec![2]);
        assert!(path_result.is_ok());
        previous = path_result.unwrap();
        assert_eq!(vec![1, 1, 1], previous);
        assert_eq!(&52, n.borrow_cargo(&previous).unwrap());

        // Descend to last grandchild under previous sibling (bis)
        path_result = n.get_previous_path(&vec![1, 1]);
        assert!(path_result.is_ok());
        previous = path_result.unwrap();
        assert_eq!(vec![1, 0, 0], previous);
        assert_eq!(&4, n.borrow_cargo(&previous).unwrap());

        // Find previous sibling
        path_result = n.get_previous_path(&vec![1, 1, 1]);
        assert!(path_result.is_ok());
        previous = path_result.unwrap();
        assert_eq!(vec![1, 1, 0], previous);
        assert_eq!(&51, n.borrow_cargo(&previous).unwrap());

        // Find previous sibling (bis)
        path_result = n.get_previous_path(&vec![1]);
        assert!(path_result.is_ok());
        previous = path_result.unwrap();
        assert_eq!(vec![0], previous);
        assert_eq!(&1, n.borrow_cargo(&previous).unwrap());

        // Find non-root parent
        path_result = n.get_previous_path(&vec![1, 1, 0]);
        assert!(path_result.is_ok());
        previous = path_result.unwrap();
        assert_eq!(vec![1, 1], previous);
        assert_eq!(&50, n.borrow_cargo(&previous).unwrap());
        
        // Find root parent
        path_result = n.get_previous_path(&vec![0]);
        assert!(path_result.is_ok());
        previous = path_result.unwrap();
        assert_eq!(Vec::<usize>::new(), previous);
        assert_eq!(&0, n.borrow_cargo(&previous).unwrap());
    }

    #[test]
    fn node_get_last_on_lone_root() {
        let n = Node::new('K');
        let result = n.get_last_path();
        assert_eq!(Vec::<usize>::new(), result);
    }

    #[test]
    fn node_get_last_on_lone_child() {
        let mut n = Node::new('K');
        n.add_cargo_under_path(&vec![], 'Z').unwrap();
        let result = n.get_last_path();
        assert_eq!(vec![0usize], result);
    }

    #[test]
    fn node_get_last_on_first_level() {
        let mut n = Node::new('A');
        n.add_cargo_under_path(&vec![], 'B').unwrap();
        n.add_cargo_under_path(&vec![], 'C').unwrap();
        n.add_cargo_under_path(&vec![], 'D').unwrap();
        let result = n.get_last_path();
        assert_eq!(vec![2usize], result);
        assert_eq!(&'D', n.borrow_cargo(&vec![2]).unwrap());
    }

    #[test]
    fn node_extract_node_by_root() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&n.get_first_path(), 1).unwrap();
        let result = n.extract_node(&n.get_first_path());
        assert!(result.is_err());
        assert_eq!(PathError::InputPathNotFitForOperation, result.unwrap_err());
    }

    #[test]
    fn node_extract_node_by_nonexistent() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&n.get_first_path(), 1).unwrap();
        let result = n.extract_node(&vec![0, 3]);
        assert!(result.is_err());
        assert_eq!(PathError::InputPathNotFound, result.unwrap_err());
    }

    #[test]
    fn node_extract_node_by_lone_leaf() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&n.get_first_path(), 1).unwrap();
        let result = n.extract_node(&vec![0]);
        assert!(result.is_ok());
        let nd = result.unwrap();
        assert_eq!(1, nd.cargo);
    }

    #[test]
    fn node_extract_node_by_lone_non_leaf() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&n.get_first_path(), 1).unwrap();
        n.add_cargo_under_path(&vec![0], 2).unwrap();
        n.add_cargo_under_path(&vec![0], 3).unwrap();
        let result = n.extract_node(&vec![0]);
        assert!(result.is_ok());
        let nd = result.unwrap();
        assert_eq!(1, nd.cargo);
        assert!(nd.borrow_cargo(&vec![1]).is_ok());
    }

    #[test]
    fn node_extract_node_by_non_leaf() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 90).unwrap();
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_under_path(&vec![], 91).unwrap();
        n.add_cargo_under_path(&vec![1], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        let result = n.extract_node(&vec![1]);
        assert!(result.is_ok());
        let nd = result.unwrap();
        assert_eq!(1, nd.cargo);
        assert!(nd.borrow_cargo(&vec![1]).is_ok());
        assert!(!n.has_path(&vec![2]));
        assert_eq!(&91, n.borrow_cargo(&vec![1]).unwrap());
    }

    #[test]
    fn node_add_node_under_nonexistent() {
        let mut n = Node::new(0u8);
        let n1 = Node::new(1u8);
        let result: Result<Vec<usize>, (PathError, Node<u8>)>;

        result = n.add_node_under_path(&vec![2, 4], n1);
        assert!(result.is_err());
        let (err, bounced_node) = result.unwrap_err();
        assert_eq!(PathError::InputPathNotFound, err);
        assert_eq!(&1u8, bounced_node.borrow_cargo(&vec![]).unwrap());
    }

    #[test]
    fn node_add_node_under_path() {
        let mut n = Node::new(0u8);
        let n1 = Node::new(1u8);
        let result: Result<Vec<usize>, (PathError, Node<u8>)>;
        let result_path: Vec<usize>;

        result = n.add_node_under_path(&vec![], n1);
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![0], result_path);
        assert_eq!(&1, n.borrow_cargo(&result_path).unwrap());
    }

    #[test]
    fn node_add_node_after_empty() {
        let mut n = Node::new(0i8);
        let n1 = Node::new(-38i8);
        let result: Result<Vec<usize>, (PathError, Node<i8>)>;

        result = n.add_node_after_path(&vec![], n1);
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFitForOperation, Node::new(-38i8)), result.unwrap_err());
    }

    #[test]
    fn node_add_node_after_path() {
        let mut n = Node::new(0i8);
        let n1 = Node::new(1i8);
        let mut result: Result<Vec<usize>, (PathError, Node<i8>)>;
        let mut result_path: Vec<usize>;

        result = n.add_node_after_path(&vec![0], n1);
        assert!(result.is_err());
        let (err, bounced_node) = result.unwrap_err();
        assert_eq!(PathError::RequestedPathNotAvailable, err);
        assert_eq!(Node::new(1i8), bounced_node);

        n.add_node_under_path(&vec![], bounced_node).unwrap();

        result = n.add_node_after_path(&vec![0], Node::new(2i8));
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![1], result_path);
        assert_eq!(&2, n.borrow_cargo(&result_path).unwrap());

        result = n.add_node_after_path(&vec![0], Node::new(3));
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![1], result_path);
        assert_eq!(&3, n.borrow_cargo(&result_path).unwrap());
        assert_eq!(&2, n.borrow_cargo(&vec![2]).unwrap());
    }

    #[test]
    fn node_add_node_before_empty() {
        let mut n = Node::new(0i8);
        let result: Result<Vec<usize>, (PathError, Node<i8>)>;

        result = n.add_node_before_path(&vec![], Node::new(-38));
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFitForOperation, Node::new(-38)), result.unwrap_err());
    }

    #[test]
    fn node_add_node_before_path() {
        let mut n = Node::new(0i8);
        let mut result: Result<Vec<usize>, (PathError, Node<i8>)>;
        let mut result_path: Vec<usize>;

        result = n.add_node_before_path(&vec![0], Node::new(1));
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![0], result_path);
        assert_eq!(&1, n.borrow_cargo(&result_path).unwrap());

        result = n.add_node_before_path(&vec![0], Node::new(2));
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![0], result_path);
        assert_eq!(&2, n.borrow_cargo(&result_path).unwrap());
        assert_eq!(&1, n.borrow_cargo(&vec![1]).unwrap());

        result = n.add_node_before_path(&vec![1], Node::new(3));
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![1], result_path);
        assert_eq!(&3, n.borrow_cargo(&result_path).unwrap());
        assert_eq!(&1, n.borrow_cargo(&vec![2]).unwrap());

        result = n.add_node_before_path(&vec![0, 0], Node::new(4));
        assert!(result.is_ok());
        result_path = result.unwrap();
        assert_eq!(vec![0, 0], result_path);
        assert_eq!(&4, n.borrow_cargo(&result_path).unwrap());

        result = n.add_node_before_path(&vec![2, 1], Node::new(5));
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFound, Node::new(5)), result.unwrap_err());
    }
    
    #[test]
    fn node_swap_node_by_root() {
        let mut n = Node::new(0u8);
        let to_swap = Node::new(1u8);
        let result = n.swap_node(&vec![], to_swap);
        assert!(result.is_err());
        let (err, bounced_node) = result.unwrap_err();
        assert_eq!(PathError::InputPathNotFitForOperation, err);
        assert_eq!(&1u8, bounced_node.borrow_cargo(&vec![]).unwrap());
    }

    #[test]
    fn node_swap_node_by_nonexistent() {
        let mut n = Node::new(0u8);
        let to_swap = Node::new(1u8);
        let result = n.swap_node(&vec![8], to_swap);
        assert!(result.is_err());
        let (err, bounced_node) = result.unwrap_err();
        assert_eq!(PathError::InputPathNotFound, err);
        assert_eq!(&1u8, bounced_node.borrow_cargo(&vec![]).unwrap());
    }
    
    #[test]
    fn node_swap_node() {
        let mut n = Node::new(0u8);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_under_path(&vec![], 2).unwrap();
        n.add_cargo_under_path(&vec![], 3).unwrap();
        n.add_cargo_under_path(&vec![1], 20).unwrap();
        n.add_cargo_under_path(&vec![1], 21).unwrap();
        n.add_cargo_under_path(&vec![1], 22).unwrap();

        let mut to_swap = Node::new(9u8);
        to_swap.add_cargo_under_path(&vec![], 90).unwrap();
        to_swap.add_cargo_under_path(&vec![], 91).unwrap();
        to_swap.add_cargo_under_path(&vec![], 92).unwrap();
        to_swap.add_cargo_under_path(&vec![], 93).unwrap();

        let result = n.swap_node(&vec![1], to_swap);
        assert!(result.is_ok());
        let removed = result.unwrap();

        assert_eq!(4, n.borrow_node(&vec![1]).unwrap().children.len());
        assert_eq!(3, removed.children.len());

        assert_eq!(&9, n.borrow_cargo(&vec![1]).unwrap());
        assert_eq!(&93, n.borrow_cargo(&vec![1, 3]).unwrap());
        assert_eq!(&2, removed.borrow_cargo(&vec![]).unwrap());
        assert_eq!(&22, removed.borrow_cargo(&vec![2]).unwrap());
    }

    #[test]
    fn node_iter_lone_root() {
        let n = Node::new('A');

        let concat = n.iter().fold(
            String::new(),
            |mut accu: String, cargo: &char| {
                accu.push(*cargo);
                accu
            }
        );

        assert_eq!("A".to_string(), concat);
    }

    #[test]
    fn node_iter_back_lone_root() {
        let n = Node::new('A');

        let concat = n.iter().rfold(
            String::new(),
            |mut accu: String, cargo: &char| {
                accu.push(*cargo);
                accu
            }
        );

        assert_eq!("A".to_string(), concat);
    }

    #[test]
    fn node_iter() {
        let mut n = Node::new('A');
        n.add_cargo_under_path(&vec![], 'B').unwrap();
        n.add_cargo_under_path(&vec![], 'C').unwrap();
        n.add_cargo_under_path(&vec![], 'D').unwrap();
        n.add_cargo_under_path(&vec![2], 'E').unwrap();
        n.add_cargo_under_path(&vec![2], 'F').unwrap();
        n.add_cargo_under_path(&vec![2, 1], 'G').unwrap();
        n.add_cargo_under_path(&vec![], 'H').unwrap();

        let concat = n.iter().fold(
            String::new(),
            |mut accu: String, cargo: &char| {
                accu.push(*cargo);
                accu
            }
        );

        assert_eq!("ABCDEFGH".to_string(), concat);
    }

    #[test]
    fn node_iter_back() {
        let mut n = Node::new('A');
        n.add_cargo_under_path(&vec![], 'B').unwrap();
        n.add_cargo_under_path(&vec![], 'C').unwrap();
        n.add_cargo_under_path(&vec![], 'D').unwrap();
        n.add_cargo_under_path(&vec![2], 'E').unwrap();
        n.add_cargo_under_path(&vec![2], 'F').unwrap();
        n.add_cargo_under_path(&vec![2, 1], 'G').unwrap();
        n.add_cargo_under_path(&vec![], 'H').unwrap();

        let concat = n.iter().rfold(
            String::new(),
            |mut accu: String, cargo: &char| {
                accu.push(*cargo);
                accu
            }
        );

        assert_eq!("HGFEDCBA".to_string(), concat);
    }

    #[test]
    fn node_iter_next_back_dont_cross() {
        let mut n = Node::new('A');
        n.add_cargo_under_path(&vec![], 'B').unwrap();
        n.add_cargo_under_path(&vec![], 'C').unwrap();
        n.add_cargo_under_path(&vec![], 'D').unwrap();
        n.add_cargo_under_path(&vec![2], 'E').unwrap();
        n.add_cargo_under_path(&vec![2], 'F').unwrap();
        n.add_cargo_under_path(&vec![2, 1], 'G').unwrap();
        n.add_cargo_under_path(&vec![], 'H').unwrap();

        let mut concat = String::new();
        let mut iter = n.iter();
        let mut no_next = false;
        let mut no_back = false;

        loop {
            if !no_next {
                match iter.next() {
                    Some(ch) => concat.push(*ch),
                    None => no_next = true,
                }
            }

            if !no_back {
                match iter.next_back() {
                    Some(ch) => concat.push(*ch),
                    None => no_back = true,
                }
            }

            if no_next && no_back {
                break;
            }
        }

        assert_eq!("AHBGCFDE", concat);
    }

    #[test]
    fn node_iter_back_next_dont_cross() {
        let mut n = Node::new('A');
        n.add_cargo_under_path(&vec![], 'B').unwrap();
        n.add_cargo_under_path(&vec![], 'C').unwrap();
        n.add_cargo_under_path(&vec![], 'D').unwrap();
        n.add_cargo_under_path(&vec![2], 'E').unwrap();
        n.add_cargo_under_path(&vec![2], 'F').unwrap();
        n.add_cargo_under_path(&vec![2, 1], 'G').unwrap();
        n.add_cargo_under_path(&vec![], 'H').unwrap();

        let mut concat = String::new();
        let mut iter = n.iter();
        let mut no_next = false;
        let mut no_back = false;

        loop {
            if !no_back {
                match iter.next_back() {
                    Some(ch) => concat.push(*ch),
                    None => no_back = true,
                }
            }

            if !no_next {
                match iter.next() {
                    Some(ch) => concat.push(*ch),
                    None => no_next = true,
                }
            }

            if no_next && no_back {
                break;
            }
        }

        assert_eq!("HAGBFCED", concat);
    }

    #[test]
    fn node_traverse_lone_root() {
        let n = Node::new(38);

        let outcome = n.traverse(
            0,
            |acc, nd, _path| {
                *acc += nd.cargo;
                TraverseAction::Continue
            }
        );

        assert_eq!(38, outcome);
    }

    #[test]
    fn node_traverse_mut_and_change() {
        let mut n = Node::new(0);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_under_path(&vec![], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_under_path(&vec![1], 4).unwrap();
        n.add_cargo_under_path(&vec![], 5).unwrap();

        let mut outcome = n.traverse_mut(
            0,
            |acc, nd, path| {
                if path.len() == 1 {
                    nd.cargo *= 2;
                    *acc += 1;
                }

                TraverseAction::Continue
            }
        );

        assert_eq!(3, outcome);

        outcome = n.traverse(
            0,
            |acc, nd, _path| {
                *acc += nd.cargo;
                TraverseAction::Continue
            }
        );

        assert_eq!(23, outcome);
    }

    #[test]
    fn node_traverse_mut_back_and_change() {
        let mut n = Node::new(0);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_under_path(&vec![], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_under_path(&vec![1], 4).unwrap();
        n.add_cargo_under_path(&vec![], 5).unwrap();

        let mut outcome = n.traverse_mut_back(
            0,
            |acc, nd, path| {
                if path.len() == 1 {
                    nd.cargo *= 2;
                    *acc += 1;
                }

                TraverseAction::Continue
            }
        );

        assert_eq!(3, outcome);

        outcome = n.traverse_back(
            0,
            |acc, nd, _path| {
                *acc += nd.cargo;
                TraverseAction::Continue
            }
        );

        assert_eq!(23, outcome);
    }

    #[test]
    fn node_traverse_break() {
        let mut n = Node::new(0);
        n.add_cargo_under_path(&vec![], 1).unwrap();
        n.add_cargo_under_path(&vec![], 2).unwrap();
        n.add_cargo_under_path(&vec![1], 3).unwrap();
        n.add_cargo_under_path(&vec![1], 4).unwrap();
        n.add_cargo_under_path(&vec![], 5).unwrap();

        let outcome = n.traverse(
            0,
            |acc, nd, _path| {
                *acc += nd.cargo;

                if *acc <= 5 { TraverseAction::Continue } else { TraverseAction::Stop }
            }
        );

        assert_eq!(6, outcome);
    }

    #[test]
    fn node_traverse_skip_children_on_lone_root() {
        let n = Node::new('A');

        let count_nodes = n.traverse(
            0usize,
            |accum, _nd, _path| {
                *accum += 1;

                TraverseAction::SkipChildren
            }
        );

        assert_eq!(1usize, count_nodes);
    }

    #[test]
    fn node_traverse_skip_children() {
        let mut n = Node::new(0);   
        let root_path = n.get_first_path();
        let deeper_path: Vec<usize>;

        let mut root_child_path = n.add_cargo_under_path(&root_path, 0).unwrap();
        n.add_cargo_under_path(&root_child_path, 1).unwrap();
        n.add_cargo_under_path(&root_child_path, 2).unwrap();
        n.add_cargo_under_path(&root_child_path, 3).unwrap();

        root_child_path = n.add_cargo_under_path(&root_path, 100).unwrap();
        n.add_cargo_under_path(&root_child_path, 4).unwrap();
        n.add_cargo_under_path(&root_child_path, 5).unwrap();
        deeper_path = n.add_cargo_under_path(&root_child_path, 6).unwrap();
        n.add_cargo_under_path(&deeper_path, 50).unwrap();

        root_child_path = n.add_cargo_under_path(&root_path, 0).unwrap();
        n.add_cargo_under_path(&root_child_path, 7).unwrap();
        n.add_cargo_under_path(&root_child_path, 8).unwrap();
        n.add_cargo_under_path(&root_child_path, 9).unwrap();

        let sum = n.traverse(
            0,
            |accum, nd, _path| {
                *accum += nd.cargo;

                match nd.cargo {
                    100 => TraverseAction::SkipChildren,
                    _ => TraverseAction::Continue,
                }
            }
        );

        assert_eq!(130, sum);
    }

    #[test]
    fn node_traverse_back_skip_children() {
        let mut n = Node::new(0);   
        let root_path = n.get_first_path();

        let mut root_child_path = n.add_cargo_under_path(&root_path, 0).unwrap();
        n.add_cargo_under_path(&root_child_path, 1).unwrap();
        n.add_cargo_under_path(&root_child_path, 2).unwrap();
        n.add_cargo_under_path(&root_child_path, 3).unwrap();

        root_child_path = n.add_cargo_under_path(&root_path, 0).unwrap();
        n.add_cargo_under_path(&root_child_path, 4).unwrap();
        n.add_cargo_under_path(&root_child_path, 5).unwrap();
        n.add_cargo_under_path(&root_child_path, 6).unwrap();

        root_child_path = n.add_cargo_under_path(&root_path, 0).unwrap();
        n.add_cargo_under_path(&root_child_path, 7).unwrap();
        n.add_cargo_under_path(&root_child_path, 8).unwrap();
        n.add_cargo_under_path(&root_child_path, 9).unwrap();

        let sum = n.traverse_back(
            0,
            |accum, nd, _path| {
                if nd.cargo > 6 {
                    TraverseAction::SkipChildren
                } else {
                    *accum += nd.cargo;
                    TraverseAction::Continue
                }
            }
        );

        assert_eq!(21, sum);
    }

    #[test]
    fn node_traverse_back_skip_children_on_first_child() {
        let mut n = Node::new(0);   
        let root_path = n.get_first_path();

        let mut root_child_path = n.add_cargo_under_path(&root_path, 0).unwrap();
        n.add_cargo_under_path(&root_child_path, 1).unwrap();
        n.add_cargo_under_path(&root_child_path, 2).unwrap();
        n.add_cargo_under_path(&root_child_path, 3).unwrap();

        root_child_path = n.add_cargo_under_path(&root_path, 0).unwrap();
        n.add_cargo_under_path(&root_child_path, 4).unwrap();
        n.add_cargo_under_path(&root_child_path, 5).unwrap();
        n.add_cargo_under_path(&root_child_path, 6).unwrap();

        root_child_path = n.add_cargo_under_path(&root_path, 0).unwrap();
        n.add_cargo_under_path(&root_child_path, 7).unwrap();
        n.add_cargo_under_path(&root_child_path, 8).unwrap();
        n.add_cargo_under_path(&root_child_path, 9).unwrap();

        let sum = n.traverse_back(
            0,
            |accum, nd, _path| {
                if nd.cargo == 4 {
                    TraverseAction::SkipChildren
                } else {
                    *accum += nd.cargo;
                    TraverseAction::Continue
                }
            }
        );

        assert_eq!(41, sum);
    }

    #[test]
    fn node_traverse_back_skip_children_on_lone_root() {
        let n = Node::new('A');

        let count_nodes = n.traverse_back(
            0usize,
            |accum, _nd, _path| {
                *accum += 1;

                TraverseAction::SkipChildren
            }
        );

        assert_eq!(1usize, count_nodes);
    }

    #[test]
    fn node_set_cargo_on_root() {
        let mut n = Node::new('a');
        n.add_cargo_under_path(&vec![], 'b').unwrap();
        n.add_cargo_under_path(&vec![], 'c').unwrap();

        let result = n.set_cargo(&vec![], 'z');
        assert!(result.is_ok());
        assert_eq!(&'z', n.borrow_cargo(&vec![]).unwrap());
    }

    #[test]
    fn node_set_cargo_wrong_path() {
        let mut n = Node::new('a');
        n.add_cargo_under_path(&vec![], 'b').unwrap();
        n.add_cargo_under_path(&vec![], 'c').unwrap();

        let result = n.set_cargo(&vec![1, 3], 'z');
        assert!(result.is_err());
        assert_eq!((PathError::InputPathNotFound, 'z'), result.unwrap_err());
    }

    #[test]
    fn node_set_cargo() {
        let mut n = Node::new('a');
        n.add_cargo_under_path(&vec![], 'b').unwrap();
        n.add_cargo_under_path(&vec![], 'c').unwrap();

        let result = n.set_cargo(&vec![1], 'z');
        assert!(result.is_ok());
        assert_eq!(&'z', n.borrow_cargo(&vec![1]).unwrap());
    }

    #[test]
    #[ignore]
    fn node_50_000_subnodes() {
        let mut n = Node::new(0usize);
        let total_siblings = 10usize;
        let total_nodes = total_siblings * 5000usize;
        let mut siblings_count = 1usize;
        let mut path = Vec::<usize>::new();

        for crg in 0usize..total_nodes {
            if siblings_count < total_siblings {
                n.add_cargo_under_path(&path, crg).unwrap();
                siblings_count += 1;
            } else {
                path = n.add_cargo_under_path(&path, crg).unwrap();
                siblings_count = 1;
            }
        }
        
        // traverse is faster than iter().fold.
        let outcome = n.traverse(
            0usize,
            |acc, _crg, _path| {
                *acc += 1;
                TraverseAction::Continue
            }
        );

        assert_eq!(total_nodes + 1, outcome);
        assert_eq!(total_nodes / total_siblings, path.len());

        // Outcome :
        // - test lasts ca. 35.7 seconds using n.iter().fold();
        // - test lasts ca. 33.2 seconds using n.traverse();
        // - test lasts ca.  8.3 seconds without any of these, so just for building the tree
        // holding 50,001 nodes.
    }

    #[test]
    fn node_clone_clonable_cargo() {
        let mut n = Node::new(0u8);
        let mut path: Vec<usize> = Vec::new();

        for i in 1u8..4 {
            path = n.add_cargo_under_path(&vec![], i).unwrap();
        }

        for i in 4u8..7 {
            n.add_cargo_under_path(&path, i).unwrap();
        }

        let orig_total = n.traverse(
            0u8,
            |accum, nd, _path| {
                *accum += nd.cargo;
                TraverseAction::Continue
            }
        );

        assert_eq!(21u8, orig_total);

        let mut nc = n.clone();

        let mut clone_total = nc.traverse(
            0u8,
            |accum, nd, _path| {
                *accum += nd.cargo;
                TraverseAction::Continue
            }
        );

        assert_eq!(orig_total, clone_total);

        // Add 1 to all cargoes of the clone.
        let clone_node_count = nc.traverse_mut(
            0u8,
            |accum, nd, _path| {
                *accum += 1u8;
                nd.cargo += 1u8;
                TraverseAction::Continue
            }
        );

        assert_eq!(7u8, clone_node_count);

        clone_total = nc.traverse(
            0u8,
            |accum, nd, _path| {
                *accum += nd.cargo;
                TraverseAction::Continue
            }
        );

        assert_eq!(orig_total + clone_node_count, clone_total);

        // Check if values of original haven't changed.

        let new_orig_total = n.traverse(
            0u8,
            |accum, nd, _path| {
                *accum += nd.cargo;
                TraverseAction::Continue
            }
        );

        assert_eq!(orig_total, new_orig_total);
    }

    /*
    #[test]
    fn node_clone_non_clonable_cargo() {
        #[derive(Debug)]
        struct NoClone {}

        let mut n = Node::new(NoClone{});
        n.add_cargo_under_path(&Vec::<usize>::new(), NoClone{}).unwrap();

        // The below statement doesn't even compile, quod erat demonstrandum :
        // error[E0599]: the method `clone` exists for struct `Node<NoClone>`, but its trait bounds were not satisfied
        // let nc = n.clone();
    }
    */

    #[test]
    fn node_traverse_mut_change_children() {
        let mut n = Node::new(0u8);
        let root_path = n.get_first_path();

        for i in 1u8..4 {
            n.add_cargo_under_path(&root_path, i).unwrap();
        }

        n.traverse_mut(
            0u8,
            |_accum, nd, _path| {
                if nd.cargo == 2 {
                    nd.add_cargo_under_path(&root_path, 4u8).unwrap();
                }
                TraverseAction::Continue
            }
        );

        assert_eq!(&4u8, n.borrow_cargo(&vec![1usize, 0usize]).unwrap());
    }

    #[test]
    fn node_swap_cargo_non_root() {

        #[derive(Debug)]
        struct NoCopy {
            value: u8,
        }

        let mut n = Node::new(NoCopy{value: 10});
        let added_path = n.add_cargo_under_path(&vec![], NoCopy{value: 1}).unwrap();
        n.add_cargo_under_path(&vec![], NoCopy{value: 7}).unwrap();

        n.add_cargo_under_path(&added_path, NoCopy{value: 2}).unwrap();
        n.add_cargo_under_path(&added_path, NoCopy{value: 3}).unwrap();

        let mut total = n.traverse(
            0u8,
            |accum, nd, _path| {
                *accum += nd.cargo.value;
                TraverseAction::Continue
            }
        );

        assert_eq!(23u8, total);

        let swap_result = Node::swap_cargo(n, &added_path, NoCopy{value: 30u8});
        assert!(swap_result.is_ok());

        let old_cargo: NoCopy;
        (n, old_cargo) = swap_result.unwrap();
        assert_eq!(1, old_cargo.value);

        total = n.traverse(
            0u8,
            |accum, nd, _path| {
                *accum += nd.cargo.value;
                TraverseAction::Continue
            }
        );

        assert_eq!(52u8, total);
    }

    #[test]
    fn node_swap_cargo_root() {

        #[derive(Debug)]
        struct NoCopy {
            value: u8,
        }

        let mut n = Node::new(NoCopy{value: 10});
        let added_path = n.add_cargo_under_path(&vec![], NoCopy{value: 1}).unwrap();
        n.add_cargo_under_path(&vec![], NoCopy{value: 7}).unwrap();

        n.add_cargo_under_path(&added_path, NoCopy{value: 2}).unwrap();
        n.add_cargo_under_path(&added_path, NoCopy{value: 3}).unwrap();

        let mut total = n.traverse(
            0u8,
            |accum, nd, _path| {
                *accum += nd.cargo.value;
                TraverseAction::Continue
            }
        );

        assert_eq!(23u8, total);

        let swap_result = Node::swap_cargo(n, &vec![], NoCopy{value: 30u8});
        assert!(swap_result.is_ok());

        let old_cargo: NoCopy;
        (n, old_cargo) = swap_result.unwrap();
        assert_eq!(10, old_cargo.value);

        total = n.traverse(
            0u8,
            |accum, nd, _path| {
                *accum += nd.cargo.value;
                TraverseAction::Continue
            }
        );

        assert_eq!(43u8, total);
    }

    #[test]
    fn node_swap_cargo_wrong_path() {
        let mut n = Node::new('g');
        n.add_cargo_under_path(&vec![], 'o').unwrap();

        let result = Node::swap_cargo(n, &vec![5], 'a');
        assert!(result.is_err());
        
        let (_, n, old_cargo) = result.unwrap_err();
        assert_eq!(&'g', n.borrow_cargo(&vec![]).unwrap());
        assert_eq!('a', old_cargo);
    }

    // Testing some assumptions about vector comparisons.
    mod vec_partialeq {
        #[test]
        fn vec_same_len() {
            let v1:Vec<usize> = vec![1, 2, 3, 1];
            let v2:Vec<usize> = vec![1, 2, 4, 0];

            assert!(v1 < v2);
        }

        #[test]
        fn vec_unequal_size() {
            let v1:Vec<usize> = vec![8, 9];
            let v2:Vec<usize> = vec![8, 8, 10];

            assert!(v1 > v2);
        }

        #[test]
        fn vec_unequal_size_same_elements() {
            let v1:Vec<usize> = vec![8, 9];
            let v2:Vec<usize> = vec![8, 9, 0];

            assert!(v1 < v2);
        }
    }
}