Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I accept a Rust HashMap with any hashing algorithm?

Tags:

hashmap

rust

I'm working on a library that has a function that I would like to be able to accept a HashMap of any hashing algorithm. But when I try to call it with a particular type of HashMap, such as a FnvHashMap, the compiler complains.

pub fn func(map: HashMap<u32, u32>) {}

let map: FnvHashMap<u32, u32> = FnvHashMap::default();
func(map);
error[E0308]: mismatched types
  --> lib.rs:42:10
   |
42 |     func(map);
   |          ^^^ expected struct `RandomState`, found struct `BuildHasherDefault`
   |
   = note: expected struct `HashMap<_, _, RandomState>`
              found struct `HashMap<_, _, BuildHasherDefault<FnvHasher>>`

As FnvHashMap is only a type alias I thought this wouldn't be a problem.

I'm sure there's a way to do this, but I haven't found it yet. What's the correct way?

like image 386
curiousdannii Avatar asked Mar 01 '21 00:03

curiousdannii


People also ask

How do you access a HashMap in Rust?

We access values in a hashmap by using the get() method with the key of the value we want to access. The example above will print the values associated with the “NV” and “NY” keys. When we try to access an element that doesn't exist with get(), it will return None (Rust's version of null) instead of panicking.

What are Hashmaps good for?

Hashmaps are probably the most commonly used implementation of the concept of a map. They allow arbitrary objects to be associated with other arbitrary objects. This can be very useful for doing things like grouping or joining data together by some common attribute.

What are the operations of HashMap in rust?

There are many operations for Rust HashMap execution like insertion, removal, iteration, addition, and many more. The key and value present within the hash table helps in one or the other way in taking decision regarding the accessibility of elements. HashMap uses hashing algorithm which is selected for providing resistance against HashDos attack.

How does the HashMap method work?

The hash map is initially created with a capacity of 0, so it will not allocate until it is first inserted into. Creates an empty HashMap with at least the specified capacity. The hash map will be able to hold at least capacity elements without reallocating. This method is allowed to allocate for more elements than capacity.

How does HashMap protect against HashDoS attacks?

By default, HashMap uses a hashing algorithm selected to provide resistance against HashDoS attacks. The algorithm is randomly seeded, and a reasonable best-effort is made to generate this seed from a high quality, secure source of randomness provided by the host without blocking the program.

How do I create a hashmap with a custom key type?

A HashMap with a known list of items can be initialized from an array: HashMap implements an Entry API, which allows for complex methods of getting, setting, updating and removing keys and their values: The easiest way to use HashMap with a custom key type is to derive Eq and Hash . We must also derive PartialEq. Creates an empty HashMap.


1 Answers

The definition of HashMap in the docs is:

pub struct HashMap<K, V, S = RandomState> { /* fields omitted */ }

RandomState is the default type, but it can be overridden with another type, including a generic. It may not be immediately clear, but most of the HashMap methods are declared in an impl block that constraints S: BuildHasher, which is the trait you need to bound it by:

use std::hash::BuildHasher;

pub fn func<S: BuildHasher>(map: HashMap<u32, u32, S>) {}
like image 192
Peter Hall Avatar answered Oct 26 '22 04:10

Peter Hall