commonware-parallel 2026.10.0

Parallelize fold operations with pluggable execution strategies.
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
//! Parallel collection operations that split a vector of items over the calling worker's pool.
//!
//! Each operation drains its vector through a producer that can be split at any index, and feeds
//! the items to a consumer that splits at the same index and reduces the results of its two
//! halves. [`bridge_producer_consumer`] splits both in half with [`join_context`] while its
//! splitter allows, folds each remaining piece sequentially, and reduces the halves of every split
//! in order, so outputs keep the order of their items and reductions follow the tree of splits.
//!
//! The splitter starts with an allowance equal to the pool's thread count, splits while that
//! allowance is positive, and halves it at every split. When the second half runs as a queued
//! job, usually on a thread that stole it, its allowance is reset to at least the thread count
//! so the work can be divided again for idle threads.
//!
//! A consumer can report that it is full, after an error or once it takes no more items. The
//! bridge checks this before splitting or folding a piece, and folders check it between items, so
//! a piece that observes it stops early. The items that no piece consumed are dropped.
//!
//! Each operation composes the producer, consumers, folders, and reducers that rayon builds for
//! the parallel iterator expression named in its documentation.

#![allow(
    clippy::disallowed_types,
    reason = "Preserve upstream synchronization semantics."
)]

use super::{SendPtr, current_num_threads, join::join_context};
use std::{
    collections::LinkedList,
    iter,
    marker::PhantomData,
    mem, ptr, slice,
    sync::{
        Mutex,
        atomic::{AtomicBool, Ordering},
    },
};

/// Maps every item with `map` and collects the outputs in the order of their items.
///
/// This is rayon's `items.into_par_iter().map(map).collect::<Vec<_>>()`.
///
/// # Panics
///
/// Panics if the calling thread is not a worker of a thread pool. A panic of `map` is resumed
/// once every other piece has finished, after every output and every item `map` did not take
/// has been dropped.
pub(crate) fn map_collect_vec<I: Send, T: Send>(
    items: Vec<I>,
    map: &(impl Fn(I) -> T + Sync),
) -> Vec<T> {
    let mut vec = Vec::new();
    let len = items.len();
    collect_with_consumer(&mut vec, len, |consumer| {
        bridge(items, MapConsumer::new(consumer, map))
    });
    vec
}

/// Maps every item with `map` and collects the outputs in the order of their items, or returns
/// an error `map` returned.
///
/// An error stops the operation early. The failing piece stops at its error, and other pieces
/// check for a failure before they start and between items. All outputs and unmapped items are
/// dropped before the error is returned. If several items fail, which of their errors is returned
/// is not specified.
///
/// This is rayon's `items.into_par_iter().map(map).collect::<Result<Vec<_>, _>>()`.
///
/// # Panics
///
/// Panics if the calling thread is not a worker of a thread pool. A panic of `map` is resumed
/// once every other piece has finished, after every output and every item `map` did not take
/// has been dropped.
pub(crate) fn try_map_collect_vec<I: Send, T: Send, E: Send>(
    items: Vec<I>,
    map: &(impl Fn(I) -> Result<T, E> + Sync),
) -> Result<Vec<T>, E> {
    let saved_error = Mutex::new(None);
    let list = {
        let ok = ok(&saved_error);
        let full = AtomicBool::new(false);
        let consumer = WhileSomeConsumer {
            base: ListVecConsumer,
            full: &full,
        };
        bridge(
            items,
            MapConsumer::new(MapConsumer::new(consumer, &ok), map),
        )
    };

    if let Some(error) = saved_error.into_inner().unwrap() {
        // Drop the outputs while the error is a local, so a panicking output destructor also drops
        // the error.
        drop(list);
        return Err(error);
    }

    // This works like `extend`, but `Vec::append` is more efficient.
    let mut collection = Vec::with_capacity(list.iter().map(Vec::len).sum());
    for mut other in list {
        collection.append(&mut other);
    }
    Ok(collection)
}

/// Maps every item with `map` and a state that `init` creates for each piece, and collects the
/// outputs in the order of their items.
///
/// This is rayon's `items.into_par_iter().map_init(init, map).collect::<Vec<_>>()`.
///
/// # Panics
///
/// Panics if the calling thread is not a worker of a thread pool. A panic of `init` or `map` is
/// resumed once every other piece has finished, after every output and every item `map` did not
/// take has been dropped.
pub(crate) fn map_init_collect_vec<I: Send, S: Send, T: Send>(
    items: Vec<I>,
    init: &(impl Fn() -> S + Sync),
    map: &(impl Fn(&mut S, I) -> T + Sync),
) -> Vec<T> {
    let mut vec = Vec::new();
    let len = items.len();
    collect_with_consumer(&mut vec, len, |consumer| {
        bridge(items, MapInitConsumer::new(consumer, init, map))
    });
    vec
}

/// Folds the items of each piece into `identity()` with `fold` and a state that `init` creates
/// for the piece, and reduces the pieces' accumulators with `reduce` in the order of their items.
///
/// Each piece reduces its accumulator into another `identity()` before the pieces are reduced.
///
/// This is rayon's `items.into_par_iter().fold(|| (init(), identity()), ..).map(|(_, acc)|
/// acc).reduce(identity, reduce)`, where the fold applies `fold` to the accumulator and the
/// state.
///
/// # Panics
///
/// Panics if the calling thread is not a worker of a thread pool. A panic of a closure is resumed
/// once every other piece has finished, after every accumulator and every item `fold` did not
/// take has been dropped.
pub(crate) fn fold_init<I: Send, S: Send, R: Send>(
    items: Vec<I>,
    init: &(impl Fn() -> S + Sync),
    identity: &(impl Fn() -> R + Sync),
    fold: &(impl Fn(R, &mut S, I) -> R + Sync),
    reduce: &(impl Fn(R, R) -> R + Sync),
) -> R {
    let fold_identity = || (init(), identity());
    let fold_op = |(mut init_val, acc): (S, R), item: I| {
        let new_acc = fold(acc, &mut init_val, item);
        (init_val, new_acc)
    };
    let map_op = |(_, acc): (S, R)| acc;
    let consumer = FoldConsumer {
        base: MapConsumer::new(
            ReduceConsumer {
                identity,
                reduce_op: reduce,
            },
            &map_op,
        ),
        fold_op: &fold_op,
        identity: &fold_identity,
    };
    bridge(items, consumer)
}

/// Folds the items of each piece into `identity()` with `fold` until it returns an error, and
/// reduces the pieces' accumulators with `reduce` in the order of their items, or returns an
/// error `fold` returned.
///
/// Each piece reduces its accumulator into another `identity()` before the pieces are reduced.
/// A piece stops at its first error. Once a failed piece has finished, each other piece checks
/// for the failure before it starts and after each item it folds, and stops once it observes it.
/// The items no piece folded are dropped. If several pieces fail, which of their errors is
/// returned is not specified.
///
/// This is rayon's `items.into_par_iter().try_fold(identity, fold).try_reduce(identity, |a, b|
/// Ok(reduce(a, b)))`.
///
/// # Panics
///
/// Panics if the calling thread is not a worker of a thread pool. A panic of a closure is resumed
/// once every other piece has finished, after every accumulator and every item `fold` did not
/// take has been dropped.
pub(crate) fn try_fold<I: Send, R: Send, E: Send>(
    items: Vec<I>,
    identity: &(impl Fn() -> R + Sync),
    fold: &(impl Fn(R, I) -> Result<R, E> + Sync),
    reduce: &(impl Fn(R, R) -> R + Sync),
) -> Result<R, E> {
    let reduce_op = |a: R, b: R| -> Result<R, E> { Ok(reduce(a, b)) };
    let full = AtomicBool::new(false);
    let consumer = TryFoldConsumer {
        base: TryReduceConsumer {
            identity,
            reduce_op: &reduce_op,
            full: &full,
        },
        identity,
        fold_op: fold,
    };
    bridge(items, consumer)
}

/// A consumer is effectively a generalized "fold" operation, and in fact each consumer will
/// eventually be converted into a [`Folder`]. What makes a consumer special is that, like a
/// producer, it can be **split** into multiple consumers using the `split_at` method. When a
/// consumer is split, it produces two consumers, as well as a **reducer**. The two consumers can
/// be fed items independently, and when they are done the reducer is used to combine their two
/// results into one.
trait Consumer<Item>: Send + Sized {
    /// The type of folder that this consumer can be converted into.
    type Folder: Folder<Item, Result = Self::Result>;

    /// The type of reducer that is produced if this consumer is split.
    type Reducer: Reducer<Self::Result>;

    /// The type of result that this consumer will ultimately produce.
    type Result: Send;

    /// Divide the consumer into two consumers, one processing items `0..index` and one
    /// processing items from `index..`. Also produces a reducer that can be used to reduce the
    /// results at the end.
    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer);

    /// Convert the consumer into a folder that can consume items sequentially, eventually
    /// producing a final result.
    fn into_folder(self) -> Self::Folder;

    /// Hint whether this `Consumer` would like to stop processing further items, e.g. if a
    /// search has been completed.
    fn full(&self) -> bool;
}

/// The `Folder` trait encapsulates the standard fold operation. It can be fed many items using
/// the `consume` method. At the end, once all items have been consumed, it can then be converted
/// (using `complete`) into a final value.
trait Folder<Item>: Sized {
    /// The type of result that will ultimately be produced by the folder.
    type Result;

    /// Consume next item and return new sequential state.
    fn consume(self, item: Item) -> Self;

    /// Consume items from the iterator until full, and return new sequential state.
    ///
    /// This method is **optional**. The default simply iterates over `iter`, invoking `consume`
    /// and checking after each iteration whether `full` returns false.
    ///
    /// The main reason to override it is if you can provide a more specialized, efficient
    /// implementation.
    fn consume_iter<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = Item>,
    {
        for item in iter {
            self = self.consume(item);
            if self.full() {
                break;
            }
        }
        self
    }

    /// Finish consuming items, produce final result.
    fn complete(self) -> Self::Result;

    /// Hint whether this `Folder` would like to stop processing further items, e.g. if a search
    /// has been completed.
    fn full(&self) -> bool;
}

/// The reducer is the final step of a `Consumer`. After a consumer has been split into two parts,
/// and each of those parts has been fully processed, we are left with two results. The reducer is
/// then used to combine those two results into one.
trait Reducer<Result> {
    /// Reduce two final results into one. This is executed after a split.
    fn reduce(self, left: Result, right: Result) -> Result;
}

/// A splitter controls the policy for splitting into smaller work items.
///
/// Thief-splitting is an adaptive policy that starts by splitting into enough jobs for every
/// worker thread, and then resets itself whenever a job is actually stolen into a different
/// thread.
#[derive(Clone, Copy)]
struct Splitter {
    /// The `splits` tell us approximately how many remaining times we'd like to split this job.
    /// We always just divide it by two though, so the effective number of pieces will be
    /// `next_power_of_two()`.
    splits: usize,
}

impl Splitter {
    #[inline]
    fn new() -> Self {
        Self {
            splits: current_num_threads(),
        }
    }

    #[inline]
    fn try_split(&mut self, stolen: bool) -> bool {
        let Self { splits } = *self;

        if stolen {
            // This job was stolen!  Reset the number of desired splits to the
            // thread count, if that's more than we had remaining anyway.
            self.splits = Ord::max(current_num_threads(), self.splits / 2);
            true
        } else if splits > 0 {
            // We have splits remaining, make it so.
            self.splits /= 2;
            true
        } else {
            // Not stolen, and no more splits, so we're done!
            false
        }
    }
}

/// The length splitter is built on thief-splitting, but additionally takes into account the
/// remaining length of the iterator.
#[derive(Clone, Copy)]
struct LengthSplitter {
    inner: Splitter,

    /// The smallest we're willing to divide into.
    min: usize,
}

impl LengthSplitter {
    /// Creates a new splitter based on lengths.
    ///
    /// The `min` is a hard lower bound.  We'll never split below that, but of course an iterator
    /// might start out smaller already.
    ///
    /// The `max` is an upper bound on the working size, used to determine the minimum number of
    /// times we need to split to get under that limit. The adaptive algorithm may very well split
    /// even further, but never smaller than the `min`.
    #[inline]
    fn new(min: usize, max: usize, len: usize) -> Self {
        let mut splitter = Self {
            inner: Splitter::new(),
            min: Ord::max(min, 1),
        };

        // Divide the given length by the max working length to get the minimum
        // number of splits we need to get under that max.  This rounds down,
        // but the splitter actually gives `next_power_of_two()` pieces anyway.
        // e.g. len 12345 / max 100 = 123 min_splits -> 128 pieces.
        let min_splits = len / Ord::max(max, 1);

        // Only update the value if it's not splitting enough already.
        if min_splits > splitter.inner.splits {
            splitter.inner.splits = min_splits;
        }

        splitter
    }

    #[inline]
    fn try_split(&mut self, len: usize, stolen: bool) -> bool {
        // If splitting wouldn't make us too small, try the inner splitter.
        len / 2 >= self.min && self.inner.try_split(stolen)
    }
}

/// Drains every item of `vec` into a producer and passes it to `callback`. The producer moves
/// or drops each item, and the vector then only frees its buffer.
fn with_producer<T: Send, R>(
    mut vec: Vec<T>,
    callback: impl FnOnce(DrainProducer<'_, T>) -> R,
) -> R {
    let len = vec.len();

    // SAFETY: The vector forgets its `len` initialized items before the producer takes them over,
    // so the producer is their only owner, and the vector, whose length stays zero, never reads
    // them again.
    unsafe {
        // Make the vector forget about the drained items.
        vec.set_len(0);

        // Create the producer as the exclusive "owner" of the slice.
        let producer = DrainProducer::from_vec(&mut vec, len);

        // The producer will move or drop each item from the drained range.
        callback(producer)
    }
}

/// Drains every item of `vec` into `consumer`, splitting and creating parallel tasks as needed.
fn bridge<T: Send, C: Consumer<T>>(vec: Vec<T>, consumer: C) -> C::Result {
    let len = vec.len();
    with_producer(vec, |producer| {
        bridge_producer_consumer(len, producer, consumer)
    })
}

/// This helper function is used to "connect" a producer and a consumer. This function will draw
/// items from `producer` and feed them to `consumer`, splitting and creating parallel tasks when
/// needed.
fn bridge_producer_consumer<T: Send, C: Consumer<T>>(
    len: usize,
    producer: DrainProducer<'_, T>,
    consumer: C,
) -> C::Result {
    // A drain splits down to single items and has no maximum piece length.
    let splitter = LengthSplitter::new(1, usize::MAX, len);
    return helper(len, false, splitter, producer, consumer);

    fn helper<T: Send, C: Consumer<T>>(
        len: usize,
        migrated: bool,
        mut splitter: LengthSplitter,
        producer: DrainProducer<'_, T>,
        consumer: C,
    ) -> C::Result {
        if consumer.full() {
            consumer.into_folder().complete()
        } else if splitter.try_split(len, migrated) {
            let mid = len / 2;
            let (left_producer, right_producer) = producer.split_at(mid);
            let (left_consumer, right_consumer, reducer) = consumer.split_at(mid);
            let (left_result, right_result) = join_context(
                |context| {
                    helper(
                        mid,
                        context.migrated(),
                        splitter,
                        left_producer,
                        left_consumer,
                    )
                },
                |context| {
                    helper(
                        len - mid,
                        context.migrated(),
                        splitter,
                        right_producer,
                        right_consumer,
                    )
                },
            );
            reducer.reduce(left_result, right_result)
        } else {
            producer.fold_with(consumer.into_folder()).complete()
        }
    }
}

/// A producer that moves the items out of a slice, splitting into producers of its halves until
/// it is converted into an iterator. Items it has not produced are dropped with it.
struct DrainProducer<'data, T: Send> {
    slice: &'data mut [T],
}

impl<'data, T: Send> DrainProducer<'data, T> {
    /// Creates a draining producer, which *moves* items from the slice.
    ///
    /// Unsafe because `!Copy` data must not be read after the borrow is released.
    const unsafe fn new(slice: &'data mut [T]) -> Self {
        Self { slice }
    }

    /// Creates a draining producer, which *moves* items from the tail of the vector.
    ///
    /// Unsafe because we're moving from beyond `vec.len()`, so the caller must ensure that data
    /// is initialized and not read after the borrow is released.
    unsafe fn from_vec(vec: &'data mut Vec<T>, len: usize) -> Self {
        let start = vec.len();
        assert!(vec.capacity() - start >= len);

        // SAFETY: The assert keeps the `len` items after `start` within the allocation, and the
        // caller ensures they are initialized and not read after the borrow is released. The
        // pointer is derived from `Vec` directly, not through a `Deref`, so it has provenance
        // over the whole allocation.
        unsafe {
            let ptr = vec.as_mut_ptr().add(start);
            Self::new(slice::from_raw_parts_mut(ptr, len))
        }
    }

    /// Split into two producers, one producing items `0..index` and the other `index..N`.
    /// Index must be less than or equal to `N`.
    fn split_at(mut self, index: usize) -> (Self, Self) {
        // replace the slice so we don't drop it twice
        let slice = mem::take(&mut self.slice);
        let (left, right) = slice.split_at_mut(index);

        // SAFETY: The halves partition the slice this producer owned, so each item still has one
        // producer that moves or drops it.
        unsafe { (Self::new(left), Self::new(right)) }
    }

    /// Iterate the producer, feeding each element to `folder`, and stop when the folder is full
    /// (or all elements have been consumed).
    fn fold_with<F>(self, folder: F) -> F
    where
        F: Folder<T>,
    {
        folder.consume_iter(self)
    }
}

impl<'data, T: 'data + Send> IntoIterator for DrainProducer<'data, T> {
    type Item = T;
    type IntoIter = SliceDrain<'data, T>;

    /// Convert `self` into an iterator. At this point, no more parallel splits are possible.
    fn into_iter(mut self) -> Self::IntoIter {
        // replace the slice so we don't drop it twice
        let slice = mem::take(&mut self.slice);
        SliceDrain {
            iter: slice.iter_mut(),
        }
    }
}

impl<'data, T: 'data + Send> Drop for DrainProducer<'data, T> {
    fn drop(&mut self) {
        // extract the slice so we can use `Drop for [T]`
        let slice_ptr: *mut [T] = mem::take::<&'data mut [T]>(&mut self.slice);

        // SAFETY: The producer owns the items left in its slice, which nothing reads afterwards,
        // and taking the slice leaves the producer empty, so each item drops once.
        unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
    }
}

/// Like `std::vec::Drain`, without updating a source `Vec`.
struct SliceDrain<'data, T> {
    iter: slice::IterMut<'data, T>,
}

impl<'data, T: 'data> Iterator for SliceDrain<'data, T> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        // Coerce the pointer early, so we don't keep the
        // reference that's about to be invalidated.
        let ptr: *const T = self.iter.next()?;

        // SAFETY: The drain owns the items of its iterator, and the iterator has moved past this
        // one, so neither it nor the drop of the drain reads it again.
        Some(unsafe { ptr::read(ptr) })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }

    fn count(self) -> usize {
        self.iter.len()
    }
}

impl<'data, T: 'data> DoubleEndedIterator for SliceDrain<'data, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        // Coerce the pointer early, so we don't keep the
        // reference that's about to be invalidated.
        let ptr: *const T = self.iter.next_back()?;

        // SAFETY: The drain owns the items of its iterator, and the iterator has moved past this
        // one, so neither it nor the drop of the drain reads it again.
        Some(unsafe { ptr::read(ptr) })
    }
}

impl<'data, T: 'data> ExactSizeIterator for SliceDrain<'data, T> {
    fn len(&self) -> usize {
        self.iter.len()
    }
}

impl<'data, T: 'data> iter::FusedIterator for SliceDrain<'data, T> {}

impl<'data, T: 'data> Drop for SliceDrain<'data, T> {
    fn drop(&mut self) {
        // extract the iterator so we can use `Drop for [T]`
        let slice_ptr: *mut [T] = mem::replace(&mut self.iter, [].iter_mut()).into_slice();

        // SAFETY: The items the iterator has not yielded are owned by the drain and were never
        // read, and the empty iterator left in place keeps them from dropping twice.
        unsafe { ptr::drop_in_place::<[T]>(slice_ptr) };
    }
}

/// Create a consumer on the slice of memory we are collecting into.
///
/// The consumer needs to be used inside the scope function, and the complete collect result
/// passed back.
///
/// This method will verify the collect result, and panic if the slice was not fully written
/// into. Otherwise, in the successful case, the vector is complete with the collected result.
fn collect_with_consumer<T, F>(vec: &mut Vec<T>, len: usize, scope_fn: F)
where
    T: Send,
    F: FnOnce(CollectConsumer<'_, T>) -> CollectResult<'_, T>,
{
    // Reserve space for `len` more elements in the vector.
    vec.reserve(len);

    // Create the consumer and run the callback for collection.
    let result = scope_fn(CollectConsumer::appender(vec, len));

    // The `CollectResult` represents a contiguous part of the slice, that has
    // been written to. On unwind here, the `CollectResult` will be dropped. If
    // some producers on the way did not produce enough elements, partial
    // `CollectResult`s may have been dropped without being reduced to the final
    // result, and we will see that as the length coming up short.
    //
    // Here, we assert that added length is fully initialized. This is checked
    // by the following assert, which verifies if a complete `CollectResult`
    // was produced. If the length is correct, it is necessarily covering the
    // target slice. Since we know that the consumer cannot have escaped from
    // `scope_fn` (by parametricity, essentially), we know that any stores that
    // will happen, have happened. Unless some code is buggy, that means we
    // should have seen `len` total writes.
    let actual_writes = result.len();
    assert!(
        actual_writes == len,
        "expected {len} total writes, but got {actual_writes}"
    );

    // Release the result's mutable borrow and "proxy ownership"
    // of the elements, before the vector takes it over.
    result.release_ownership();

    let new_len = vec.len() + len;

    // SAFETY: The complete result covered the `len` slots after the vector's length, so they are
    // initialized, and releasing the result handed their ownership to the vector.
    unsafe {
        vec.set_len(new_len);
    }
}

/// Writes the items of its range in order into the spare capacity of a vector.
struct CollectConsumer<'c, T: Send> {
    /// See `CollectResult` for explanation of why this is not a slice
    start: SendPtr<T>,
    len: usize,
    marker: PhantomData<&'c mut T>,
}

impl<'c, T: Send + 'c> CollectConsumer<'c, T> {
    /// Create a collector for `len` items in the unused capacity of the vector.
    fn appender(vec: &'c mut Vec<T>, len: usize) -> Self {
        let start = vec.len();
        assert!(vec.capacity() - start >= len);

        // SAFETY: We already made sure to have the additional space allocated.
        // The pointer is derived from `Vec` directly, not through a `Deref`,
        // so it has provenance over the whole allocation.
        unsafe { Self::new(vec.as_mut_ptr().add(start), len) }
    }

    /// The target memory is considered uninitialized, and will be
    /// overwritten without reading or dropping existing values.
    const unsafe fn new(start: *mut T, len: usize) -> Self {
        Self {
            start: SendPtr(start),
            len,
            marker: PhantomData,
        }
    }
}

/// CollectResult represents an initialized part of the target slice.
///
/// This is a proxy owner of the elements in the slice. When it drops,
/// the elements will be dropped, unless its ownership is released before then.
#[must_use]
struct CollectResult<'c, T> {
    /// This pointer and length has the same representation as a slice,
    /// but retains the provenance of the entire array so that we can merge
    /// these regions together in `CollectReducer`.
    start: SendPtr<T>,
    total_len: usize,
    /// The current initialized length after `start`
    initialized_len: usize,
    /// Lifetime invariance guarantees that the data flows from consumer to result,
    /// especially for the `scope_fn` callback in `collect_with_consumer`.
    invariant_lifetime: PhantomData<&'c mut &'c mut [T]>,
}

// SAFETY: The result owns the initialized elements of its part of the slice, so sending it sends
// those elements, which are `Send`.
unsafe impl<'c, T> Send for CollectResult<'c, T> where T: Send {}

impl<'c, T> CollectResult<'c, T> {
    /// The current length of the collect result
    const fn len(&self) -> usize {
        self.initialized_len
    }

    /// Release ownership of the slice of elements, and return the length
    fn release_ownership(mut self) -> usize {
        let ret = self.initialized_len;
        self.initialized_len = 0;
        ret
    }
}

impl<'c, T> Drop for CollectResult<'c, T> {
    fn drop(&mut self) {
        // SAFETY: Drop the first `self.initialized_len` elements, which have been recorded to be
        // initialized by the folder and are owned by this result until it releases its ownership,
        // which zeroes the length.
        unsafe {
            ptr::drop_in_place(ptr::slice_from_raw_parts_mut(
                self.start.0,
                self.initialized_len,
            ));
        }
    }
}

impl<'c, T: Send + 'c> Consumer<T> for CollectConsumer<'c, T> {
    type Folder = CollectResult<'c, T>;
    type Reducer = CollectReducer;
    type Result = CollectResult<'c, T>;

    fn split_at(self, index: usize) -> (Self, Self, CollectReducer) {
        let CollectConsumer { start, len, .. } = self;

        // Produce new consumers.
        // SAFETY: This assert checks that `index` is a valid offset for `start`
        unsafe {
            assert!(index <= len);
            (
                Self::new(start.0, index),
                Self::new(start.0.add(index), len - index),
                CollectReducer,
            )
        }
    }

    fn into_folder(self) -> Self::Folder {
        // Create a result/folder that consumes values and writes them
        // into the region after start. The initial result has length 0.
        CollectResult {
            start: self.start,
            total_len: self.len,
            initialized_len: 0,
            invariant_lifetime: PhantomData,
        }
    }

    fn full(&self) -> bool {
        false
    }
}

impl<'c, T: Send + 'c> Folder<T> for CollectResult<'c, T> {
    type Result = Self;

    fn consume(mut self, item: T) -> Self {
        assert!(
            self.initialized_len < self.total_len,
            "too many values pushed to consumer"
        );

        // SAFETY: The assert above is a bounds check for this write, and we
        // avoid assignment here so we do not drop an uninitialized T.
        unsafe {
            // Write item and increase the initialized length
            self.start.0.add(self.initialized_len).write(item);
            self.initialized_len += 1;
        }

        self
    }

    fn complete(self) -> Self::Result {
        // We don't explicitly check that the local writes were complete,
        // but `collect_with_consumer` asserts the total result length in the end.
        self
    }

    fn full(&self) -> bool {
        false
    }
}

/// CollectReducer combines adjacent chunks. The result must always
/// be contiguous so that it is one combined slice.
struct CollectReducer;

impl<'c, T> Reducer<CollectResult<'c, T>> for CollectReducer {
    fn reduce(
        self,
        mut left: CollectResult<'c, T>,
        right: CollectResult<'c, T>,
    ) -> CollectResult<'c, T> {
        // Merge if the CollectResults are adjacent and in left to right order.
        // Otherwise, drop the right piece now, and the total length will end up
        // short when the correctness of the collected result is asserted.
        //
        // SAFETY: The initialized elements of `left` lie within its part of the slice, so the
        // offset past them stays within the allocation.
        unsafe {
            let left_end = left.start.0.add(left.initialized_len);
            if left_end == right.start.0 {
                left.total_len += right.total_len;
                left.initialized_len += right.release_ownership();
            }
            left
        }
    }
}

/// Collects the items of each piece into a vector and links the pieces' vectors in order.
struct ListVecConsumer;

struct ListVecFolder<T> {
    vec: Vec<T>,
}

impl<T: Send> Consumer<T> for ListVecConsumer {
    type Folder = ListVecFolder<T>;
    type Reducer = ListReducer;
    type Result = LinkedList<Vec<T>>;

    fn split_at(self, _index: usize) -> (Self, Self, Self::Reducer) {
        (Self, Self, ListReducer)
    }

    fn into_folder(self) -> Self::Folder {
        ListVecFolder { vec: Vec::new() }
    }

    fn full(&self) -> bool {
        false
    }
}

impl<T> Folder<T> for ListVecFolder<T> {
    type Result = LinkedList<Vec<T>>;

    fn consume(mut self, item: T) -> Self {
        self.vec.push(item);
        self
    }

    fn consume_iter<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        self.vec.extend(iter);
        self
    }

    fn complete(self) -> Self::Result {
        let mut list = LinkedList::new();
        if !self.vec.is_empty() {
            list.push_back(self.vec);
        }
        list
    }

    fn full(&self) -> bool {
        false
    }
}

struct ListReducer;

impl<T> Reducer<LinkedList<T>> for ListReducer {
    fn reduce(self, mut left: LinkedList<T>, mut right: LinkedList<T>) -> LinkedList<T> {
        left.append(&mut right);
        left
    }
}

/// Maps each item with `map_op` before passing it on.
struct MapConsumer<'f, C, F> {
    base: C,
    map_op: &'f F,
}

impl<'f, C, F> MapConsumer<'f, C, F> {
    const fn new(base: C, map_op: &'f F) -> Self {
        Self { base, map_op }
    }
}

impl<'f, T, R, C, F> Consumer<T> for MapConsumer<'f, C, F>
where
    C: Consumer<F::Output>,
    F: Fn(T) -> R + Sync,
    R: Send,
{
    type Folder = MapFolder<'f, C::Folder, F>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            MapConsumer::new(left, self.map_op),
            MapConsumer::new(right, self.map_op),
            reducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        MapFolder {
            base: self.base.into_folder(),
            map_op: self.map_op,
        }
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

struct MapFolder<'f, C, F> {
    base: C,
    map_op: &'f F,
}

impl<'f, T, R, C, F> Folder<T> for MapFolder<'f, C, F>
where
    C: Folder<F::Output>,
    F: Fn(T) -> R,
{
    type Result = C::Result;

    fn consume(self, item: T) -> Self {
        let mapped_item = (self.map_op)(item);
        MapFolder {
            base: self.base.consume(mapped_item),
            map_op: self.map_op,
        }
    }

    fn consume_iter<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        self.base = self.base.consume_iter(iter.into_iter().map(self.map_op));
        self
    }

    fn complete(self) -> C::Result {
        self.base.complete()
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

/// Maps each item with `map_op` and a state that `init` creates for each folder before passing
/// it on.
struct MapInitConsumer<'f, C, INIT, F> {
    base: C,
    init: &'f INIT,
    map_op: &'f F,
}

impl<'f, C, INIT, F> MapInitConsumer<'f, C, INIT, F> {
    const fn new(base: C, init: &'f INIT, map_op: &'f F) -> Self {
        Self { base, init, map_op }
    }
}

impl<'f, T, INIT, U, R, C, F> Consumer<T> for MapInitConsumer<'f, C, INIT, F>
where
    C: Consumer<R>,
    INIT: Fn() -> U + Sync,
    F: Fn(&mut U, T) -> R + Sync,
    R: Send,
{
    type Folder = MapWithFolder<'f, C::Folder, U, F>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            MapInitConsumer::new(left, self.init, self.map_op),
            MapInitConsumer::new(right, self.init, self.map_op),
            reducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        MapWithFolder {
            base: self.base.into_folder(),
            item: (self.init)(),
            map_op: self.map_op,
        }
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

struct MapWithFolder<'f, C, U, F> {
    base: C,
    item: U,
    map_op: &'f F,
}

impl<'f, T, U, R, C, F> Folder<T> for MapWithFolder<'f, C, U, F>
where
    C: Folder<R>,
    F: Fn(&mut U, T) -> R,
{
    type Result = C::Result;

    fn consume(mut self, item: T) -> Self {
        let mapped_item = (self.map_op)(&mut self.item, item);
        self.base = self.base.consume(mapped_item);
        self
    }

    fn consume_iter<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        fn with<'f, T, U, R>(
            item: &'f mut U,
            map_op: impl Fn(&mut U, T) -> R + 'f,
        ) -> impl FnMut(T) -> R + 'f {
            move |x| map_op(item, x)
        }

        {
            let mapped_iter = iter.into_iter().map(with(&mut self.item, self.map_op));
            self.base = self.base.consume_iter(mapped_iter);
        }
        self
    }

    fn complete(self) -> C::Result {
        self.base.complete()
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

/// Turns an `Err` into `None` after saving its error, unless another error was saved first.
fn ok<T, E>(saved: &Mutex<Option<E>>) -> impl Fn(Result<T, E>) -> Option<T> + '_ {
    move |item| match item {
        Ok(item) => Some(item),
        Err(error) => {
            // We don't need a blocking `lock()`, as anybody
            // else holding the lock will also be writing
            // `Some(error)`, and then ours is irrelevant.
            if let Ok(mut guard) = saved.try_lock()
                && guard.is_none()
            {
                *guard = Some(error);
            }
            None
        }
    }
}

/// Passes on the items of `Some` and stops every piece at the first `None`.
struct WhileSomeConsumer<'f, C> {
    base: C,
    full: &'f AtomicBool,
}

impl<'f, T, C> Consumer<Option<T>> for WhileSomeConsumer<'f, C>
where
    C: Consumer<T>,
    T: Send,
{
    type Folder = WhileSomeFolder<'f, C::Folder>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            WhileSomeConsumer { base: left, ..self },
            WhileSomeConsumer {
                base: right,
                ..self
            },
            reducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        WhileSomeFolder {
            base: self.base.into_folder(),
            full: self.full,
        }
    }

    fn full(&self) -> bool {
        self.full.load(Ordering::Relaxed) || self.base.full()
    }
}

struct WhileSomeFolder<'f, C> {
    base: C,
    full: &'f AtomicBool,
}

impl<'f, T, C> Folder<Option<T>> for WhileSomeFolder<'f, C>
where
    C: Folder<T>,
{
    type Result = C::Result;

    fn consume(mut self, item: Option<T>) -> Self {
        match item {
            Some(item) => self.base = self.base.consume(item),
            None => self.full.store(true, Ordering::Relaxed),
        }
        self
    }

    fn consume_iter<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = Option<T>>,
    {
        fn some<T>(full: &AtomicBool) -> impl Fn(&Option<T>) -> bool + '_ {
            move |x| match *x {
                Some(_) => !full.load(Ordering::Relaxed),
                None => {
                    full.store(true, Ordering::Relaxed);
                    false
                }
            }
        }

        self.base = self.base.consume_iter(
            iter.into_iter()
                .take_while(some(self.full))
                .map(Option::unwrap),
        );
        self
    }

    fn complete(self) -> C::Result {
        self.base.complete()
    }

    fn full(&self) -> bool {
        self.full.load(Ordering::Relaxed) || self.base.full()
    }
}

/// Folds the items of each folder into `identity()` with `fold_op`, and passes on the
/// accumulator when the folder completes.
struct FoldConsumer<'c, C, ID, F> {
    base: C,
    fold_op: &'c F,
    identity: &'c ID,
}

impl<'r, U, T, C, ID, F> Consumer<T> for FoldConsumer<'r, C, ID, F>
where
    C: Consumer<U>,
    F: Fn(U, T) -> U + Sync,
    ID: Fn() -> U + Sync,
    U: Send,
{
    type Folder = FoldFolder<'r, C::Folder, U, F>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            FoldConsumer { base: left, ..self },
            FoldConsumer {
                base: right,
                ..self
            },
            reducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        FoldFolder {
            base: self.base.into_folder(),
            item: (self.identity)(),
            fold_op: self.fold_op,
        }
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

struct FoldFolder<'r, C, ID, F> {
    base: C,
    fold_op: &'r F,
    item: ID,
}

impl<'r, C, ID, F, T> Folder<T> for FoldFolder<'r, C, ID, F>
where
    C: Folder<ID>,
    F: Fn(ID, T) -> ID + Sync,
{
    type Result = C::Result;

    fn consume(self, item: T) -> Self {
        let item = (self.fold_op)(self.item, item);
        FoldFolder {
            base: self.base,
            fold_op: self.fold_op,
            item,
        }
    }

    fn consume_iter<I>(self, iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        fn not_full<C, ID, T>(base: &C) -> impl Fn(&T) -> bool + '_
        where
            C: Folder<ID>,
        {
            move |_| !base.full()
        }

        let base = self.base;
        let item = iter
            .into_iter()
            // stop iterating if another thread has finished
            .take_while(not_full(&base))
            .fold(self.item, self.fold_op);

        FoldFolder {
            base,
            item,
            fold_op: self.fold_op,
        }
    }

    fn complete(self) -> C::Result {
        self.base.consume(self.item).complete()
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

/// Reduces the items of each folder into `identity()` with `reduce_op`, and reduces the results
/// of split consumers with `reduce_op`.
struct ReduceConsumer<'r, R, ID> {
    identity: &'r ID,
    reduce_op: &'r R,
}

impl<'r, R, ID> Copy for ReduceConsumer<'r, R, ID> {}

impl<'r, R, ID> Clone for ReduceConsumer<'r, R, ID> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<'r, R, ID, T> Consumer<T> for ReduceConsumer<'r, R, ID>
where
    R: Fn(T, T) -> T + Sync,
    ID: Fn() -> T + Sync,
    T: Send,
{
    type Folder = ReduceFolder<'r, R, T>;
    type Reducer = Self;
    type Result = T;

    fn split_at(self, _index: usize) -> (Self, Self, Self) {
        (self, self, self)
    }

    fn into_folder(self) -> Self::Folder {
        ReduceFolder {
            reduce_op: self.reduce_op,
            item: (self.identity)(),
        }
    }

    fn full(&self) -> bool {
        false
    }
}

impl<'r, R, ID, T> Reducer<T> for ReduceConsumer<'r, R, ID>
where
    R: Fn(T, T) -> T + Sync,
{
    fn reduce(self, left: T, right: T) -> T {
        (self.reduce_op)(left, right)
    }
}

struct ReduceFolder<'r, R, T> {
    reduce_op: &'r R,
    item: T,
}

impl<'r, R, T> Folder<T> for ReduceFolder<'r, R, T>
where
    R: Fn(T, T) -> T,
{
    type Result = T;

    fn consume(self, item: T) -> Self {
        ReduceFolder {
            reduce_op: self.reduce_op,
            item: (self.reduce_op)(self.item, item),
        }
    }

    fn consume_iter<I>(self, iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        ReduceFolder {
            reduce_op: self.reduce_op,
            item: iter.into_iter().fold(self.item, self.reduce_op),
        }
    }

    fn complete(self) -> T {
        self.item
    }

    fn full(&self) -> bool {
        false
    }
}

/// Folds the items of each folder into `identity()` with `fold_op` until it returns an error,
/// and passes on the accumulator or the error when the folder completes.
struct TryFoldConsumer<'c, C, ID, F> {
    base: C,
    identity: &'c ID,
    fold_op: &'c F,
}

impl<'r, R, E, T, C, ID, F> Consumer<T> for TryFoldConsumer<'r, C, ID, F>
where
    C: Consumer<Result<R, E>>,
    F: Fn(R, T) -> Result<R, E> + Sync,
    ID: Fn() -> R + Sync,
    R: Send,
    E: Send,
{
    type Folder = TryFoldFolder<'r, C::Folder, R, E, F>;
    type Reducer = C::Reducer;
    type Result = C::Result;

    fn split_at(self, index: usize) -> (Self, Self, Self::Reducer) {
        let (left, right, reducer) = self.base.split_at(index);
        (
            TryFoldConsumer { base: left, ..self },
            TryFoldConsumer {
                base: right,
                ..self
            },
            reducer,
        )
    }

    fn into_folder(self) -> Self::Folder {
        TryFoldFolder {
            base: self.base.into_folder(),
            control: Ok((self.identity)()),
            fold_op: self.fold_op,
        }
    }

    fn full(&self) -> bool {
        self.base.full()
    }
}

struct TryFoldFolder<'r, C, R, E, F> {
    base: C,
    fold_op: &'r F,
    control: Result<R, E>,
}

impl<'r, C, R, E, F, T> Folder<T> for TryFoldFolder<'r, C, R, E, F>
where
    C: Folder<Result<R, E>>,
    F: Fn(R, T) -> Result<R, E> + Sync,
{
    type Result = C::Result;

    fn consume(mut self, item: T) -> Self {
        let fold_op = self.fold_op;
        if let Ok(acc) = self.control {
            self.control = fold_op(acc, item);
        }
        self
    }

    fn complete(self) -> C::Result {
        self.base.consume(self.control).complete()
    }

    fn full(&self) -> bool {
        match self.control {
            Err(_) => true,
            _ => self.base.full(),
        }
    }
}

/// Reduces the items of each folder into `identity()` with `reduce_op`, and the results of split
/// consumers with `reduce_op`, until an error. An error marks every consumer split from the same
/// one full and is passed on in place of any result, the left one if both halves failed.
struct TryReduceConsumer<'r, R, ID> {
    identity: &'r ID,
    reduce_op: &'r R,
    full: &'r AtomicBool,
}

impl<'r, R, ID> Copy for TryReduceConsumer<'r, R, ID> {}

impl<'r, R, ID> Clone for TryReduceConsumer<'r, R, ID> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<'r, R, ID, T, E> Consumer<Result<T, E>> for TryReduceConsumer<'r, R, ID>
where
    R: Fn(T, T) -> Result<T, E> + Sync,
    ID: Fn() -> T + Sync,
    T: Send,
    E: Send,
{
    type Folder = TryReduceFolder<'r, R, T, E>;
    type Reducer = Self;
    type Result = Result<T, E>;

    fn split_at(self, _index: usize) -> (Self, Self, Self) {
        (self, self, self)
    }

    fn into_folder(self) -> Self::Folder {
        TryReduceFolder {
            reduce_op: self.reduce_op,
            control: Ok((self.identity)()),
            full: self.full,
        }
    }

    fn full(&self) -> bool {
        self.full.load(Ordering::Relaxed)
    }
}

impl<'r, R, ID, T, E> Reducer<Result<T, E>> for TryReduceConsumer<'r, R, ID>
where
    R: Fn(T, T) -> Result<T, E> + Sync,
{
    fn reduce(self, left: Result<T, E>, right: Result<T, E>) -> Result<T, E> {
        match (left, right) {
            (Ok(left), Ok(right)) => (self.reduce_op)(left, right),
            (Err(r), _) | (_, Err(r)) => Err(r),
        }
    }
}

struct TryReduceFolder<'r, R, T, E> {
    reduce_op: &'r R,
    control: Result<T, E>,
    full: &'r AtomicBool,
}

impl<'r, R, T, E> Folder<Result<T, E>> for TryReduceFolder<'r, R, T, E>
where
    R: Fn(T, T) -> Result<T, E>,
{
    type Result = Result<T, E>;

    fn consume(mut self, item: Result<T, E>) -> Self {
        let reduce_op = self.reduce_op;
        self.control = match (self.control, item) {
            (Ok(left), Ok(right)) => reduce_op(left, right),
            (control @ Err(_), _) | (_, control @ Err(_)) => control,
        };
        if self.control.is_err() {
            self.full.store(true, Ordering::Relaxed);
        }
        self
    }

    fn complete(self) -> Result<T, E> {
        self.control
    }

    fn full(&self) -> bool {
        match self.control {
            Err(_) => true,
            _ => self.full.load(Ordering::Relaxed),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rayon::{ThreadPool, ThreadPoolBuilder, current_thread_index, join};
    use commonware_utils::{sync::Mutex, test_rng};
    use rand::{Rng as _, RngExt as _};
    use std::{
        fmt,
        panic::{self, AssertUnwindSafe},
        sync::atomic::AtomicUsize,
        thread::{self, Result as ThreadResult},
        time::{Duration, Instant},
    };

    const LENGTHS: [usize; 6] = [0, 1, 2, 7, 1000, 100_000];

    fn pool(threads: usize) -> ThreadPool {
        ThreadPoolBuilder::new()
            .num_threads(threads)
            .build()
            .unwrap()
    }

    /// Runs `op` on a worker of a new pool of `threads` threads.
    fn in_pool<R: Send>(threads: usize, op: impl FnOnce() -> R + Send) -> R {
        pool(threads).install(op)
    }

    /// Waits for `condition` to hold, failing after 10 seconds.
    fn wait_until(condition: impl Fn() -> bool) {
        let start = Instant::now();
        while !condition() {
            assert!(
                start.elapsed() < Duration::from_secs(10),
                "the condition must hold within 10 seconds"
            );
            thread::sleep(Duration::from_millis(1));
        }
    }

    /// Appends the items of `b` to `a`, a reduction that keeps the order of its items.
    fn append<T>(mut a: Vec<T>, b: Vec<T>) -> Vec<T> {
        a.extend(b);
        a
    }

    /// Counts, for each index of a test's items, the calls on the item, the drops of the item,
    /// and the outputs made from the item and their drops.
    struct Ledger {
        calls: Vec<AtomicUsize>,
        inputs: Vec<AtomicUsize>,
        created: Vec<AtomicUsize>,
        outputs: Vec<AtomicUsize>,
    }

    /// An item that counts its drop in a ledger.
    struct Input<'a> {
        index: usize,
        ledger: &'a Ledger,
    }

    impl Drop for Input<'_> {
        fn drop(&mut self) {
            self.ledger.inputs[self.index].fetch_add(1, Ordering::SeqCst);
        }
    }

    /// An output that counts its drop in a ledger.
    struct Output<'a> {
        index: usize,
        ledger: &'a Ledger,
    }

    impl Drop for Output<'_> {
        fn drop(&mut self) {
            self.ledger.outputs[self.index].fetch_add(1, Ordering::SeqCst);
        }
    }

    impl Ledger {
        fn new(len: usize) -> Self {
            let counters = || (0..len).map(|_| AtomicUsize::new(0)).collect();
            Self {
                calls: counters(),
                inputs: counters(),
                created: counters(),
                outputs: counters(),
            }
        }

        fn inputs(&self) -> Vec<Input<'_>> {
            (0..self.calls.len())
                .map(|index| Input {
                    index,
                    ledger: self,
                })
                .collect()
        }

        /// Records a call on `input` and returns its index.
        fn call(&self, input: &Input<'_>) -> usize {
            self.calls[input.index].fetch_add(1, Ordering::SeqCst);
            input.index
        }

        fn output(&self, index: usize) -> Output<'_> {
            self.created[index].fetch_add(1, Ordering::SeqCst);
            Output {
                index,
                ledger: self,
            }
        }

        /// Returns the indices of the items that were called on.
        fn called(&self) -> Vec<usize> {
            (0..self.calls.len())
                .filter(|&index| self.calls[index].load(Ordering::SeqCst) > 0)
                .collect()
        }

        /// Asserts that every item was called on at most once and dropped exactly once, and that
        /// every output made was dropped exactly once.
        fn assert_dropped_once(&self) {
            for index in 0..self.calls.len() {
                let calls = self.calls[index].load(Ordering::SeqCst);
                assert!(
                    calls <= 1,
                    "item {index} must be called at most once, got {calls} calls"
                );
                let drops = self.inputs[index].load(Ordering::SeqCst);
                assert_eq!(drops, 1, "item {index} must be dropped exactly once");
                let created = self.created[index].load(Ordering::SeqCst);
                assert!(
                    created <= calls,
                    "item {index} must make at most one output per call, made {created}"
                );
                let drops = self.outputs[index].load(Ordering::SeqCst);
                assert_eq!(
                    drops, created,
                    "each output of item {index} must be dropped exactly once"
                );
            }
        }
    }

    /// Maps keep the order of their items at every length and pool size.
    #[test]
    fn maps_keep_the_order_of_their_items() {
        let mut rng = test_rng();
        for threads in [1, 2, 4] {
            let pool = pool(threads);
            for len in LENGTHS {
                // Map random numbers to strings, so outputs own their memory
                let items: Vec<u64> = (0..len).map(|_| rng.next_u64()).collect();
                let expected: Vec<String> = items.iter().map(u64::to_string).collect();

                // Each operation returns the sequential result
                let mapped = pool.install(|| map_collect_vec(items.clone(), &|x| x.to_string()));
                assert_eq!(mapped, expected);
                let mapped = pool.install(|| {
                    map_init_collect_vec(items.clone(), &|| 0usize, &|seen, x| {
                        *seen += 1;
                        x.to_string()
                    })
                });
                assert_eq!(mapped, expected);
                let mapped = pool.install(|| {
                    try_map_collect_vec(items.clone(), &|x| Ok::<_, ()>(x.to_string()))
                });
                assert_eq!(mapped, Ok(expected));
            }
        }
    }

    /// Folds reduce their pieces in the order of their items, so a reduction that is associative
    /// but not commutative returns the sequential result.
    #[test]
    fn folds_reduce_in_the_order_of_their_items() {
        let mut rng = test_rng();
        let concat = |mut a: String, b: String| {
            a.push_str(&b);
            a
        };
        for threads in [1, 2, 4] {
            let pool = pool(threads);
            for len in LENGTHS {
                // Concatenate random words of one to three letters
                let items: Vec<String> = (0..len)
                    .map(|_| {
                        (0..rng.random_range(1..4))
                            .map(|_| char::from(b'a' + rng.random_range(0..26u8)))
                            .collect()
                    })
                    .collect();
                let expected = items.concat();

                // Each operation returns the sequential concatenation
                let folded = pool.install(|| {
                    fold_init(
                        items.clone(),
                        &|| 0usize,
                        &String::new,
                        &|mut acc, seen, item| {
                            *seen += 1;
                            acc.push_str(&item);
                            acc
                        },
                        &concat,
                    )
                });
                assert_eq!(folded, expected);
                let folded = pool.install(|| {
                    try_fold(
                        items.clone(),
                        &String::new,
                        &|mut acc, item| {
                            acc.push_str(&item);
                            Ok::<_, ()>(acc)
                        },
                        &concat,
                    )
                });
                assert_eq!(folded, Ok(expected));
            }
        }
    }

    /// On one thread the bridge splits once, each piece reduces its accumulator into an identity,
    /// and the pieces reduce left to right, so a reduction that records its grouping returns
    /// `((left)(right))`. A piece of fewer than two items is not split.
    #[test]
    fn reductions_follow_the_tree_of_splits() {
        let pool = pool(1);
        let group = |a: String, b: String| format!("({a}{b})");
        for len in [0, 1, 2, 7, 1000] {
            let items: Vec<String> = (0..len).map(|i| format!("{i},")).collect();
            let mid = len / 2;
            let (expected, pieces) = if len < 2 {
                (format!("({})", items.concat()), 1)
            } else {
                let (left, right) = items.split_at(mid);
                (format!("(({})({}))", left.concat(), right.concat()), 2)
            };

            // The fold creates one state per piece
            let states = AtomicUsize::new(0);
            let folded = pool.install(|| {
                fold_init(
                    items.clone(),
                    &|| states.fetch_add(1, Ordering::SeqCst),
                    &String::new,
                    &|mut acc, _, item| {
                        acc.push_str(&item);
                        acc
                    },
                    &group,
                )
            });
            assert_eq!(folded, expected);
            assert_eq!(states.load(Ordering::SeqCst), pieces);

            // The fallible fold groups its pieces the same way
            let folded = pool.install(|| {
                try_fold(
                    items.clone(),
                    &String::new,
                    &|mut acc, item| {
                        acc.push_str(&item);
                        Ok::<_, ()>(acc)
                    },
                    &group,
                )
            });
            assert_eq!(folded, Ok(expected));
        }
    }

    /// The halves a worker keeps halve their allowance of splits, while a half another worker
    /// steals splits again from the thread count.
    ///
    /// On two threads, the first worker splits the items into halves and its half into quarters,
    /// which it maps as two pieces. The second worker is held until the first finished a piece,
    /// then steals the second half, and the first worker holds its second piece until the second
    /// worker mapped that half, so neither worker steals from the other while it maps.
    #[test]
    fn a_stolen_half_splits_again() {
        const LEN: usize = 1024;

        // A piece records its worker and length when it finishes
        struct Piece<'a> {
            worker: usize,
            ordinal: usize,
            len: usize,
            finished: &'a [AtomicUsize; 2],
            pieces: &'a Mutex<Vec<(usize, usize)>>,
        }
        impl Drop for Piece<'_> {
            fn drop(&mut self) {
                self.pieces.lock().push((self.worker, self.len));
                self.finished[self.worker].fetch_add(1, Ordering::SeqCst);
            }
        }

        // Count the pieces each worker started and finished, and the items it mapped
        let started = [AtomicUsize::new(0), AtomicUsize::new(0)];
        let finished = [AtomicUsize::new(0), AtomicUsize::new(0)];
        let mapped = [AtomicUsize::new(0), AtomicUsize::new(0)];
        let pieces = Mutex::new(Vec::new());

        // Map on the first worker while its join holds the second
        let (first, outputs) = pool(2).install(|| {
            let first = current_thread_index().unwrap();
            let second = 1 - first;
            let (outputs, ()) = join(
                || {
                    map_init_collect_vec(
                        (0..LEN).collect(),
                        &|| {
                            let worker = current_thread_index().unwrap();
                            Piece {
                                worker,
                                ordinal: started[worker].fetch_add(1, Ordering::SeqCst),
                                len: 0,
                                finished: &finished,
                                pieces: &pieces,
                            }
                        },
                        &|piece, item| {
                            if piece.worker == first && piece.ordinal == 1 && piece.len == 0 {
                                wait_until(|| mapped[second].load(Ordering::SeqCst) == LEN / 2);
                            }
                            piece.len += 1;
                            mapped[piece.worker].fetch_add(1, Ordering::SeqCst);
                            item
                        },
                    )
                },
                || wait_until(|| finished[first].load(Ordering::SeqCst) >= 1),
            );
            (first, outputs)
        });

        // The outputs keep their order, the first worker mapped two quarters, and the second
        // split its half into eight pieces
        assert_eq!(outputs, (0..LEN).collect::<Vec<_>>());
        let pieces = pieces.into_inner();
        let lens = |worker| {
            pieces
                .iter()
                .filter(|&&(w, _)| w == worker)
                .map(|&(_, len)| len)
                .collect::<Vec<_>>()
        };
        assert_eq!(lens(first), [LEN / 4; 2]);
        assert_eq!(lens(1 - first), [LEN / 16; 8]);
    }

    /// On one thread, an error stops the first half at the failing item and skips the second
    /// half. On more threads, the error of one of several failing items is returned. Every item
    /// and every output is dropped exactly once.
    #[test]
    fn try_map_stops_at_an_error_and_drops_every_item_once() {
        const LEN: usize = 1000;
        for (threads, errors) in [(1, &[10][..]), (2, &[10, 500, 999]), (4, &[10, 500, 999])] {
            // Map every item to an output, or fail at the error indices
            let ledger = Ledger::new(LEN);
            let result = in_pool(threads, || {
                try_map_collect_vec(ledger.inputs(), &|input| {
                    let index = ledger.call(&input);
                    if errors.contains(&index) {
                        Err(index)
                    } else {
                        Ok(ledger.output(index))
                    }
                })
            });

            // The error is returned and every item and output dropped once
            let error = result.err().expect("an item must fail");
            assert!(errors.contains(&error));
            if threads == 1 {
                assert_eq!(ledger.called(), (0..=10).collect::<Vec<_>>());
            }
            ledger.assert_dropped_once();
        }

        // Without an error, the outputs come back in order
        let ledger = Ledger::new(LEN);
        let outputs = in_pool(4, || {
            try_map_collect_vec(ledger.inputs(), &|input| {
                Ok::<_, ()>(ledger.output(ledger.call(&input)))
            })
        })
        .unwrap();
        assert!(outputs.iter().map(|output| output.index).eq(0..LEN));
        drop(outputs);
        ledger.assert_dropped_once();
    }

    /// A destructor panic releases the remaining outputs and the saved error exactly once.
    #[test]
    fn try_map_drops_every_item_when_an_output_drop_panics() {
        struct PanickingOutput<'a> {
            output: Output<'a>,
            panic_at: usize,
        }

        impl Drop for PanickingOutput<'_> {
            fn drop(&mut self) {
                assert_ne!(
                    self.output.index, self.panic_at,
                    "output index must differ from the panic index"
                );
            }
        }

        const LEN: usize = 16;
        for panic_at in [0, LEN / 2, LEN - 2] {
            let ledger = Ledger::new(LEN);
            let result = panic::catch_unwind(AssertUnwindSafe(|| {
                // A single worker fills both halves before the last item fails.
                in_pool(1, || {
                    let _ = try_map_collect_vec(ledger.inputs(), &|input| {
                        let index = ledger.call(&input);
                        if index == LEN - 1 {
                            Err(input)
                        } else {
                            Ok(PanickingOutput {
                                output: ledger.output(index),
                                panic_at,
                            })
                        }
                    });
                });
            }));

            assert_is_panic_with_message(&result, "output index must differ from the panic index");
            assert_eq!(ledger.called(), (0..LEN).collect::<Vec<_>>());
            ledger.assert_dropped_once();
        }
    }

    /// On one thread, an error stops the first half at the failing item and skips the second
    /// half. On more threads, the error of one of several failing items is returned. Every item,
    /// including the items the accumulators hold, is dropped exactly once.
    #[test]
    fn try_fold_stops_at_an_error_and_drops_every_item_once() {
        const LEN: usize = 1000;
        for (threads, errors) in [(1, &[10][..]), (2, &[10, 500, 999]), (4, &[10, 500, 999])] {
            // Collect every item into the accumulators, or fail at the error indices
            let ledger = Ledger::new(LEN);
            let result = in_pool(threads, || {
                try_fold(
                    ledger.inputs(),
                    &Vec::new,
                    &|mut acc, input| {
                        let index = ledger.call(&input);
                        if errors.contains(&index) {
                            return Err(index);
                        }
                        acc.push(input);
                        Ok(acc)
                    },
                    &append,
                )
            });

            // The error is returned and every item dropped once
            let error = result.err().expect("an item must fail");
            assert!(errors.contains(&error));
            if threads == 1 {
                assert_eq!(ledger.called(), (0..=10).collect::<Vec<_>>());
            }
            ledger.assert_dropped_once();
        }

        // Without an error, the items come back in order
        let ledger = Ledger::new(LEN);
        let items = in_pool(4, || {
            try_fold(
                ledger.inputs(),
                &Vec::new,
                &|mut acc, input| {
                    ledger.call(&input);
                    acc.push(input);
                    Ok::<_, ()>(acc)
                },
                &append,
            )
        })
        .unwrap();
        assert!(items.iter().map(|input| input.index).eq(0..LEN));
        drop(items);
        ledger.assert_dropped_once();
    }

    /// A panic of a map on one item propagates to the caller of `install` once every other piece
    /// finished, and every item and every output is dropped exactly once. On one thread, the rest
    /// of the panicking half is skipped and the other half is mapped.
    #[test]
    fn a_panicking_map_drops_every_item_and_output_once() {
        const LEN: usize = 1000;
        type Map = for<'a> fn(&'a Ledger, Vec<Input<'a>>, usize);
        let maps: [Map; 3] = [
            |ledger, inputs, panic_at| {
                map_collect_vec(inputs, &|input| {
                    let index = ledger.call(&input);
                    assert_ne!(index, panic_at, "map panicked");
                    ledger.output(index)
                });
            },
            |ledger, inputs, panic_at| {
                map_init_collect_vec(inputs, &|| (), &|_, input| {
                    let index = ledger.call(&input);
                    assert_ne!(index, panic_at, "map panicked");
                    ledger.output(index)
                });
            },
            |ledger, inputs, panic_at| {
                let _ = try_map_collect_vec(inputs, &|input| {
                    let index = ledger.call(&input);
                    assert_ne!(index, panic_at, "map panicked");
                    Ok::<_, ()>(ledger.output(index))
                });
            },
        ];
        for map in maps {
            for threads in [1, 2, 4] {
                for panic_at in [0, 10, LEN / 2, LEN - 1] {
                    // Map on a pool and catch the panic its caller resumes
                    let ledger = Ledger::new(LEN);
                    let result = panic::catch_unwind(AssertUnwindSafe(|| {
                        in_pool(threads, || map(&ledger, ledger.inputs(), panic_at));
                    }));
                    assert_is_panic_with_message(&result, "map panicked");

                    // On one thread the other half was still mapped, and every item and output
                    // was dropped once
                    if threads == 1 && panic_at < LEN / 2 {
                        let expected: Vec<_> = (0..=panic_at).chain(LEN / 2..LEN).collect();
                        assert_eq!(ledger.called(), expected);
                    }
                    ledger.assert_dropped_once();
                }
            }
        }
    }

    /// A panic of a fold propagates to the caller of `install`, and every item, including the
    /// items the accumulators hold, is dropped exactly once.
    #[test]
    fn a_panicking_fold_drops_every_item_once() {
        const LEN: usize = 1000;
        for threads in [1, 2, 4] {
            for panic_at in [0, 10, LEN / 2, LEN - 1] {
                // Collect every item into the accumulators until one panics
                let ledger = Ledger::new(LEN);
                let result = panic::catch_unwind(AssertUnwindSafe(|| {
                    in_pool(threads, || {
                        fold_init(
                            ledger.inputs(),
                            &|| (),
                            &Vec::new,
                            &|mut acc, _, input| {
                                assert_ne!(ledger.call(&input), panic_at, "fold panicked");
                                acc.push(input);
                                acc
                            },
                            &append,
                        )
                    });
                }));
                assert_is_panic_with_message(&result, "fold panicked");
                ledger.assert_dropped_once();

                // The fallible fold drops its items the same way
                let ledger = Ledger::new(LEN);
                let result = panic::catch_unwind(AssertUnwindSafe(|| {
                    let _ = in_pool(threads, || {
                        try_fold(
                            ledger.inputs(),
                            &Vec::new,
                            &|mut acc, input| {
                                assert_ne!(ledger.call(&input), panic_at, "fold panicked");
                                acc.push(input);
                                Ok::<_, ()>(acc)
                            },
                            &append,
                        )
                    });
                }));
                assert_is_panic_with_message(&result, "fold panicked");
                ledger.assert_dropped_once();
            }
        }
    }

    /// Without a pool to split over, every operation refuses a caller outside every pool, and
    /// drops its items.
    #[test]
    fn operations_require_a_worker() {
        let operations: [fn(&Ledger); 5] = [
            |ledger| {
                map_collect_vec(ledger.inputs(), &|input| ledger.call(&input));
            },
            |ledger| {
                let _ =
                    try_map_collect_vec(ledger.inputs(), &|input| Ok::<_, ()>(ledger.call(&input)));
            },
            |ledger| {
                map_init_collect_vec(ledger.inputs(), &|| (), &|_, input| ledger.call(&input));
            },
            |ledger| {
                fold_init(
                    ledger.inputs(),
                    &|| (),
                    &|| 0,
                    &|acc, _, input| acc + ledger.call(&input),
                    &|a, b| a + b,
                );
            },
            |ledger| {
                let _ = try_fold(
                    ledger.inputs(),
                    &|| 0,
                    &|acc, input| Ok::<_, ()>(acc + ledger.call(&input)),
                    &|a, b| a + b,
                );
            },
        ];
        for operation in operations {
            // Each operation panics before calling on an item, and drops every item
            let ledger = Ledger::new(8);
            let result = panic::catch_unwind(AssertUnwindSafe(|| operation(&ledger)));
            assert_is_panic_with_message(
                &result,
                "the calling thread must be a worker of a thread pool",
            );
            assert!(ledger.called().is_empty());
            ledger.assert_dropped_once();
        }
    }

    #[test]
    fn execute() {
        let a: Vec<i32> = (0..1024).collect();
        let b = in_pool(4, || map_collect_vec(a, &|i| i + 1));
        let c: Vec<i32> = (0..1024).map(|i| i + 1).collect();
        assert_eq!(b, c);
    }

    #[test]
    fn check_move() {
        let a = vec![vec![1, 2, 3]];
        let ptr = a[0].as_ptr();

        let b = in_pool(4, || map_collect_vec(a, &|v| v));

        // a simple move means the inner vec will be completely unchanged
        assert_eq!(ptr, b[0].as_ptr());
    }

    #[test]
    fn check_drops() {
        #[derive(Clone)]
        struct DropCounter<'a>(&'a AtomicUsize);
        impl Drop for DropCounter<'_> {
            fn drop(&mut self) {
                self.0.fetch_add(1, Ordering::Relaxed);
            }
        }

        let c = AtomicUsize::new(0);
        let a = vec![DropCounter(&c); 10];

        let b = in_pool(4, || map_collect_vec(a.clone(), &|x| x));
        assert_eq!(c.load(Ordering::Relaxed), 0);

        // A producer drops the items it did not produce
        with_producer(b, |_| ());
        assert_eq!(c.load(Ordering::Relaxed), 10);

        // So do its halves and their iterators
        with_producer(a, |producer| {
            let (a, _) = producer.split_at(5);
            a.into_iter().next();
        });
        assert_eq!(c.load(Ordering::Relaxed), 20);
    }

    /// Stress-test indexes for `DrainProducer::split_at`.
    #[commonware_macros::test_group("miri")]
    #[test]
    fn drain_producer_split_at() {
        fn check_len<I: ExactSizeIterator>(iter: &I, len: usize) {
            assert_eq!(iter.size_hint(), (len, Some(len)));
            assert_eq!(iter.len(), len);
        }

        let v: Vec<_> = (0..10).collect();
        let end = v.len() + 1;
        for i in 0..end {
            for j in i..end {
                for k in j..end {
                    for reverse in [false, true] {
                        let result = with_producer(v.clone(), |producer| {
                            // Splitting the outer indexes first gets us an arbitrary mid section,
                            // which we then split further to get full test coverage.
                            let (left, d) = producer.split_at(k);
                            let (a, mid) = left.split_at(i);
                            let (b, c) = mid.split_at(j - i);

                            let a = a.into_iter();
                            let b = b.into_iter();
                            let c = c.into_iter();
                            let d = d.into_iter();

                            check_len(&a, i);
                            check_len(&b, j - i);
                            check_len(&c, k - j);

                            let chain = a.chain(b).chain(c).chain(d);
                            if reverse {
                                chain.rev().collect::<Vec<_>>()
                            } else {
                                chain.collect()
                            }
                        });
                        if reverse {
                            assert!(result.iter().eq(v.iter().rev()));
                        } else {
                            assert_eq!(result, v);
                        }
                    }
                }
            }
        }
    }

    /// A producer, its halves, and their iterators drop every item they did not produce.
    #[commonware_macros::test_group("miri")]
    #[test]
    fn drain_producer_drops_unproduced_items() {
        let items = || (0..10).map(|i| i.to_string()).collect::<Vec<_>>();

        // Dropping the producer drops every item
        with_producer(items(), |_| ());

        // Dropping a half and a partly consumed iterator drops the items they hold
        let produced = with_producer(items(), |producer| {
            let (left, right) = producer.split_at(4);
            let (_, mid) = right.split_at(3);
            let mut iter = mid.into_iter();
            let first = iter.next();
            let last = iter.next_back();
            drop(left);
            (first, last)
        });
        assert_eq!(produced, (Some("7".to_string()), Some("9".to_string())));
    }

    #[test]
    fn par_iter_collect_result() {
        in_pool(4, || {
            let a: Result<Vec<_>, ()> = (0_i32..2048).map(Ok).collect();
            let b: Result<Vec<_>, ()> = try_map_collect_vec((0_i32..2048).collect(), &Ok);
            assert_eq!(a, b);

            let c: Result<Vec<_>, _> = try_map_collect_vec((0_i32..2048).collect(), &|x| {
                if x == 1234 { Err(x) } else { Ok(x) }
            });
            assert_eq!(c, Err(1234));

            let d: Result<Vec<_>, _> = try_map_collect_vec((0_i32..2048).collect(), &|x| {
                if x % 100 == 99 { Err(x) } else { Ok(x) }
            });
            assert_eq!(d.map_err(|x| x % 100), Err(99));
        });
    }

    #[test]
    fn collect_drop_on_unwind() {
        struct Recorddrop<'a>(i64, &'a Mutex<Vec<i64>>);

        impl Drop for Recorddrop<'_> {
            fn drop(&mut self) {
                self.1.lock().push(self.0);
            }
        }

        let test_collect_panic = |will_panic: bool| {
            let test_vec_len = 1024;
            let panic_point = 740;

            let mut inserts = Mutex::new(Vec::new());
            let mut drops = Mutex::new(Vec::new());

            let a = (0..test_vec_len).collect::<Vec<_>>();
            let b = (0..test_vec_len).collect::<Vec<_>>();

            let _result = panic::catch_unwind(AssertUnwindSafe(|| {
                let pairs = a.iter().copied().zip(b.iter().copied()).collect();
                let result = in_pool(4, || {
                    map_collect_vec(pairs, &|(a, b)| {
                        if a > panic_point && will_panic {
                            panic!("unwinding for test");
                        }
                        let elt = a + b;
                        inserts.lock().push(elt);
                        Recorddrop(elt, &drops)
                    })
                });

                // If we reach this point, this must pass
                assert_eq!(a.len(), result.len());
            }));

            let inserts = inserts.get_mut();
            let drops = drops.get_mut();

            assert_eq!(inserts.len(), drops.len(), "Incorrect number of drops");
            // sort to normalize order
            inserts.sort();
            drops.sort();
            assert_eq!(inserts, drops, "Incorrect elements were dropped");
        };

        for &should_panic in &[true, false] {
            test_collect_panic(should_panic);
        }
    }

    #[test]
    fn collect_drop_on_unwind_zst() {
        static INSERTS: AtomicUsize = AtomicUsize::new(0);
        static DROPS: AtomicUsize = AtomicUsize::new(0);

        struct RecorddropZst;

        impl Drop for RecorddropZst {
            fn drop(&mut self) {
                DROPS.fetch_add(1, Ordering::SeqCst);
            }
        }

        let test_collect_panic = |will_panic: bool| {
            INSERTS.store(0, Ordering::SeqCst);
            DROPS.store(0, Ordering::SeqCst);

            let test_vec_len = 1024;
            let panic_point = 740;

            let a = (0..test_vec_len).collect::<Vec<_>>();

            let _result = panic::catch_unwind(AssertUnwindSafe(|| {
                let result = in_pool(4, || {
                    map_collect_vec(a.clone(), &|a| {
                        if a > panic_point && will_panic {
                            panic!("unwinding for test");
                        }
                        INSERTS.fetch_add(1, Ordering::SeqCst);
                        RecorddropZst
                    })
                });

                // If we reach this point, this must pass
                assert_eq!(a.len(), result.len());
            }));

            let inserts = INSERTS.load(Ordering::SeqCst);
            let drops = DROPS.load(Ordering::SeqCst);

            assert_eq!(inserts, drops, "Incorrect number of drops");
            assert!(will_panic || drops == test_vec_len)
        };

        for &should_panic in &[true, false] {
            test_collect_panic(should_panic);
        }
    }

    /// Promises to produce 2 items, but then produces 3.  Does not do any
    /// splits at all.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "too many values")]
    fn produce_too_many_items() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 2, |consumer| {
            let _folder = consumer.into_folder().consume(22).consume(23).consume(24);
            unreachable!("folder does not complete")
        });
    }

    /// Produces fewer items than promised. Does not do any
    /// splits at all.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "expected 5 total writes, but got 2")]
    fn produce_fewer_items() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 5, |consumer| {
            let mut folder = consumer.into_folder();
            folder = folder.consume(22);
            folder = folder.consume(23);
            folder.complete()
        });
    }

    // Complete is not called by the consumer. Hence, the collection vector is not fully initialized.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "expected 4 total writes, but got 2")]
    fn left_produces_items_with_no_complete() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, _) = consumer.split_at(2);
            let _left_folder = left_consumer.into_folder().consume(0).consume(1);
            let mut right_folder = right_consumer.into_folder();
            right_folder = right_folder.consume(2).consume(3);
            right_folder.complete()
        });
    }

    // Complete is not called by the right consumer. Hence, the
    // collection vector is not fully initialized.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "expected 4 total writes, but got 2")]
    fn right_produces_items_with_no_complete() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, _) = consumer.split_at(2);
            let mut left_folder = left_consumer.into_folder();
            let _right_folder = right_consumer.into_folder().consume(2).consume(3);
            left_folder = left_folder.consume(0).consume(1);
            left_folder.complete()
        });
    }

    // Complete is not called by the consumer. Hence, the collection vector is not fully initialized.
    #[commonware_macros::test_group("miri")]
    #[test]
    fn produces_items_with_no_complete() {
        let counter = DropCounter::default();
        let mut v = vec![];
        let panic_result = panic::catch_unwind(AssertUnwindSafe(|| {
            collect_with_consumer(&mut v, 2, |consumer| {
                let _folder = consumer
                    .into_folder()
                    .consume(counter.element())
                    .consume(counter.element());
                panic!("folder does not complete");
            });
        }));
        assert!(v.is_empty());
        assert_is_panic_with_message(&panic_result, "folder does not complete");
        counter.assert_drop_count();
    }

    // The left consumer produces too many items while the right
    // consumer produces correct number.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "too many values")]
    fn left_produces_too_many_items() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, _) = consumer.split_at(2);
            let _left_folder = left_consumer.into_folder().consume(0).consume(1).consume(2);
            let _ = right_consumer
                .into_folder()
                .consume(2)
                .consume(3)
                .complete();
            unreachable!("folder does not complete");
        });
    }

    // The right consumer produces too many items while the left
    // consumer produces correct number.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "too many values")]
    fn right_produces_too_many_items() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, _) = consumer.split_at(2);
            let left_folder = left_consumer.into_folder().consume(0).consume(1);
            let _right_folder = right_consumer
                .into_folder()
                .consume(2)
                .consume(3)
                .consume(4);
            let _ = left_folder.complete();
            unreachable!("folder does not complete");
        });
    }

    // The left consumer produces fewer items while the right
    // consumer produces correct number.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "expected 4 total writes, but got 1")]
    fn left_produces_fewer_items() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, reducer) = consumer.split_at(2);
            let mut left_folder = left_consumer.into_folder();
            let mut right_folder = right_consumer.into_folder();
            left_folder = left_folder.consume(0);
            right_folder = right_folder.consume(2).consume(3);
            let left_result = left_folder.complete();
            let right_result = right_folder.complete();
            reducer.reduce(left_result, right_result)
        });
    }

    // The left and right consumer produce the correct number but
    // only left result is returned
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "expected 4 total writes, but got 2")]
    fn only_left_result() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, _) = consumer.split_at(2);
            let mut left_folder = left_consumer.into_folder();
            let mut right_folder = right_consumer.into_folder();
            left_folder = left_folder.consume(0).consume(1);
            right_folder = right_folder.consume(2).consume(3);
            let left_result = left_folder.complete();
            let _ = right_folder.complete();
            left_result
        });
    }

    // The left and right consumer produce the correct number but
    // only right result is returned
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "expected 4 total writes, but got 2")]
    fn only_right_result() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, _) = consumer.split_at(2);
            let mut left_folder = left_consumer.into_folder();
            let mut right_folder = right_consumer.into_folder();
            left_folder = left_folder.consume(0).consume(1);
            right_folder = right_folder.consume(2).consume(3);
            let _ = left_folder.complete();
            right_folder.complete()
        });
    }

    // The left and right consumer produce the correct number but reduce
    // in the wrong order.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "expected 4 total writes, but got 2")]
    fn reducer_does_not_preserve_order() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, reducer) = consumer.split_at(2);
            let mut left_folder = left_consumer.into_folder();
            let mut right_folder = right_consumer.into_folder();
            left_folder = left_folder.consume(0).consume(1);
            right_folder = right_folder.consume(2).consume(3);
            let left_result = left_folder.complete();
            let right_result = right_folder.complete();
            reducer.reduce(right_result, left_result)
        });
    }

    // The right consumer produces fewer items while the left
    // consumer produces correct number.
    #[commonware_macros::test_group("miri")]
    #[test]
    #[should_panic(expected = "expected 4 total writes, but got 3")]
    fn right_produces_fewer_items() {
        let mut v = vec![];
        collect_with_consumer(&mut v, 4, |consumer| {
            let (left_consumer, right_consumer, reducer) = consumer.split_at(2);
            let mut left_folder = left_consumer.into_folder();
            let mut right_folder = right_consumer.into_folder();
            left_folder = left_folder.consume(0).consume(1);
            right_folder = right_folder.consume(2);
            let left_result = left_folder.complete();
            let right_result = right_folder.complete();
            reducer.reduce(left_result, right_result)
        });
    }

    // The left consumer panics and the right stops short, like `panic_fuse()`.
    // We should get the left panic without finishing `collect_with_consumer`.
    #[test]
    #[should_panic(expected = "left consumer panic")]
    fn left_panics() {
        in_pool(4, || {
            let mut v = vec![];
            collect_with_consumer(&mut v, 4, |consumer| {
                let (left_consumer, right_consumer, reducer) = consumer.split_at(2);
                let (left_result, right_result) = join(
                    || {
                        let _left_folder = left_consumer.into_folder().consume(0);
                        panic!("left consumer panic");
                    },
                    || {
                        let mut right_folder = right_consumer.into_folder();
                        right_folder = right_folder.consume(2);
                        right_folder.complete() // early return
                    },
                );
                reducer.reduce(left_result, right_result)
            });
            unreachable!();
        });
    }

    // The right consumer panics and the left stops short, like `panic_fuse()`.
    // We should get the right panic without finishing `collect_with_consumer`.
    #[test]
    #[should_panic(expected = "right consumer panic")]
    fn right_panics() {
        in_pool(4, || {
            let mut v = vec![];
            collect_with_consumer(&mut v, 4, |consumer| {
                let (left_consumer, right_consumer, reducer) = consumer.split_at(2);
                let (left_result, right_result) = join(
                    || {
                        let mut left_folder = left_consumer.into_folder();
                        left_folder = left_folder.consume(0);
                        left_folder.complete() // early return
                    },
                    || {
                        let _right_folder = right_consumer.into_folder().consume(2);
                        panic!("right consumer panic");
                    },
                );
                reducer.reduce(left_result, right_result)
            });
            unreachable!();
        });
    }

    // The left consumer produces fewer items while the right
    // consumer produces correct number. Check that created elements are dropped.
    #[commonware_macros::test_group("miri")]
    #[test]
    fn left_produces_fewer_items_drops() {
        let counter = DropCounter::default();
        let mut v = vec![];
        let panic_result = panic::catch_unwind(AssertUnwindSafe(|| {
            collect_with_consumer(&mut v, 4, |consumer| {
                let (left_consumer, right_consumer, reducer) = consumer.split_at(2);
                let mut left_folder = left_consumer.into_folder();
                let mut right_folder = right_consumer.into_folder();
                left_folder = left_folder.consume(counter.element());
                right_folder = right_folder
                    .consume(counter.element())
                    .consume(counter.element());
                let left_result = left_folder.complete();
                let right_result = right_folder.complete();
                reducer.reduce(left_result, right_result)
            });
        }));
        assert!(v.is_empty());
        assert_is_panic_with_message(&panic_result, "expected 4 total writes, but got 1");
        counter.assert_drop_count();
    }

    /// This counter can create elements, and then count and verify
    /// the number of which have actually been dropped again.
    #[derive(Default)]
    struct DropCounter {
        created: AtomicUsize,
        dropped: AtomicUsize,
    }

    struct Element<'a>(&'a AtomicUsize);

    impl DropCounter {
        fn created(&self) -> usize {
            self.created.load(Ordering::SeqCst)
        }

        fn dropped(&self) -> usize {
            self.dropped.load(Ordering::SeqCst)
        }

        fn element(&self) -> Element<'_> {
            self.created.fetch_add(1, Ordering::SeqCst);
            Element(&self.dropped)
        }

        fn assert_drop_count(&self) {
            assert_eq!(
                self.created(),
                self.dropped(),
                "Expected {} dropped elements, but found {}",
                self.created(),
                self.dropped()
            );
        }
    }

    impl Drop for Element<'_> {
        fn drop(&mut self) {
            self.0.fetch_add(1, Ordering::SeqCst);
        }
    }

    /// Assert that the result from catch_unwind is a panic that contains expected message
    fn assert_is_panic_with_message<T>(result: &ThreadResult<T>, expected: &str)
    where
        T: fmt::Debug,
    {
        match result {
            Ok(value) => {
                panic!("assertion failure: Expected panic, got successful {value:?}");
            }
            Err(error) => {
                let message_str = error.downcast_ref::<&'static str>().cloned();
                let message_string = error.downcast_ref::<String>().map(String::as_str);
                if let Some(message) = message_str.or(message_string) {
                    if !message.contains(expected) {
                        panic!(
                            "assertion failure: Expected {expected:?}, but found panic with {message:?}"
                        );
                    }
                // assertion passes
                } else {
                    panic!(
                        "assertion failure: Expected {expected:?}, but found panic with unknown value"
                    );
                }
            }
        }
    }
}