I've been thinking about the
Borrow trait, for reasons that I'll come to a little further on. But first, some observations:
- the signature of the trait's
borrowmethod necessitates that the
Borrowedvalue is some subset of the memory borrowed by
- the trait requires that "
Hashmust be equivalent for borrowed and owned values".
Can't we therefore conclude that whatever state is used by the
Self type to implement
Hash must also be within the subset of its memory that is used by the
Borrowed type? Indeed, that state must be within every such
Borrowed type supported by the
Shouldn't it therefore be possible to specify a minimal type
T which includes (only) that state, such that all
Borrowed types transitively derived from any given
Self can also implement
Hash only need be explicitly implemented for that minimal
T, with implicit implementations inferable for any type that implements
Borrow<T>. Thus the trait's logic requirement is enforced.
I'm not necessarily proposing that any of this should be done, only observing that it could be (at least in theory, absent stability concerns etc).
The particular use-case that got me to thinking about this was that posed in the following StackOverflow question:
Here the solution is probably to create a map type that holds the runtime-defined comparator, but if that comparator is defined in terms of the key type
K then the convenience of accessing the map via borrowed versions of the key (e.g. of type
K: Borrow<Q>) becomes very much more complicated: somehow the comparator needs to be converted to work in terms of such borrowed type(s). It seems to me that, instead, the comparator only need be defined in terms of the minimal type
T, which all lookups (whether using key type
K or some borrowed type
Q thereof) will be able to borrow.
It may well be that such complexity is not required in this case, or indeed that the solution can be implemented with explicit knowledge of the actual types in use, but nevertheless I can't help wondering if perhaps the current design of
Borrow lacks some important/useful expressivity here?