I have the following code:
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
#[test]
fn test_hash() {
trait LetMeHashed: Hash{}
struct TraitTry{
val: usize
}
impl Hash for TraitTry {
fn hash<H: Hasher>(&self, state: &mut H) {
self.val.hash(state);
}
}
impl LetMeHashed for TraitTry{}
let new = Box::new(TraitTry{val:1}) as Box<dyn LetMeHashed>;
let mut new_hash = HashMap::new();
new_hash.insert(new, 1);
assert!(new_hash.contains(new.clone()))
}
I got the trait LetMeHashed cannot be made into an object
error. Can I somehow implement a HashMap
or HashSet
while using Arc<dyn SomeTrait>
as the key?