From 7d5c9252d83446d0dbcf4ba18ac7a58f167be021 Mon Sep 17 00:00:00 2001 From: Rachit2323 Date: Tue, 14 Jul 2026 12:37:26 +0530 Subject: [PATCH] slice: make swap delegate to swap_unchecked --- library/core/src/slice/mod.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 25889994be977..14c207a2a0a98 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -906,16 +906,12 @@ impl [T] { #[inline] #[track_caller] pub const fn swap(&mut self, a: usize, b: usize) { - // FIXME: use swap_unchecked here (https://github.com/rust-lang/rust/pull/88540#issuecomment-944344343) - // Can't take two mutable loans from one vector, so instead use raw pointers. - let pa = &raw mut self[a]; - let pb = &raw mut self[b]; - // SAFETY: `pa` and `pb` have been created from safe mutable references and refer - // to elements in the slice and therefore are guaranteed to be valid and aligned. - // Note that accessing the elements behind `a` and `b` is checked and will - // panic when out of bounds. + // Bounds checks that panic exactly like indexing would. + let _ = &self[a]; + let _ = &self[b]; + // SAFETY: `a` and `b` were checked to be in bounds above. unsafe { - ptr::swap(pa, pb); + self.swap_unchecked(a, b); } }