1
Fork 0

Changes according to review

This commit is contained in:
Markus Everling 2023-02-20 23:25:26 +01:00
parent 1e114a88bd
commit acc876e42f
2 changed files with 11 additions and 11 deletions

View file

@ -52,21 +52,22 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
} }
} }
// Only returns pointers to the slices, as that's // Only returns pointers to the slices, as that's all we need
// all we need to drop them. May only be called if `self.remaining != 0`. // to drop them. May only be called if `self.remaining != 0`.
unsafe fn as_slices(&self) -> (*mut [T], *mut [T]) { unsafe fn as_slices(&self) -> (*mut [T], *mut [T]) {
unsafe { unsafe {
let deque = self.deque.as_ref(); 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. // 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, // haven't been drained yet, so they're all initialized,
// and `slice::range(start..end, end) == start..end`, // and `slice::range(start..end, end) == start..end`,
// so the preconditions for `slice_ranges` are met. // 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)) (deque.buffer_range(a_range), deque.buffer_range(b_range))
} }
} }

View file

@ -1230,10 +1230,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// # Safety /// # Safety
/// ///
/// This function is always safe to call. For the resulting ranges to be valid /// 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 /// ranges into the physical buffer, the caller must ensure that the result of
/// values of `range` and `len`, the result of calling `slice::range(range, ..len)` /// calling `slice::range(range, ..len)` represents a valid range into the
/// represents a valid range into the logical buffer, and that all elements /// logical buffer, and that all elements in that range are initialized.
/// in that range are initialized.
fn slice_ranges<R>(&self, range: R, len: usize) -> (Range<usize>, Range<usize>) fn slice_ranges<R>(&self, range: R, len: usize) -> (Range<usize>, Range<usize>)
where where
R: RangeBounds<usize>, R: RangeBounds<usize>,
@ -1244,7 +1243,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
if len == 0 { if len == 0 {
(0..0, 0..0) (0..0, 0..0)
} else { } 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` // because `len != 0`, we know that `start < end`, so `start < len`
// and the indexing is valid. // and the indexing is valid.
let wrapped_start = self.to_physical_idx(start); let wrapped_start = self.to_physical_idx(start);