Comment for why char boundaries aren't checked

This commit is contained in:
Peter Jaszkowiak 2023-03-04 15:11:24 -07:00
parent 09f8885b3b
commit cd35794d5e

View file

@ -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)