From c082c1c4c37cb672e295a4cb1700fafb51d1e824 Mon Sep 17 00:00:00 2001 From: lch361 Date: Mon, 25 Dec 2023 01:59:37 +0300 Subject: [PATCH] Documented unsafe blocks --- library/core/src/slice/iter.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index a20b7c9bef0..3d58afd26ea 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -458,8 +458,12 @@ fn next(&mut self) -> Option<&'a [T]> { match self.v.iter().position(|x| (self.pred)(x)) { None => self.finish(), Some(idx) => { - let ret = Some(unsafe { self.v.get_unchecked(..idx) }); - self.v = unsafe { self.v.get_unchecked(idx + 1..) }; + let (left, right) = + // SAFETY: if v.iter().position returns Some(idx), that + // idx is definitely a valid index for v + unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) }; + let ret = Some(left); + self.v = right; ret } } @@ -491,8 +495,12 @@ fn next_back(&mut self) -> Option<&'a [T]> { match self.v.iter().rposition(|x| (self.pred)(x)) { None => self.finish(), Some(idx) => { - let ret = Some(unsafe { self.v.get_unchecked(idx + 1..) }); - self.v = unsafe { self.v.get_unchecked(..idx) }; + let (left, right) = + // SAFETY: if v.iter().rposition returns Some(idx), then + // idx is definitely a valid index for v + unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) }; + let ret = Some(right); + self.v = left; ret } }