Skip to content

Commit 52f9762

Browse files
committed
Add comment on use of unsafe
1 parent 8cc6998 commit 52f9762

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/libcore/slice/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,6 +2694,15 @@ impl<T> [T] {
26942694

26952695
while left != right {
26962696
let mid = left + (right - left) / 2;
2697+
// SAFETY:
2698+
// When left < right, left <= mid < right.
2699+
// Therefore left always increases and right always decreases,
2700+
// and eigher of them is selected.
2701+
// In both cases left <= right is satisfied.
2702+
// Therefore if left < right in a step,
2703+
// left <= right is satisfied in the next step.
2704+
// Therefore as long as left != right, 0 <= left < right <= len is satisfied
2705+
// and if this case 0 <= mid < len is satisfied too.
26972706
let value = unsafe { self.get_unchecked(mid) };
26982707
if pred(value) {
26992708
left = mid + 1;

0 commit comments

Comments
 (0)