Skip to content

Commit c082c1c

Browse files
author
lch361
committedDec 24, 2023
Documented unsafe blocks
1 parent b15e137 commit c082c1c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
 

‎library/core/src/slice/iter.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,12 @@ where
458458
match self.v.iter().position(|x| (self.pred)(x)) {
459459
None => self.finish(),
460460
Some(idx) => {
461-
let ret = Some(unsafe { self.v.get_unchecked(..idx) });
462-
self.v = unsafe { self.v.get_unchecked(idx + 1..) };
461+
let (left, right) =
462+
// SAFETY: if v.iter().position returns Some(idx), that
463+
// idx is definitely a valid index for v
464+
unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
465+
let ret = Some(left);
466+
self.v = right;
463467
ret
464468
}
465469
}
@@ -491,8 +495,12 @@ where
491495
match self.v.iter().rposition(|x| (self.pred)(x)) {
492496
None => self.finish(),
493497
Some(idx) => {
494-
let ret = Some(unsafe { self.v.get_unchecked(idx + 1..) });
495-
self.v = unsafe { self.v.get_unchecked(..idx) };
498+
let (left, right) =
499+
// SAFETY: if v.iter().rposition returns Some(idx), then
500+
// idx is definitely a valid index for v
501+
unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
502+
let ret = Some(right);
503+
self.v = left;
496504
ret
497505
}
498506
}

0 commit comments

Comments
 (0)
Please sign in to comment.