I want to pass a generic type P to HashMap<String, P) in the following function, but the compiler seems to interpret P as an empty struct instead of a generic type. Is this a bug, if not, is there a way to workaround it?
use std::{collections::HashMap, sync::{Arc, RwLock}};
/// Clear any prior entries
pub fn clear_map<P>(parse_map: &Arc<RwLock<HashMap<String, P>>>) -> Result<(), ()>
{
{
match parse_map.write() {
Ok(mut map) => {
for (_key, entry) in map.iter_mut() {
entry.value.clear();
}
}
Err(e) => {
println!("clear_map failed to lock {}", format!("{}", e));
return Err(());
}
}
}
Ok(())
}
fn main() {
}
Compiling generic-test v0.1.0 (generic-test)
error[E0609]: no field `value` on type `&mut P`
--> src\main.rs:10:27
|
4 | pub fn clear_map<P>(parse_map: &Arc<RwLock<HashMap<String, P>>>) -> Result<(), ()>
| - type parameter 'P' declared here
...
10 | entry.value.clear();
| ^^^^^
error[E0609]: no field `value` on type `&mut P`
--> src\main.rs:10:27
|
4 | pub fn clear_map<P>(parse_map: &Arc<RwLock<HashMap<String, P>>>) -> Result<(), ()>
| - type parameter 'P' declared here
...
10 | entry.value.clear();
| ^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0609`.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0609`.