-
Notifications
You must be signed in to change notification settings - Fork 70
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
Read/Write mutable reference in serialize
and deserialize_reader
is unnecessary
#270
Comments
serialize
and deserialize_reader
is unnecessary
it probably should've been fn deserialize_reader<R: Read>(mut reader: R) -> Result<Self>; Does this change allow any wider use cases for the traits? |
No. You do not need to specify mut in the trait definition itself, only in the implementation. See playground link below.
It's somewhat weird that you have to pass a mutable reference to a immutable buffer when you're deserializing. See playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=dd2f2606d714535fbba3e5e5b00208cc
Yes. But if your writer is a mutable reference itself then it makes sense for the mutable reference to be dropped after one object, does it not? For reference: |
I believe Rust API guidelines recommends to pass Regarding the playground examples, I think this is what by_ref() is for. Read example fixed, Write example fixed |
More info here: rust-lang/api-guidelines#174 |
Frankly speaking this is illuminating video_2023-12-13_17-22-28.mp4 |
So, do you guys intend to change this for 2.0? I also wanna add that this issue has surfaced before: #34 |
@billythedummy Thank you for raising it
We don't intent to release 2.0 any time soon unless there is a very good reason to. I have created a tracking issue for the ideas that would require us to bump the major version here: #279 |
Currently, the required method for
BorshDeserialize
has the following signature:However, it can be changed to just:
because
Read
has a blanket impl for mut references: https://docs.rs/borsh/latest/borsh/io/trait.Read.html#impl-Read-for-%26mut+RSimilarly,
BorshSerialize
can be changed fromto
because
Write
has a blanket impl for mut references: https://docs.rs/borsh/latest/borsh/io/trait.Write.html#impl-Write-for-%26mut+WThis will be a breaking change
The text was updated successfully, but these errors were encountered: