Skip to content

Commit

Permalink
Comment for why char boundaries aren't checked
Browse files Browse the repository at this point in the history
  • Loading branch information
pitaj committed Mar 4, 2023
1 parent 09f8885 commit cd35794
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/core/src/str/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ unsafe impl const SliceIndex<str> for ops::Range<usize> {
assert_unsafe_precondition!(
"str::get_unchecked requires that the range is within the string slice",
(this: ops::Range<usize>, slice: *const [u8]) =>
// We'd like to check that the bounds are on char boundaries,
// but there's not really a way to do so without reading
// behind the pointer, which has aliasing implications.
// It's also not possible to move this check up to
// `str::get_unchecked` without adding a special function
// to `SliceIndex` just for this.
this.end >= this.start && this.end <= slice.len()
);
slice.as_ptr().add(self.start)
Expand Down

0 comments on commit cd35794

Please sign in to comment.