5
5
//! `rayon_core::join`.
6
6
7
7
use std:: cmp;
8
+ use std:: marker:: PhantomData ;
8
9
use std:: mem:: { self , MaybeUninit } ;
9
10
use std:: ptr;
10
11
11
12
/// When dropped, copies from `src` into `dest`.
12
- struct CopyOnDrop < T > {
13
+ struct CopyOnDrop < ' src , ' dest , T > {
13
14
src : * const T ,
14
15
dest : * mut T ,
16
+ /// `src` is often a local pointer here, make sure we have appropriate
17
+ /// PhantomData so that dropck can protect us.
18
+ marker : PhantomData < ( & ' src T , & ' dest mut T ) > ,
15
19
}
16
20
17
- impl < T > Drop for CopyOnDrop < T > {
21
+ impl < T > Drop for CopyOnDrop < ' _ , ' _ , T > {
18
22
fn drop ( & mut self ) {
19
23
// SAFETY: This is a helper class.
20
24
// Please refer to its usage for correctness.
@@ -26,7 +30,7 @@ impl<T> Drop for CopyOnDrop<T> {
26
30
}
27
31
28
32
/// Shifts the first element to the right until it encounters a greater or equal element.
29
- fn shift_head < T , F > ( v : & mut [ T ] , is_less : & F )
33
+ fn shift_head < ' v , T , F > ( v : & ' v mut [ T ] , is_less : & F )
30
34
where
31
35
F : Fn ( & T , & T ) -> bool ,
32
36
{
54
58
// into the slice.
55
59
let tmp = mem:: ManuallyDrop :: new ( ptr:: read ( v. get_unchecked ( 0 ) ) ) ;
56
60
let v = v. as_mut_ptr ( ) ;
57
- let mut hole = CopyOnDrop {
61
+ let mut hole = CopyOnDrop :: < ' _ , ' v , _ > {
58
62
src : & * tmp,
59
63
dest : v. add ( 1 ) ,
64
+ marker : PhantomData ,
60
65
} ;
61
66
ptr:: copy_nonoverlapping ( v. add ( 1 ) , v. add ( 0 ) , 1 ) ;
62
67
75
80
}
76
81
77
82
/// Shifts the last element to the left until it encounters a smaller or equal element.
78
- fn shift_tail < T , F > ( v : & mut [ T ] , is_less : & F )
83
+ fn shift_tail < ' v , T , F > ( v : & ' v mut [ T ] , is_less : & F )
79
84
where
80
85
F : Fn ( & T , & T ) -> bool ,
81
86
{
@@ -103,9 +108,10 @@ where
103
108
// into the slice.
104
109
let tmp = mem:: ManuallyDrop :: new ( ptr:: read ( v. get_unchecked ( len - 1 ) ) ) ;
105
110
let v = v. as_mut_ptr ( ) ;
106
- let mut hole = CopyOnDrop {
111
+ let mut hole = CopyOnDrop :: < ' _ , ' v , _ > {
107
112
src : & * tmp,
108
113
dest : v. add ( len - 2 ) ,
114
+ marker : PhantomData ,
109
115
} ;
110
116
ptr:: copy_nonoverlapping ( v. add ( len - 2 ) , v. add ( len - 1 ) , 1 ) ;
111
117
@@ -495,7 +501,7 @@ where
495
501
///
496
502
/// 1. Number of elements smaller than `v[pivot]`.
497
503
/// 2. True if `v` was already partitioned.
498
- fn partition < T , F > ( v : & mut [ T ] , pivot : usize , is_less : & F ) -> ( usize , bool )
504
+ fn partition < ' v , T , F > ( v : & ' v mut [ T ] , pivot : usize , is_less : & F ) -> ( usize , bool )
499
505
where
500
506
F : Fn ( & T , & T ) -> bool ,
501
507
{
@@ -510,9 +516,10 @@ where
510
516
511
517
// SAFETY: `pivot` is a reference to the first element of `v`, so `ptr::read` is safe.
512
518
let tmp = mem:: ManuallyDrop :: new ( unsafe { ptr:: read ( pivot) } ) ;
513
- let _pivot_guard = CopyOnDrop {
519
+ let _pivot_guard = CopyOnDrop :: < ' _ , ' v , _ > {
514
520
src : & * tmp,
515
521
dest : pivot,
522
+ marker : PhantomData ,
516
523
} ;
517
524
let pivot = & * tmp;
518
525
@@ -556,7 +563,7 @@ where
556
563
///
557
564
/// Returns the number of elements equal to the pivot. It is assumed that `v` does not contain
558
565
/// elements smaller than the pivot.
559
- fn partition_equal < T , F > ( v : & mut [ T ] , pivot : usize , is_less : & F ) -> usize
566
+ fn partition_equal < ' v , T , F > ( v : & ' v mut [ T ] , pivot : usize , is_less : & F ) -> usize
560
567
where
561
568
F : Fn ( & T , & T ) -> bool ,
562
569
{
@@ -569,9 +576,10 @@ where
569
576
// operation panics, the pivot will be automatically written back into the slice.
570
577
// SAFETY: The pointer here is valid because it is obtained from a reference to a slice.
571
578
let tmp = mem:: ManuallyDrop :: new ( unsafe { ptr:: read ( pivot) } ) ;
572
- let _pivot_guard = CopyOnDrop {
579
+ let _pivot_guard = CopyOnDrop :: < ' _ , ' v , _ > {
573
580
src : & * tmp,
574
581
dest : pivot,
582
+ marker : PhantomData ,
575
583
} ;
576
584
let pivot = & * tmp;
577
585
0 commit comments