Are AsRef and Borrow supposed to be deterministic?

2¢: I think that it would be good if there was a single place this concept was defined, rather than wording it anew for each trait where it comes up.

In a previous thread, there was quite a long discussion about how this it is hard to define and the existing library documentation for HashMap could be taken as meaning “may do absolutely anything, but not UB”, which is not really a useful claim since “anything” could be taken to include modifying application/system state in ways that might not be technically UB but are equally destructive to the desired functioning of a program. Here's what I wrote trying to define this concept; talking about the other end — the code that uses the trait — and Hash rather than Borrow, but I think it is the same general principle.

The above is talking about HashMap, not Hash, but that's just the opposite end of the contract — though harder to nail down, since it's not clear what the abstraction/encapsulation-boundary is for “thing that uses Borrow or Hash”.

So, I think that this is a coherent concept, but it is not simple and so it deserves its own name and reference page, not just an attempt to explain it in each trait or type whose contract involves it.

2 Likes