Skip to content

Consider implementing Deref on Yoke where possible #3159

Open
@Manishearth

Description

@Manishearth

Currently, to access the yoked object, Yoke has a .get() method, and does not implement Deref.

In the general case this makes sense: if the yoked object is e.g. (String, &str, Cow<str>), the signature of Deref will just not work since the output object needs to be &'a (String, &'a str, Cow<'a, str>), and the signature of Deref admits precisely one Target type.

OTOH for "normal" dereffable yoked types like &T and Cow<T> where T: 'static we actually can expose Deref to produce an &T if we want.

The impl we want looks somewhat like this:

impl<Y, C> Deref for Yoke<Y, C>
   where Y: for<'a> Yokeable<'a>, 
               Y: Deref;
               for<'a> <Y as Yokeable<'a>>::Output: Deref,
                // given how Yokeable works, this implies that the target types are all the same:
               for<'a> <<Y as Yokeable<'a>>::Output as  Deref>::Target: 'static,
{
    type Target: <Y as Deref>::Target;
    fn deref(&self) -> &Self::Target {
        self.get().deref()
    }
}

the annoying bit is that self.get().deref() has the return type &<<Y as Yokeable<'a>>::Output as Deref>::Target which the compiler cannot prove is the same as &<Y as Deref>::Target. We know it is, but the compiler doesn't.

We could do an unsafe cast here, but I'm not actually 100% sure that that is safe here (or the precise bounds needed for safety) given the valid ways to implement Yokeable and Deref. I can think about it more and get a definitive answer here.

We could also expose an unsafe trait DerefYokeable: for<'a> Yokeable<'a> + Deref that we manually implement on some known dereffy types that have 'static contained values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-zerovecComponent: Yoke, ZeroVec, DataBakeS-mediumSize: Less than a week (larger bug fix or enhancement)T-enhancementType: Nice-to-have but not requiredhelp wantedIssue needs an assignee

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions