File tree Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Expand file tree Collapse file tree 1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -587,11 +587,17 @@ impl<T> [T] {
587587 #[ inline]
588588 #[ track_caller]
589589 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+ }
595601 }
596602
597603 /// Swaps two elements in the slice, without doing bounds checking.
You can’t perform that action at this time.
0 commit comments