-
Notifications
You must be signed in to change notification settings - Fork 377
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
Implement Hash and Eq on protobuf messages #211
Comments
@stepancheg any thoughts on this? |
Yes, I have thoughts, but I don't know the right answer. My thoughts are:
|
You're right, that should be in there.
Do you think they should not be generated by default? |
Rust doesn't implement |
Hi @stepancheg, I would like to work on this particular issue. For one, I have been trying to add the "Hash" trait derivation to the protobuf-codegen folder. Here's what I have done so far: Added the following helper functino to protobuf-gen/src/message.rs
Then updated the logic adding "Hash" to the derive vector and how to handle unknown_fields and cached_size. Currently, I have simply added [#skip(Hash)] above unknown_flied (since the Hash trait is not implemented). cached_size needs no special treatment. What I do not understand is how to have hash_derive_enabled evaluate to true. I added a member on the customize struct:
and then added logic to the update_with function in the implementation of Customize. But what I do not understand is how to add hash_derive to rustproto, i.e. in customize.rs I call:
And in rustproto, I added hash_derive as a field of value 17031 like this:
However, I have no idea how to format my .proto files such that hash_derive_enabled() = true. I believe I need to set the hash_derive field in the .proto file, but that hasn't worked yet. Is this because I have to modify the file_descriptor_proto_data??? Have I chosen the wrong field number? Also, what modifications would I need to make to the protobuf directory in order to use protobuf instead of protobuf-codegen-pure to generate my rust files from .proto files? Thanks for your help. I can add a PR if you would prefer |
@trustyrust what is BTW, I think that if a message has float fields, then
If I understood the question correctly. However, this options can be set programmatically when |
Hi @stepancheg, Thanks for the help. Regarding skip(Hash), sorry I was mistaken. I wanted to implement something like here: rust-protobuf/protobuf-codegen/src/message.rs Line 391 in 838c873
Otherwise I could implement the Hash trait on unknown_fields. Which would you advise is better? I'm not sure I understand the comment on how to format .proto files:
If I wanted to have the same functionality in /protobuf instead of /protobuf-codegen, how would I go about doing this? As I understand it, /protobuf is a library that uses the bindings to protoc in protoc-rust, is that correct? Would I make these modifications there? To set the options programmatically, do I just set the options in the build.rs when I execute the run command? |
Serde skip does not apply to
Hash for
It is updated manually, and should be in sync with Note that options can be specified via
To make this change, modifications are needed in:
For development, the easiest set up is writing protos and tests in E. g. look at this pair: test_oneof_expose_pb.proto proto And one last thoughtrust-protobuf has a reflection implementation. Lots of things can be done in runtime without invoking code generator (json and text format are implemented that way). When performance is not very critical, probably hashing can be implemented via reflection. This is probably not acceptable, but still need to be considered. And yet another one last thoughtProbably rust-protobuf should provide hooks for code generation. E. g. protobuf-codegen could have a callback parameter like:
So |
Task about callbacks: #338 |
Shall I assume that nobody is working on this and #338? I will have a go at solving them. |
Looks like |
I'm not sure there should be UnknownFields could be an exception as it's internal and we can judge if and how it makes sense in this particular case. But that's not the case for fields generated from proto messages. Just my two cents. I'm all in favour of implementing them for any message where they can be derived! |
rust-protobuf=3 allows user-provided callbacks, so users can insert |
For our use case it'd be really convenient to have the
Hash
andOrd
trait implemented on protobuf messages. Mainly to be able to do some caching using protobuf keys in aHashMap
orBTreeMap
.I see a way to do this by generating these traits using only the actual protobuf fields, and not the specials fields like
unknown_fields
.Is this something you'd be interested in merging?
The text was updated successfully, but these errors were encountered: