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
// 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))
}
}

View File

@ -1230,10 +1230,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// # 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<R>(&self, range: R, len: usize) -> (Range<usize>, Range<usize>)
where
R: RangeBounds<usize>,
@ -1244,7 +1243,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
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);