From 52f976236f0dc1a797578ae41ba1193cbed50677 Mon Sep 17 00:00:00 2001 From: VillSnow Date: Sun, 28 Jun 2020 16:25:33 +0900 Subject: [PATCH] Add comment on use of unsafe --- src/libcore/slice/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index a07690bc669f..3b51f973ffbf 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -2694,6 +2694,15 @@ impl [T] { while left != right { let mid = left + (right - left) / 2; + // SAFETY: + // When left < right, left <= mid < right. + // Therefore left always increases and right always decreases, + // and eigher of them is selected. + // In both cases left <= right is satisfied. + // Therefore if left < right in a step, + // left <= right is satisfied in the next step. + // Therefore as long as left != right, 0 <= left < right <= len is satisfied + // and if this case 0 <= mid < len is satisfied too. let value = unsafe { self.get_unchecked(mid) }; if pred(value) { left = mid + 1;