From acc876e42fce0dc3d6596f754dcfbc0ce07cabd6 Mon Sep 17 00:00:00 2001 From: Markus Everling Date: Mon, 20 Feb 2023 23:25:26 +0100 Subject: [PATCH] Changes according to review --- library/alloc/src/collections/vec_deque/drain.rs | 13 +++++++------ library/alloc/src/collections/vec_deque/mod.rs | 9 ++++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/library/alloc/src/collections/vec_deque/drain.rs b/library/alloc/src/collections/vec_deque/drain.rs index 99bd7902e69..0be274a3822 100644 --- a/library/alloc/src/collections/vec_deque/drain.rs +++ b/library/alloc/src/collections/vec_deque/drain.rs @@ -52,21 +52,22 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> { } } - // Only returns pointers to the slices, as that's - // all we need to drop them. May only be called if `self.remaining != 0`. + // Only returns pointers to the slices, as that's all we need + // to drop them. May only be called if `self.remaining != 0`. unsafe fn as_slices(&self) -> (*mut [T], *mut [T]) { unsafe { let deque = self.deque.as_ref(); - let start = self.idx; // We know that `self.idx + self.remaining <= deque.len <= usize::MAX`, so this won't overflow. - let end = start + self.remaining; + let logical_remaining_range = self.idx..self.idx + self.remaining; - // SAFETY: `start..end` represents the range of elements that + // SAFETY: `logical_remaining_range` represents the + // range into the logical buffer of elements that // haven't been drained yet, so they're all initialized, // and `slice::range(start..end, end) == start..end`, // so the preconditions for `slice_ranges` are met. - let (a_range, b_range) = deque.slice_ranges(start..end, end); + let (a_range, b_range) = + deque.slice_ranges(logical_remaining_range.clone(), logical_remaining_range.end); (deque.buffer_range(a_range), deque.buffer_range(b_range)) } } diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 813430ae615..40f31f1a194 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -1230,10 +1230,9 @@ impl VecDeque { /// # Safety /// /// This function is always safe to call. For the resulting ranges to be valid - /// ranges into the physical buffer, the caller must ensure that for all possible - /// values of `range` and `len`, the result of calling `slice::range(range, ..len)` - /// represents a valid range into the logical buffer, and that all elements - /// in that range are initialized. + /// ranges into the physical buffer, the caller must ensure that the result of + /// calling `slice::range(range, ..len)` represents a valid range into the + /// logical buffer, and that all elements in that range are initialized. fn slice_ranges(&self, range: R, len: usize) -> (Range, Range) where R: RangeBounds, @@ -1244,7 +1243,7 @@ impl VecDeque { if len == 0 { (0..0, 0..0) } else { - // `slice::range` guarantees that `start <= end <= self.len`. + // `slice::range` guarantees that `start <= end <= len`. // because `len != 0`, we know that `start < end`, so `start < len` // and the indexing is valid. let wrapped_start = self.to_physical_idx(start);