1/// Test that one can emulate join with `scope`:
2fn pseudo_join<F, G>(f: F, g: G)
3where
4 F: FnOnce() + Send,
5 G: FnOnce() + Send,
6{
7 rayon_core::scope(|s| {
8 s.spawn(|_| g());
9 f();
10 });
11}
12
13fn quick_sort<T: PartialOrd + Send>(v: &mut [T]) {
14 if v.len() <= 1 {
15 return;
16 }
17
18 let mid = partition(v);
19 let (lo, hi) = v.split_at_mut(mid);
20 pseudo_join(|| quick_sort(lo), || quick_sort(hi));
21}
22
23fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
24 let pivot = v.len() - 1;
25 let mut i = 0;
26 for j in 0..pivot {
27 if v[j] <= v[pivot] {
28 v.swap(i, j);
29 i += 1;
30 }
31 }
32 v.swap(i, pivot);
33 i
34}
35
36fn is_sorted<T: Send + Ord>(v: &[T]) -> bool {
37 (1..v.len()).all(|i| v[i - 1] <= v[i])
38}
39
40#[test]
41fn scope_join() {
42 let mut v: Vec<i32> = (0..256).rev().collect();
43 quick_sort(&mut v);
44 assert!(is_sorted(&v));
45}
46