Skip to content

Commit 4f8d4f2

Browse files
committed
FIX: Use raw pointers for .swap(i, j)
This fixes a minor issue where we want to avoid overlapping &mut from the same array.
1 parent 159ae36 commit 4f8d4f2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/impl_methods.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,18 @@ where
630630
S: DataMut,
631631
I: NdIndex<D>,
632632
{
633-
let ptr1: *mut _ = &mut self[index1];
634-
let ptr2: *mut _ = &mut self[index2];
635-
unsafe {
636-
std_ptr::swap(ptr1, ptr2);
633+
let ptr = self.as_mut_ptr();
634+
let offset1 = index1.index_checked(&self.dim, &self.strides);
635+
let offset2 = index2.index_checked(&self.dim, &self.strides);
636+
if let Some(offset1) = offset1 {
637+
if let Some(offset2) = offset2 {
638+
unsafe {
639+
std_ptr::swap(ptr.offset(offset1), ptr.offset(offset2));
640+
}
641+
return;
642+
}
637643
}
644+
panic!("swap: index out of bounds for indices {:?} {:?}", index1, index2);
638645
}
639646

640647
/// Swap elements *unchecked* at indices `index1` and `index2`.

0 commit comments

Comments
 (0)