Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic From impl for newtypes #20

Open
dhardy opened this issue Oct 8, 2019 · 2 comments
Open

Generic From impl for newtypes #20

dhardy opened this issue Oct 8, 2019 · 2 comments

Comments

@dhardy
Copy link

dhardy commented Oct 8, 2019

Creating a new issue because this is, in my opinion, probably the most commonly encountered and annoying to work around issue caused by no-orphan rules:

struct NewType<T>(T);

impl<A, B: From<A>> From<NewType<A>> for NewType<B> {
    fn from(a: NewType<A>) -> NewType<B> {
        NewType(a.0.into())
    }
}

Namely, the special case where A == B overlaps with the generic impl<T> From<T> for T.

It's also an instance where it's usually trivial to prove that the overlapping implementations are equivalent, though I realise this is probably not a productive path toward a solution.

@Ixrec
Copy link
Owner

Ixrec commented Oct 8, 2019

Would specialization help with this case?

(I don't understand specialization enough to figure that out myself)

@dhardy
Copy link
Author

dhardy commented Oct 8, 2019

I presume it would, so long as the most generic impl uses the default keyword (assuming specialization will work roughly as in the RFC).

impl<T> From<T> for T {
    default fn from(t: T) -> T { t }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants