How to wrap a reference in a struct to make it hashable by address


#1

I am trying to figure out how to wrap a reference in a struct in order to create a HashSet of references. These references will all be allocated with an arena allocator, and thus will have identical lifetimes and remain stationary for their lifetimes.

My current attempt looks like:

struct HashableRef<'a, T> (&'a T);
struct RefSet<'a, T> (std::collections::HashSet<HashableRef<'a,T>>);

and fails because T may not live long enough. Any suggestions?

The broader context is my fac build system, where I need to construct a graph describing files and rule needed to build them (or that they are needed to build). In addition to this graph, I want to have sets of rules that have not yet been built, files that are dirty, files I haven’t yet looked at in a given operation or or another, etc. This graph is used in a single thread, and an arena allocator looks great so far. I could instead add a unique identifier, but would rather just use the address itself and remove the obligation of creating and storing unique identifiers.


#2

I think you just need to write HashableRef<'a, T: 'a>


#3

Thanks @withoutboats , that was exactly my problem!