Skip to content

Alternative Deref implementations #32

@peterjoel

Description

@peterjoel

This might not go anywhere, but here seemed like the right place to open the conversation.

I think there is a need for a type that captures the possibility of being either owned or some kind of reference, but without requiring the ToOwned constraint of Cow.

Currently Either has this Deref impl:

impl<L, R> Deref for Either<L, R> 
where
    L: Deref,
    R: Deref<Target = L::Target>,

Which is nice for Either<&T, Box<T>> or even things like Either<Ref<'a, T>, Either<Rc<T>, Box<T>>> etc. But Deref isn't implemented for Either<T, &T> which is a shame, because Cow also won't work here unless T: ToOwned<Owned = T>, which it mostly isn't unless T: Clone. An impl for Either that would work there would look like this:

impl<L, R> Deref for Either<L, R> 
where
    R: Deref<Target = L>, 

But it can't co-exist with the current implementation.

A workaround is to introduce a newtype:

struct Own<T>(T);
impl<T> Deref for Own<T> {
     type Target = T;
     fn deref(&self) -> Self::Target {
          self.0
     }
}

So you can do now use Either<Own<T>, &T>.

I'm sure many other crates have rolled their own (though I didn't find any), but it would be better if there was one canonical way to do this. What's the best way to achieve that? Try to do something in either, some other crate or a completely new one?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions