RcAddr<K> is like a Rc<K>, except Eq/Hash is defined based on address of K rather than value of K. WeakAddr<K> is like Weak<K> except Eq/Hash is defined based on address of Krather than value of K.
MagicRc<K> is sorta like a RcAddr<k> MagicWeak<K> is sorta like a WeakAddr<k> MagicHashSet<K, V> is sorta like a Rc<RefCell<HashSet<MagicWeak<K>, V>>>
Now, here is the magical part. When the last
MagicRc<K> is dropped for a given
K (i.e. the strong ref count goes to 0, and the drop on
K itself is about to be called), I want a drop handler to fire off and rmove the element from all
MagicHashSet<K, V> that has a
WeakAddr<K> to the same object.
Put another way: although
MagicHashSet<K, V> is sorta like a
Rc<RefCell<HashSet<Magic<K>, V>>> , except when drop handlers are executing, all of the keys should be convertable to a
MagicRc<K>. This is because when a
MagicRc<K> hits ref count 0, in it's drop handler, it calls remove on itself from all
Rc<RefCell<HashSet<MagicWeak<K>, _>>> for the relevant
Sorry for the confusing terminology above. The logic of 'Magic' here is as follows:
RAII/drop has this amazing property where when an object goes out of scope, we can use it to free resources.
I want a "magical" Rc/Weak with the property tht when the RefCount of the MagicRc goes to 0, it looks around at all hash maps using a MagicWeak as key, and removes the (k,v) pair if the MagicWeak is pointing to object of strong ref count 0.
Now, having it search through all memory for this is unreasoanble, so I'd like this class
MagicHashMap , which someone does some type of tracking along with
MagicRc to achieve this.
I'm currently trying to hack something together. I think it can be done, but it is very messy so far. I am wondering if this problem already has a solution (or a name).
EDIT: (In response to @2e71828 's excellent question): "Lazy removal" (i.e. next time HashMap is accessed) does not work for this application. The reason is that I am keeping "snapshots" of fairly complicated data structures in memory. As soon as I no longer need a "snapshot", I want everything realted to that snapshot to be freed / dropped.