Your code seems to be attempting to take a generic type K with various trait bounds (Hash, Sync, Send, and ParallelIterator) and then call par_iter() on it. However, this isn't quite correct because DashMap itself has the method par_iter(), not the generic type K.
DashMap is a concurrent map and its par_iter() method is used to iterate over its entries in parallel using the Rayon crate.
Let's correct the function to take a DashMap as its argument, and then use par_iter() to iterate over it:
use dashmap::DashMap;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
pub fn write_objs<K, V>(pocket: &DashMap<K, V>)
where
K: Hash + Eq + Sync + Send,
V: Sync + Send,
{
pocket.par_iter().for_each(|entry| {
let (key, value) = entry.pair();
log::info!("{}: {}", key, value.len());
});
}
Here's what's changed:
-
The function write_objs now takes a reference to a DashMap<K, V>, where K and V are the key and value types.
-
We've removed the bound ParallelIterator from K since it is not K itself that needs to be an iterator but rather it is the DashMap that provides the parallel iterator.
-
We've also added a bound for Eq for the keys since hashing requires equality checks.
-
The error with .pair() method does not exist, so I've replaced it with .key() and .value(), but based on your function, it looks like you expect the key and value so reverted to using entry.pair() which should now correctly destructure the reference to the key-value pair.
-
The value.len() implies that V should be something that has a len() method (like Vec<String> from your description of pocket).
-
We match against the tuple (key, value) using entry.pair(), which gives us references to the key and value in the map.
Note that you'll need to include both the dashmap and rayon crates in your Cargo.toml for this code to work.
Assuming DashMap is parameterized with Pocket as the key type and Vec<String> as the value type, you would invoke write_objs in your code like this:
use dashmap::DashMap; // Make sure you have dashmap in your Cargo.toml
use std::sync::Arc; // Required for thread-safe reference counting
fn main() {
// Initialize logging or replace with your logging initialization
// Example with env_logger:
// env_logger::init();
let pocket: DashMap<Pocket, Vec<String>> = DashMap::new();
// ... Populate your DashMap with data ...
// Now call the function with a reference to the map
write_objs(&pocket);
}
Ensure to replace Pocket with the actual type you're using for your keys. The logging functionality requires that Pocket type implements Display trait so it can be formatted as a string for the log output. If Pocket does not implement Display, you'll need to add a way to convert it to a string representation.