From c9ff579405a67ac5e37baa2ddf322acbcab3a185 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Tue, 6 Sep 2022 17:12:47 +0100 Subject: [PATCH] Fix stacked borrows violations Use offsets from a single pointer instead of multiple pointers into an array. Also removed an integer-pointer cast --- src/array_string.rs | 8 +++++--- src/arrayvec.rs | 9 ++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/array_string.rs b/src/array_string.rs index c4712a0..1758526 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -371,10 +371,12 @@ impl ArrayString 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 diff --git a/src/arrayvec.rs b/src/arrayvec.rs index e69e60c..a21bbb3 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -507,7 +507,7 @@ impl ArrayVec { } 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); } } @@ -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); } } @@ -1082,7 +1081,7 @@ impl ArrayVec { unsafe fn raw_ptr_add(ptr: *mut T, offset: usize) -> *mut T { if mem::size_of::() == 0 { // Special case for ZST - (ptr as usize).wrapping_add(offset) as _ + ptr.cast::().wrapping_add(offset).cast() } else { ptr.add(offset) }