33
33
F : FnMut ( & T , & T ) -> bool ,
34
34
{
35
35
let len = v. len ( ) ;
36
- // SAFETY: The unsafe operations below involves indexing without a bound check (`get_unchecked` and `get_unchecked_mut`)
37
- // and copying memory (`ptr::copy_nonoverlapping`).
36
+ // SAFETY: The unsafe operations below involves indexing without a bounds check (by offsetting a
37
+ // pointer) and copying memory (`ptr::copy_nonoverlapping`).
38
38
//
39
39
// a. Indexing:
40
40
// 1. We checked the size of the array to >=2.
@@ -55,17 +55,18 @@ where
55
55
// operation panics, `hole` will get dropped and automatically write the element back
56
56
// into the slice.
57
57
let mut tmp = mem:: ManuallyDrop :: new ( ptr:: read ( v. get_unchecked ( 0 ) ) ) ;
58
- let mut hole = CopyOnDrop { src : & mut * tmp, dest : v. get_unchecked_mut ( 1 ) } ;
59
- ptr:: copy_nonoverlapping ( v. get_unchecked ( 1 ) , v. get_unchecked_mut ( 0 ) , 1 ) ;
58
+ let v = v. as_mut_ptr ( ) ;
59
+ let mut hole = CopyOnDrop { src : & mut * tmp, dest : v. add ( 1 ) } ;
60
+ ptr:: copy_nonoverlapping ( v. add ( 1 ) , v. add ( 0 ) , 1 ) ;
60
61
61
62
for i in 2 ..len {
62
- if !is_less ( v . get_unchecked ( i) , & * tmp) {
63
+ if !is_less ( & * v . add ( i) , & * tmp) {
63
64
break ;
64
65
}
65
66
66
67
// Move `i`-th element one place to the left, thus shifting the hole to the right.
67
- ptr:: copy_nonoverlapping ( v. get_unchecked ( i) , v. get_unchecked_mut ( i - 1 ) , 1 ) ;
68
- hole. dest = v. get_unchecked_mut ( i) ;
68
+ ptr:: copy_nonoverlapping ( v. add ( i) , v. add ( i - 1 ) , 1 ) ;
69
+ hole. dest = v. add ( i) ;
69
70
}
70
71
// `hole` gets dropped and thus copies `tmp` into the remaining hole in `v`.
71
72
}
78
79
F : FnMut ( & T , & T ) -> bool ,
79
80
{
80
81
let len = v. len ( ) ;
81
- // SAFETY: The unsafe operations below involves indexing without a bound check (`get_unchecked` and `get_unchecked_mut`)
82
- // and copying memory (`ptr::copy_nonoverlapping`).
82
+ // SAFETY: The unsafe operations below involves indexing without a bound check (by offsetting a
83
+ // pointer) and copying memory (`ptr::copy_nonoverlapping`).
83
84
//
84
85
// a. Indexing:
85
86
// 1. We checked the size of the array to >= 2.
@@ -100,17 +101,18 @@ where
100
101
// operation panics, `hole` will get dropped and automatically write the element back
101
102
// into the slice.
102
103
let mut tmp = mem:: ManuallyDrop :: new ( ptr:: read ( v. get_unchecked ( len - 1 ) ) ) ;
103
- let mut hole = CopyOnDrop { src : & mut * tmp, dest : v. get_unchecked_mut ( len - 2 ) } ;
104
- ptr:: copy_nonoverlapping ( v. get_unchecked ( len - 2 ) , v. get_unchecked_mut ( len - 1 ) , 1 ) ;
104
+ let v = v. as_mut_ptr ( ) ;
105
+ let mut hole = CopyOnDrop { src : & mut * tmp, dest : v. add ( len - 2 ) } ;
106
+ ptr:: copy_nonoverlapping ( v. add ( len - 2 ) , v. add ( len - 1 ) , 1 ) ;
105
107
106
108
for i in ( 0 ..len - 2 ) . rev ( ) {
107
- if !is_less ( & * tmp, v . get_unchecked ( i) ) {
109
+ if !is_less ( & * tmp, & * v . add ( i) ) {
108
110
break ;
109
111
}
110
112
111
113
// Move `i`-th element one place to the right, thus shifting the hole to the left.
112
- ptr:: copy_nonoverlapping ( v. get_unchecked ( i) , v. get_unchecked_mut ( i + 1 ) , 1 ) ;
113
- hole. dest = v. get_unchecked_mut ( i) ;
114
+ ptr:: copy_nonoverlapping ( v. add ( i) , v. add ( i + 1 ) , 1 ) ;
115
+ hole. dest = v. add ( i) ;
114
116
}
115
117
// `hole` gets dropped and thus copies `tmp` into the remaining hole in `v`.
116
118
}
@@ -302,7 +304,7 @@ where
302
304
if start_l == end_l {
303
305
// Trace `block_l` elements from the left side.
304
306
start_l = MaybeUninit :: slice_as_mut_ptr ( & mut offsets_l) ;
305
- end_l = MaybeUninit :: slice_as_mut_ptr ( & mut offsets_l ) ;
307
+ end_l = start_l ;
306
308
let mut elem = l;
307
309
308
310
for i in 0 ..block_l {
@@ -328,7 +330,7 @@ where
328
330
if start_r == end_r {
329
331
// Trace `block_r` elements from the right side.
330
332
start_r = MaybeUninit :: slice_as_mut_ptr ( & mut offsets_r) ;
331
- end_r = MaybeUninit :: slice_as_mut_ptr ( & mut offsets_r ) ;
333
+ end_r = start_r ;
332
334
let mut elem = r;
333
335
334
336
for i in 0 ..block_r {
@@ -579,7 +581,8 @@ where
579
581
580
582
// Swap the found pair of out-of-order elements.
581
583
r -= 1 ;
582
- ptr:: swap ( v. get_unchecked_mut ( l) , v. get_unchecked_mut ( r) ) ;
584
+ let ptr = v. as_mut_ptr ( ) ;
585
+ ptr:: swap ( ptr. add ( l) , ptr. add ( r) ) ;
583
586
l += 1 ;
584
587
}
585
588
}
0 commit comments