Fix stacked borrows violations

Use offsets from a single pointer instead of multiple pointers into an array.
Also removed an integer-pointer cast
This commit is contained in:
clubby789
2022-09-06 17:12:47 +01:00
committed by Ulrik Sverdrup
parent a927f7cf03
commit c9ff579405
2 changed files with 9 additions and 8 deletions
+5 -3
View File
@@ -371,10 +371,12 @@ impl<const CAP: usize> ArrayString<CAP>
let next = idx + ch.len_utf8(); let next = idx + ch.len_utf8();
let len = self.len(); let len = self.len();
let ptr = self.as_mut_ptr();
unsafe { unsafe {
ptr::copy(self.as_ptr().add(next), ptr::copy(
self.as_mut_ptr().add(idx), ptr.add(next),
len - next); ptr.add(idx),
len - next);
self.set_len(len - (next - idx)); self.set_len(len - (next - idx));
} }
ch ch
+4 -5
View File
@@ -507,7 +507,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
} }
if DELETED { if DELETED {
unsafe { 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); ptr::copy_nonoverlapping(cur, hole_slot, 1);
} }
} }
@@ -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 // memmove back untouched tail, update to new length
let start = source_vec.len(); let start = source_vec.len();
let tail = self.tail_start; let tail = self.tail_start;
let src = source_vec.as_ptr().add(tail); let ptr = source_vec.as_mut_ptr();
let dst = source_vec.as_mut_ptr().add(start); ptr::copy(ptr.add(tail), ptr.add(start), self.tail_len);
ptr::copy(src, dst, self.tail_len);
source_vec.set_len(start + self.tail_len); source_vec.set_len(start + self.tail_len);
} }
} }
@@ -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 { unsafe fn raw_ptr_add<T>(ptr: *mut T, offset: usize) -> *mut T {
if mem::size_of::<T>() == 0 { if mem::size_of::<T>() == 0 {
// Special case for ZST // Special case for ZST
(ptr as usize).wrapping_add(offset) as _ ptr.cast::<u8>().wrapping_add(offset).cast()
} else { } else {
ptr.add(offset) ptr.add(offset)
} }