Skip to content

Commit 1e114a8

Browse files
committed
Add slice_ranges safety comment
1 parent 8ca25b8 commit 1e114a8

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

library/alloc/src/collections/vec_deque/drain.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
6262
// We know that `self.idx + self.remaining <= deque.len <= usize::MAX`, so this won't overflow.
6363
let end = start + self.remaining;
6464

65-
// SAFETY: the range `start..end` lies strictly inside
66-
// the range `0..deque.original_len`. Because of this, and because
67-
// we haven't touched the elements inside this range yet,
68-
// it's guaranteed that `a_range` and `b_range` represent valid ranges into
69-
// the deques buffer.
65+
// SAFETY: `start..end` represents the range of elements that
66+
// haven't been drained yet, so they're all initialized,
67+
// and `slice::range(start..end, end) == start..end`,
68+
// so the preconditions for `slice_ranges` are met.
7069
let (a_range, b_range) = deque.slice_ranges(start..end, end);
7170
(deque.buffer_range(a_range), deque.buffer_range(b_range))
7271
}

library/alloc/src/collections/vec_deque/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,14 @@ impl<T, A: Allocator> VecDeque<T, A> {
12261226
/// the given range. The `len` parameter should usually just be `self.len`;
12271227
/// the reason it's passed explicitly is that if the deque is wrapped in
12281228
/// a `Drain`, then `self.len` is not actually the length of the deque.
1229+
///
1230+
/// # Safety
1231+
///
1232+
/// This function is always safe to call. For the resulting ranges to be valid
1233+
/// ranges into the physical buffer, the caller must ensure that for all possible
1234+
/// values of `range` and `len`, the result of calling `slice::range(range, ..len)`
1235+
/// represents a valid range into the logical buffer, and that all elements
1236+
/// in that range are initialized.
12291237
fn slice_ranges<R>(&self, range: R, len: usize) -> (Range<usize>, Range<usize>)
12301238
where
12311239
R: RangeBounds<usize>,

0 commit comments

Comments
 (0)