Hasher for clonable objects with Arc

If a struct implements Clone and contains an Arc (the Arc is cloned properly, no shenanigans involved). If one wants to be able to put one of the clones in a HashSet -- is it safe to make it hashable based on address of the object the Arc points to?

Semantically it is the right thing to do (the Arcs point to the unique elements), but I'm wondering if there are any technical reasons not to do that. The memory an Arc points to doesn't move, right?

It's only logically consistent if you also actively make the PartialEq impl use the address. Otherwise, there could be two objects that compare equal but have different hashes.

It technically is safe, though – unsafe code should always be written in a way that it is impossible to cause unsoundness using logically inconsistent but safe code.

It doesn't, but why does that matter? Even if it did move around, using the pointer address for both PartialEq and Hash would remain consistent.

1 Like


1 Like

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.