Borrow to another type


#1

I have these two wrapper types:

struct Wrapper(Box<Trait>);
struct RefWrapper<'a>(&'a Trait);

(They are necessary, because of the Eq and Hash trait, as they are not implementable on trait objects due to trait object safety rules. So im doing some magic to implement Eq and Hash on the wrappers.)

Now i need to borrow from Wrapper to RefWrapper:

impl <'a> Borrow<RefWrapper<'a>> for Wrapper {
    fn borrow(&'a self) -> RefWrapper<'a> {
        RefWrapper (self.0.borrow())
    }
}

but that isn’t working for obvious reason:

method `borrow` has an incompatible type for trait
pub trait Borrow<Borrowed: ?Sized> {
    fn borrow(&self) -> &Borrowed;
}

#2

This is very similar to the problems with Deref only returning borrowed values: https://github.com/rust-lang/rfcs/issues/997. I am not aware of any solutions.