Skip to content
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

[RUST] Add Eq and Hash Trait to Rust client struct to allow Hashset usage #20413

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Snazzie
Copy link

@Snazzie Snazzie commented Jan 8, 2025

Resolves #20412
Hashset insert requires Eq and Hash traits.. This change will allow inserting generated struct into Hashsets.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@Snazzie
Copy link
Author

Snazzie commented Jan 8, 2025

@Snazzie Snazzie marked this pull request as ready for review January 8, 2025 03:31
@wing328
Copy link
Member

wing328 commented Jan 8, 2025

@Snazzie thanks for the PR

can you please review the CI failures when you've time?

@Snazzie
Copy link
Author

Snazzie commented Jan 8, 2025

Hmm, my rust and mustache knowledge is a bit low. Im not sure the best way to support for number types (i16, f32 etc) with Eq and Hash.
would need support from someone more experienced.

it looks like i have to do separate implements like so. where need conditional hash implementation on all properties.

@wing328

impl Eq for PizzaSpeziale {}

if type is f64

impl Hash for PizzaSpeziale {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.pizza_size.map(|v| v.to_bits()).hash(state);
    }
}

if type is i32

impl Hash for PizzaSpeziale {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.pizza_size.map(|v| v.to_be()).hash(state);
    }
}

perhapse in mustache something like this

impl Eq for {{{classname}}} {}
impl Hash for {{{classname}}} {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // Combine hashes of all fields
        {{#vars}}
            {{#isInteger}}
                self.{{name}}.map(|v| v.to_bits()).hash(state);
        {{/vars}}
    }
}

or keep existing trait and use wrappers for number types.

struct F64Wrapper(f64);

@wing328
Copy link
Member

wing328 commented Jan 8, 2025

Im not sure the best way to support for number types (i16, f32 etc) with Eq and Hash.

what about using isLong and isShort in the template?

e.g.

{{#isLong}}
attemptParsing |= (token == JsonToken.VALUE_NUMBER_INT);
{{/isLong}}
{{#isShort}}
attemptParsing |= (token == JsonToken.VALUE_NUMBER_INT);
{{/isShort}}

@devhl-labs
Copy link
Contributor

Am i alone in thinking that eq and hash should not be implemented by default? Im assuming your including all properties in the hash calculation, but what if the entity has an id property? I would expect only the id to be considered in the hash. Since we cant tell which properties are the id imho we should not implement hash or equals.

@Snazzie
Copy link
Author

Snazzie commented Jan 8, 2025

Am i alone in thinking that eq and hash should not be implemented by default? Im assuming your including all properties in the hash calculation, but what if the entity has an id property? I would expect only the id to be considered in the hash. Since we cant tell which properties are the id imho we should not implement hash or equals.

I see your point, there can be different needs. However i think the most common expected behavior when you're doing equality comparison between two objects, every property is considered, especially in a hashset. And if you need a different equality behavior you would override or separately code it in consumer function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Rust client Structs missing Eq and Hash trait
3 participants