File tree 1 file changed +11
-5
lines changed
1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -587,11 +587,17 @@ impl<T> [T] {
587
587
#[ inline]
588
588
#[ track_caller]
589
589
pub const fn swap ( & mut self , a : usize , b : usize ) {
590
- let _ = & self [ a] ;
591
- let _ = & self [ b] ;
592
-
593
- // SAFETY: we just checked that both `a` and `b` are in bounds
594
- unsafe { self . swap_unchecked ( a, b) }
590
+ // FIXME: use swap_unchecked here (https://github.com/rust-lang/rust/pull/88540#issuecomment-944344343)
591
+ // Can't take two mutable loans from one vector, so instead use raw pointers.
592
+ let pa = ptr:: addr_of_mut!( self [ a] ) ;
593
+ let pb = ptr:: addr_of_mut!( self [ b] ) ;
594
+ // SAFETY: `pa` and `pb` have been created from safe mutable references and refer
595
+ // to elements in the slice and therefore are guaranteed to be valid and aligned.
596
+ // Note that accessing the elements behind `a` and `b` is checked and will
597
+ // panic when out of bounds.
598
+ unsafe {
599
+ ptr:: swap ( pa, pb) ;
600
+ }
595
601
}
596
602
597
603
/// Swaps two elements in the slice, without doing bounds checking.
You can’t perform that action at this time.
0 commit comments