Skip to content

Commit cd35794

Browse files
committed
Comment for why char boundaries aren't checked
1 parent 09f8885 commit cd35794

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

library/core/src/str/traits.rs

+6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ unsafe impl const SliceIndex<str> for ops::Range<usize> {
204204
assert_unsafe_precondition!(
205205
"str::get_unchecked requires that the range is within the string slice",
206206
(this: ops::Range<usize>, slice: *const [u8]) =>
207+
// We'd like to check that the bounds are on char boundaries,
208+
// but there's not really a way to do so without reading
209+
// behind the pointer, which has aliasing implications.
210+
// It's also not possible to move this check up to
211+
// `str::get_unchecked` without adding a special function
212+
// to `SliceIndex` just for this.
207213
this.end >= this.start && this.end <= slice.len()
208214
);
209215
slice.as_ptr().add(self.start)

0 commit comments

Comments
 (0)