Closed jyn514 closed 3 years ago
I guess I'm not opposed to documenting this, although it kind of seems self evident to me? memchr
returns the position of a particular byte in a slice, if it exists. It it doesn't exist, then None
is returned. Since the largest possible size of a slice is usize::MAX
and since memchr
can never possibly return an index greater than or equal to the length of the given slice (since it would be an invalid index), it follows immediately that the value returned is guaranteed to be less than usize::MAX
.
From https://github.com/rust-lang/rust/pull/73139#discussion_r439832328:
It would be great to tell rustc this is the case by using
rustc_layout_scalar_valid_range_end(usize::MAX - 1)
. That would allow storingOption<index>
inusize
instead of needing an extra bit, which in some cases could double the size of the struct due to alignment requirements.Failing that (since
rustc_layout_scalar_valid_range_end
is unstable and likely will never be stabilized), would it be possible to document that the index is always less thanusize::MAX
?