Skip to content

Commit

Permalink
Fix stacked borrows violations
Browse files Browse the repository at this point in the history
Use offsets from a single pointer instead of multiple pointers into an array.
Also removed an integer-pointer cast
  • Loading branch information
clubby789 authored and bluss committed Nov 12, 2022
1 parent a927f7c commit c9ff579
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/array_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,12 @@ impl<const CAP: usize> ArrayString<CAP>

let next = idx + ch.len_utf8();
let len = self.len();
let ptr = self.as_mut_ptr();
unsafe {
ptr::copy(self.as_ptr().add(next),
self.as_mut_ptr().add(idx),
len - next);
ptr::copy(
ptr.add(next),
ptr.add(idx),
len - next);
self.set_len(len - (next - idx));
}
ch
Expand Down
9 changes: 4 additions & 5 deletions src/arrayvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
}
if DELETED {
unsafe {
let hole_slot = g.v.as_mut_ptr().add(g.processed_len - g.deleted_cnt);
let hole_slot = cur.sub(g.deleted_cnt);
ptr::copy_nonoverlapping(cur, hole_slot, 1);
}
}
Expand Down Expand Up @@ -978,9 +978,8 @@ impl<'a, T: 'a, const CAP: usize> Drop for Drain<'a, T, CAP> {
// memmove back untouched tail, update to new length
let start = source_vec.len();
let tail = self.tail_start;
let src = source_vec.as_ptr().add(tail);
let dst = source_vec.as_mut_ptr().add(start);
ptr::copy(src, dst, self.tail_len);
let ptr = source_vec.as_mut_ptr();
ptr::copy(ptr.add(tail), ptr.add(start), self.tail_len);
source_vec.set_len(start + self.tail_len);
}
}
Expand Down Expand Up @@ -1082,7 +1081,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
unsafe fn raw_ptr_add<T>(ptr: *mut T, offset: usize) -> *mut T {
if mem::size_of::<T>() == 0 {
// Special case for ZST
(ptr as usize).wrapping_add(offset) as _
ptr.cast::<u8>().wrapping_add(offset).cast()
} else {
ptr.add(offset)
}
Expand Down

0 comments on commit c9ff579

Please sign in to comment.