-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Undesirable(?) implementation of DerefMut on BytesMut #757
Comments
Note: There is an existing issue around this trait/type (#387) but I do not believe this is describing the same issue arising from this. |
FYI, we just opened a call for breaking changes. |
I think this should be possible to implement using the semver trick.
I'll have another look on the breaking changes issue to see how things are being coordinated, and if I have time would be happy to try raise PRs for both parts. |
The semver trick works explicitly on re-exports, it works because the compiler sees that |
It's impossible to remove the impl without breakage and it happens that there's a similar issue in |
Note: I am a relatively new Rust user - so if the following is a skill issue please let me know 😅🙏
When
BytesMut
is implicitly coerced into a&mut [u8]
through it's implementation of theDerefMut
trait, callers may mutate the buffer but will not be able to advance the internal cursor.In the 80% case, this is fine and will be caught during development time - whilst data will have been written to the underlying storage, the length will show as 0 and the developer will realise their mistake at runtime.
However this assumption does not always hold true - an empty buffer can still have valid semantics, especially in context of handling binary data. Below is an example of this deserialising protobuf messages using the
prost
library (noting thatprost::bytes::BytesMut
is a re-export of this crates types) where this behaviour causes an issue.It seems like a footgun to allow users to implicitly and automatically allow coercion - this removes the usefulness of the type system to steer engineers to the correct behaviour. Especially so given we already implement the
BorrowMut
requiring an explicit call.Without deep understanding of how this crate is used I would suggest:
BorrowMut
DerefMut
trait for this type as a breaking changeThe text was updated successfully, but these errors were encountered: