All right, this one's a doozy. I'm trying to make evmap
have sound aliasing of values. The linked PR has a lot more discussion around the exact safety requirements, but that's only somewhat orthogonal to this issue.
Essentially, I have a type, Aliased<T, D>
that is basically a &T
. I also have a that that holds a HashSet
that in turns stores Aliased<T, SomeType>
. SomeType
is private and must remain private for soundness (so no #[doc(hidden)]
tricks. And now, I want to provide an method for checking if a T
is in the set, and I want to do so while providing the nice ergonomic of Borrow
. So, I want to write:
pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
where
Aliased<T, SomeType>: Borrow<Q>,
Q: Eq + Hash,
{
self.set.contains(value)
}
But, this won't work, since SomeType
is now a private type in a public interface. Rats.
And so I turn to you smart people of Rust world. How can I do this without making SomeType
public, given that I have no control over Borrow
or HashSet
? Do I just have to give up on the nice ergonomics and take a &T
, or can we find some sneaky type system hack to make this work?