Skip to content

Commit

Permalink
FIX: Use raw pointers for .swap(i, j)
Browse files Browse the repository at this point in the history
This fixes a minor issue where we want to avoid overlapping &mut from
the same array.
  • Loading branch information
bluss committed Jan 30, 2021
1 parent 159ae36 commit 4f8d4f2
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,18 @@ where
S: DataMut,
I: NdIndex<D>,
{
let ptr1: *mut _ = &mut self[index1];
let ptr2: *mut _ = &mut self[index2];
unsafe {
std_ptr::swap(ptr1, ptr2);
let ptr = self.as_mut_ptr();
let offset1 = index1.index_checked(&self.dim, &self.strides);
let offset2 = index2.index_checked(&self.dim, &self.strides);
if let Some(offset1) = offset1 {
if let Some(offset2) = offset2 {
unsafe {
std_ptr::swap(ptr.offset(offset1), ptr.offset(offset2));
}
return;
}
}
panic!("swap: index out of bounds for indices {:?} {:?}", index1, index2);
}

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

0 comments on commit 4f8d4f2

Please sign in to comment.