Skip to content

Commit

Permalink
Clarify safety comment on using i to index into self.source
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 26, 2021
1 parent 2be9a83 commit c6810a5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions library/core/src/str/lossy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {
valid_up_to = i;
}

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

Expand Down

0 comments on commit c6810a5

Please sign in to comment.