Skip to content

Commit b8df250

Browse files
committed
address safety comment review
Signed-off-by: Petros Angelatos <[email protected]>
1 parent 1330458 commit b8df250

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

library/alloc/src/vec/extract_if.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ where
6666
fn next(&mut self) -> Option<T> {
6767
while self.idx < self.end {
6868
let i = self.idx;
69-
// SAFETY: Unchecked element must be valid.
69+
// SAFETY:
70+
// We know that `i < self.end` from the if guard and that `self.end <= self.old_len` from
71+
// the validity of `Self`. Therefore `i` points to an element within `vec`.
72+
//
73+
// Additionally, the i-th element is valid because each element is visited at most once
74+
// and it is the first time we access vec[i].
75+
//
76+
// Note: we can't use `vec.get_unchecked_mut(i)` here since the precondition for that
77+
// function is that i < vec.len(), but we've set vec's length to zero.
7078
let cur = unsafe { &mut *self.vec.as_mut_ptr().add(i) };
7179
let drained = (self.pred)(cur);
7280
// Update the index *after* the predicate is called. If the index

0 commit comments

Comments
 (0)