Documented unsafe blocks

This commit is contained in:
lch361 2023-12-25 01:59:37 +03:00
parent b15e13760a
commit c082c1c4c3

View File

@ -458,8 +458,12 @@ fn next(&mut self) -> Option<&'a [T]> {
match self.v.iter().position(|x| (self.pred)(x)) { match self.v.iter().position(|x| (self.pred)(x)) {
None => self.finish(), None => self.finish(),
Some(idx) => { Some(idx) => {
let ret = Some(unsafe { self.v.get_unchecked(..idx) }); let (left, right) =
self.v = unsafe { self.v.get_unchecked(idx + 1..) }; // 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 ret
} }
} }
@ -491,8 +495,12 @@ fn next_back(&mut self) -> Option<&'a [T]> {
match self.v.iter().rposition(|x| (self.pred)(x)) { match self.v.iter().rposition(|x| (self.pred)(x)) {
None => self.finish(), None => self.finish(),
Some(idx) => { Some(idx) => {
let ret = Some(unsafe { self.v.get_unchecked(idx + 1..) }); let (left, right) =
self.v = unsafe { self.v.get_unchecked(..idx) }; // 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 ret
} }
} }