I'd like to write a struct that validates a Bytes as containing valid utf8 on construction, and then derefs to &str using the unsafe function std::str::from_utf8_unchecked. I can guarantee the underlying Bytes will always contain valid utf8 on deref because the struct can only be constructed after validating the Bytes.
But the problem is that the documentation for Bytes strongly implies but never explicitly guarantees immutability. Which is necessary for my safety argument to hold up, because it falls apart if the underlying bytes can mutate from under me. Am I correct in assuming Bytes is guaranteed to never mutate? And if so, can we add that to the documentation? (If I didn't just somehow miss it)
I'd like to write a struct that validates a
Bytes
as containing valid utf8 on construction, and then derefs to &str using the unsafe functionstd::str::from_utf8_unchecked
. I can guarantee the underlying Bytes will always contain valid utf8 on deref because the struct can only be constructed after validating theBytes
.But the problem is that the documentation for
Bytes
strongly implies but never explicitly guarantees immutability. Which is necessary for my safety argument to hold up, because it falls apart if the underlying bytes can mutate from under me. Am I correct in assumingBytes
is guaranteed to never mutate? And if so, can we add that to the documentation? (If I didn't just somehow miss it)