0

Background

I'm fairly new to Rust, and I'm trying to set up a hash table using the CRC32 hash in a #[no_std] environment, without an allocator.

I found heapless::IndexMap which is generic around the hashing algorithm, so I was hoping to implement this with the hasher from the crc32fast crate. The heapless crate also provides heapless::FnvIndexMap which is their default implementation using hash32::FnvHasher.

Problem

I unsurprisingly failed at implementing heapless::IndexMap with the crc32fast::Hasher (bonus points if you can figure that out), so for my own understanding/debugging, I decided to try and reimplement FnvIndexMap. Copying the exact type definition of FnvIndexMap into my own crate failed, and unfortunately the compiler error hasn't pointed me in the right direction.

The FnvIndexMap definition (along with the rest of the IndexMap source) can be found here: https://github.com/japaric/heapless/blob/main/src/indexmap.rs#L56

Example

Cargo.toml:

[dependencies]
heapless = "0.7.16"
hash32 = "0.3.1"

main.rs:

use hash32::{BuildHasherDefault, FnvHasher};
use heapless::{IndexMap, FnvIndexMap};

type MyIndexMap<K, V, const N: usize> = IndexMap<K, V, BuildHasherDefault<FnvHasher>, N>;

fn main() {
    let fnv_map: FnvIndexMap<u32, u32, 256> = FnvIndexMap::new();
    let my_map: MyIndexMap<u32, u32, 256> = MyIndexMap::new();
}

What I expected: The crate compiles correctly.

What actually happens:

error[E0599]: no function or associated item named `new` found for struct `IndexMap<_, _, hash32::BuildHasherDefault<FnvHasher>, _>` in the current scope
 --> src\main.rs:8:57
  |
8 |     let my_map: MyIndexMap<u32, u32, 256> = MyIndexMap::new();
  |                                                         ^^^ function or associated item not found in `IndexMap<_, _, BuildHasherDefault<FnvHasher>, _>`
  |
  = note: the function or associated item was found for
          - `IndexMap<K, V, hash32::BuildHasherDefault<S>, N>`

If you can point me in the right direction for why this isn't working, I should be able to figure out how to get it working with crc32fast::Hasher.

  • 1
    You most likely have two versions of `hash32` in your dependencies. Check the output of `cargo tree` and downgrade hash32 to 0.2. Imho, this is heapless' fault, they should reexport `hash32::BuildHasherDefault` if it's part of their public API. (Since they already reexport (`pub use`) some things, submitting a PR that does so might be well-received. ;)) – Caesar Apr 20 '23 at 07:49
  • @Caesar Thank you. Changing to `hash32 = "0.2.1"` in `Cargo.toml` did the trick to allow it to compile. Now to try and get it working with `crc32fast::Hasher` - hopefully it will be more straightforward. – DeathDonkey387 Apr 20 '23 at 08:00

0 Answers0