File tree Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Expand file tree Collapse file tree 1 file changed +17
-4
lines changed Original file line number Diff line number Diff line change 6
6
// option. This file may not be copied, modified, or distributed
7
7
// except according to those terms.
8
8
9
- use std:: mem:: { size_of, ManuallyDrop } ;
9
+ use std:: mem:: { size_of, ManuallyDrop , MaybeUninit } ;
10
10
use alloc:: slice;
11
11
use alloc:: vec;
12
12
use alloc:: vec:: Vec ;
@@ -2462,9 +2462,22 @@ where
2462
2462
let mut lane_iter = lane. iter_mut ( ) ;
2463
2463
let mut dst = if let Some ( dst) = lane_iter. next ( ) { dst } else { return } ;
2464
2464
2465
- for elt in lane_iter {
2466
- std:: mem:: swap ( dst, elt) ;
2467
- dst = elt;
2465
+ // Logically we do a circular swap here, all elements in a chain
2466
+ // Using MaybeUninit to avoid unecessary writes in the safe swap solution
2467
+ //
2468
+ // for elt in lane_iter {
2469
+ // std::mem::swap(dst, elt);
2470
+ // dst = elt;
2471
+ // }
2472
+ //
2473
+ let mut slot = MaybeUninit :: < A > :: uninit ( ) ;
2474
+ unsafe {
2475
+ slot. as_mut_ptr ( ) . copy_from_nonoverlapping ( dst, 1 ) ;
2476
+ for elt in lane_iter {
2477
+ ( dst as * mut A ) . copy_from_nonoverlapping ( elt, 1 ) ;
2478
+ dst = elt;
2479
+ }
2480
+ ( dst as * mut A ) . copy_from_nonoverlapping ( slot. as_ptr ( ) , 1 ) ;
2468
2481
}
2469
2482
} ) ;
2470
2483
// then slice the axis in place to cut out the removed final element
You can’t perform that action at this time.
0 commit comments