Skip to content

Commit c6810a5

Browse files
committed
Clarify safety comment on using i to index into self.source
1 parent 2be9a83 commit c6810a5

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/core/src/str/lossy.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,14 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
122122
valid_up_to = i;
123123
}
124124

125-
// SAFETY: `i <= self.source.len()` because it only ever increments by 1
126-
// and the loop is terminated as soon as that goes beyond bounds.
125+
// SAFETY: `i <= self.source.len()` because it is only ever incremented
126+
// via `i += 1` and in between every single one of those increments, `i`
127+
// is compared against `self.source.len()`. That happens either
128+
// literally by `i < self.source.len()` in the while-loop's condition,
129+
// or indirectly by `safe_get(self.source, i) & 192 != TAG_CONT_U8`. The
130+
// loop is terminated as soon as the latest `i += 1` has made `i` no
131+
// longer less than `self.source.len()`, which means it'll be at most
132+
// equal to `self.source.len()`.
127133
let (inspected, remaining) = unsafe { self.source.split_at_unchecked(i) };
128134
self.source = remaining;
129135

0 commit comments

Comments
 (0)