Reference equality between pointers to different types

I'm messing around with some memory management stuff which basically amounts to a custom specialised GC for a particular data graph where nodes can have different types but share a Node trait.

I need to keep a map like DashMap<*const (), u32> or DashMap<*const dyn Node, u32> to track the reference counts of the small subset of nodes that are roots. The important points here are that we're keyed on reference equality between pointers, and that they can point to different underlying types, which can even be ?Sized. These pointers won't overlap, so in the case of fat pointers I actually only need to store the address.

I'm having a surprising amount of trouble doing this neatly. The only way that I've found uses ptr.to_raw_parts().0, which is an experimental nightly feature. Is there a better way?

You can cast *const dyn Node to *const () if you want to get the address part of the trait-object pointer.

1 Like

Oh. I swear I tried that, but obviously I did something wrong. Thanks! So easy. Sorry for the trivial question.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.