xacrimon / dashmap

Blazing fast concurrent HashMap for Rust.
MIT License
2.84k stars 140 forks source link

Trait bounds not satisfied for ParallelIterator methods? #291

Open OtherJohnGray opened 5 months ago

OtherJohnGray commented 5 months ago

I'm probably doing something really silly, however, just in case this is a legitimate issue:

With latest DashMap from github:

[package]
name = "par_maps"
version = "0.1.0"
edition = "2021"

[dependencies]
rayon = "1.8.0"
#dashmap = "5.5.3"
dashmap = { git = "https://github.com/xacrimon/dashmap.git", branch = "master", features = ["inline"] }

And the following test code:

use dashmap::DashMap;

fn main() {
    // Create a new DashMap
    let map = DashMap::new();

    // Populate the map
    for i in 0..100 {
        map.insert(i, i * 2);
    }

    // Use Rayon to iterate over the map in parallel
    map.par_iter().for_each(|pair| {
        println!("Key: {}, Value: {}", *pair.key(), *pair.value());
    });
}

I get the following error:

johngray@practice ~/w/b/par_maps (master) [127]> cargo build
   Compiling par_maps v0.1.0 (/home/johngray/workspace/benchmarks/par_maps)
error[E0599]: no method named `par_iter` found for struct `DashMap` in the current scope
  --> src/main.rs:13:9
   |
13 |     map.par_iter().for_each(|pair| {
   |         ^^^^^^^^ help: there is a method with a similar name: `iter`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `par_maps` (bin "par_maps") due to 1 previous error

And if I then add a use statement for the rayon prelude:

use dashmap::DashMap;
use rayon::prelude::*;

fn main() {
    // Create a new DashMap
    let map = DashMap::new();

    // Populate the map
    for i in 0..100 {
        map.insert(i, i * 2);
    }

    // Use Rayon to iterate over the map in parallel
    map.par_iter().for_each(|pair| {
        println!("Key: {}, Value: {}", *pair.key(), *pair.value());
    });
}

I get the following error:

    johngray@practice ~/w/b/par_maps (master) [101]> cargo build
   Compiling par_maps v0.1.0 (/home/johngray/workspace/benchmarks/par_maps)
error[E0599]: the method `par_iter` exists for struct `DashMap<{integer}, {integer}>`, but its trait bounds were not satisfied
  --> src/main.rs:14:9
   |
14 |     map.par_iter().for_each(|pair| {
   |         ^^^^^^^^
   |
  ::: /home/johngray/.cargo/git/checkouts/dashmap-d1e8e211bd771f55/626b98d/src/lib.rs:88:1
   |
88 | pub struct DashMap<K, V, S = RandomState> {
   | ----------------------------------------- doesn't satisfy `_: IntoParallelRefIterator<'_>`
   |
   = note: the following trait bounds were not satisfied:
           `&DashMap<{integer}, {integer}>: IntoParallelIterator`
           which is required by `DashMap<{integer}, {integer}>: rayon::iter::IntoParallelRefIterator<'_>`

warning: unused import: `rayon::prelude`
 --> src/main.rs:2:5
  |
2 | use rayon::prelude::*;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0599`.
warning: `par_maps` (bin "par_maps") generated 1 warning
error: could not compile `par_maps` (bin "par_maps") due to 1 previous error; 1 warning emitted

Am I doing this the wrong way, or is this an issue?

tomkarw commented 5 months ago

Have you tried with rayon feature of DashMap enabled?

dashmap = { version = "5.5.3", features = ["rayon"] }