tokio-rs / bytes

Utilities for working with bytes
MIT License
1.86k stars 278 forks source link

Explicitly guarantee `Bytes` to be immutable #692

Closed VixieTSQ closed 5 months ago

VixieTSQ commented 5 months ago

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)

Darksonn commented 5 months ago

Yes, it's guaranteed. What you're doing is fine.

VixieTSQ commented 5 months ago

Yes, it's guaranteed. What you're doing is fine.

Okay! Thank you