Stream: wg-traits

Topic: will specialization allow From of generics?


Jake Goulding (Feb 11 2019 at 21:42, on Zulip):
struct Wrapper<T>(T);

impl<T> From<Wrapper<T>> for T {
    fn from(other: Wrapper<T>) -> Self {
        other.0
    }
}

This fails in today's Rust because of the standard library's impl From<T> for T. Would the current proposed design of specialization allow this somehow?

tmandry (Feb 12 2019 at 00:37, on Zulip):

@Jake Goulding I don't believe so, because this is a violation of the orphan rules
which state that if a type parameter appears in the Self type of an impl for a non-local trait, it must be "covered" by a local type

tmandry (Feb 12 2019 at 00:38, on Zulip):

e.g. the following example does compile:

struct Wrapper<T>(T);
struct OtherWrapper<T>(T);

impl<T> From<Wrapper<T>> for OtherWrapper<T> {
    fn from(other: Wrapper<T>) -> Self {
        OtherWrapper(other.0)
    }
}
tmandry (Feb 12 2019 at 00:38, on Zulip):

due to OtherWrapper covering the type parameter T

tmandry (Feb 12 2019 at 00:38, on Zulip):

the accepted specialization RFC does not propose changes to this rule

tmandry (Feb 12 2019 at 00:39, on Zulip):

(see this blog post for more on the orphan rules)

tmandry (Feb 12 2019 at 00:41, on Zulip):

I know there is other work going on outside specialization to change the orphan rules to make them more permissive, though

tmandry (Feb 12 2019 at 00:41, on Zulip):

or at least interest. not sure what state all that is in

Jake Goulding (Feb 12 2019 at 02:34, on Zulip):

feature(re_rebalance_coherence) is live, but does not address this specific aspect, (due to the generic, I believe).

Jake Goulding (Feb 12 2019 at 02:34, on Zulip):

That's why I'm curious if specialization (perhaps with rebalancing) would address it

Jake Goulding (Feb 12 2019 at 02:35, on Zulip):

Since conceptually, impl From<T> for T could be seen as a specialization.

Last update: Nov 12 2019 at 17:00UTC