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 66// option. This file may not be copied, modified, or distributed
77// except according to those terms.
88
9- use std:: mem:: { size_of, ManuallyDrop } ;
9+ use std:: mem:: { size_of, ManuallyDrop , MaybeUninit } ;
1010use alloc:: slice;
1111use alloc:: vec;
1212use alloc:: vec:: Vec ;
@@ -2462,9 +2462,22 @@ where
24622462 let mut lane_iter = lane. iter_mut ( ) ;
24632463 let mut dst = if let Some ( dst) = lane_iter. next ( ) { dst } else { return } ;
24642464
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 rest.iter_mut() {
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 ) ;
24682481 }
24692482 } ) ;
24702483 // 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