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

Support for alternative HashMap hashing algorithms #913

Open
alexheretic opened this issue Sep 11, 2023 · 0 comments · May be fixed by #914
Open

Support for alternative HashMap hashing algorithms #913

alexheretic opened this issue Sep 11, 2023 · 0 comments · May be fixed by #914

Comments

@alexheretic
Copy link

It is currently possible to derive Message for HashMaps. However it is not possible to use an alternative hashing algorithm like rustc-hash/ahash which may be more optimal in certain situations.

Example

Take a simple Msg message with a int map field. This can be implemented using the default hasher.

use std::collections::HashMap;

#[derive(Clone, PartialEq, prost::Message)]
pub struct Msg {
    #[prost(map = "uint32, uint32", tag = "1")]
    pub map: HashMap<u32, u32>,
}

#[test]
fn decode() {
    use prost::Message;
    const FOO_BYTES: &[u8] = &[10, 4, 8, 3, 16, 4, 10, 4, 8, 1, 16, 2];

    let msg = Msg::decode(FOO_BYTES).unwrap();

    assert_eq!(msg.map.len(), 2);
    assert_eq!(msg.map[&1], 2);
    assert_eq!(msg.map[&3], 4);
}

However, were I to try to use instead pub map: rustc_hash::FxHashMap<u32, u32> as a more optimal hashing algo for u32 Message derive fails to build as it only implements for the default hasher.

$ cargo build
   Compiling prost-alt-hash v0.1.0 (/home/alex/project/prost-alt-hash)
error[E0308]: mismatched types
    --> src/lib.rs:3:28
     |
3    | #[derive(Clone, PartialEq, prost::Message)]
     |                            ^^^^^^^^^^^^^^
     |                            |
     |                            expected `&HashMap<u32, u32>`, found `&HashMap<u32, u32, ...>`
     |                            arguments to this function are incorrect
     |
     = note: expected reference `&HashMap<u32, u32>`
                found reference `&HashMap<u32, u32, BuildHasherDefault<FxHasher>>`
@alexheretic alexheretic linked a pull request Sep 11, 2023 that will close this issue
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 a pull request may close this issue.

1 participant