Conversion between the same type but different const generics

I'm currently running into an issue with the default impl<T> std::convert::From<T> for <T> when it comes to const generics. Currently I have the following:

struct Decimal<const S: u32>(/* fields unimportant */);

impl<const S1: u32, const S2: u32> From<Decimal<S1>> for Decimal<S2> {
    fn from(previous: Decimal<S1>) -> Self {
        // Mathematical conversions from S1 to S2
        Decimal<S2>(/*fields*/)
    }
}

This however conflicts with the default From implementation from one type to the same type. Can I opt out of that default implementation? Or do I really need to create a custom into method for this?

Edit link to playground

You can't opt out of the blanket implementation; you need some other way to avoid the coherence conflict. I don't think there is one on stable.

I gave this a shot on nightly but coherence couldn't see through it.

If the set of parameters is small enough, you can implement for individual ordered pairs of const parameters (e.g. via macro).

Thats really unfortunate since event the default implementation fails to compile...

Maybe this would make for a good issue upstream?

1 Like

No idea, try and see I suppose.

done

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.