I’m using the HashMap find_equiv to look up elements in a hashmap without having to reproduce all of the object that I’m using as a key.
My example is this:
I have a struct that contains lots of metadata and only one field that is hashed or checked for equality.
pub struct Node<'a, N: 'a, C: 'a> {
pub state: RefCell<Option<N>>, // ONLY HASH AND EQ THIS FIELD
pub parent: RefCell<Option<&'a Node<'a, N, C>>>,
pub open: RefCell<bool>,
pub cost: RefCell<C>,
pub cost_with_heuristic: RefCell<C>
}
I have a large hashmap that contains these Nodes as keys.
But when I want to see if a certain state is in the map, I wrap it up in a “Dummy Node” that looks like this:
pub struct DumbNode<'b, N: 'b>(pub &'b N);
impl <'a, 'b, N: PartialEq, C> Equiv<&'a Node<'a, N, C>> for DumbNode<'b, N> {
fn equiv(&self, other: &&Node<'a, N, C>) -> bool {
let DumbNode(x) = *self;
x.eq(other.state.borrow().deref().as_ref().unwrap())
}
}
Notice the use of Equiv which lets me not have to include all of the metadata that is inside of Node.
Now with find_equiv being depricated, I’m not sure how I’ll get around this! I can’t afford to create full Node instances for every single query into the hash table. My current solution is to copy-paste the code from HashTable so that I can keep using it after it has been removed.