You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
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?
The text was updated successfully, but these errors were encountered:
Borrow might work for you, except Either doesn't implement that, and I think it would be a breaking change to add that as a new blanket impl.
Besides, you might really want Deref, especially since the compiler will auto-deref in some cases. Then I think your Own is a reasonable solution. I don't think this problem is so specific to Either though, for the solution to belong in this crate.
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 ofCow
.Currently
Either
has thisDeref
impl:Which is nice for
Either<&T, Box<T>>
or even things likeEither<Ref<'a, T>, Either<Rc<T>, Box<T>>>
etc. ButDeref
isn't implemented forEither<T, &T>
which is a shame, becauseCow
also won't work here unlessT: ToOwned<Owned = T>
, which it mostly isn't unlessT: Clone
. An impl forEither
that would work there would look like this:But it can't co-exist with the current implementation.
A workaround is to introduce a newtype:
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?The text was updated successfully, but these errors were encountered: