0

So I have a struct

#[derive(Clone, Debug)]
pub struct Polynomial {
    pub coefficients: Vec<BIG>,
}

It has 2 MulAssign methods as follows

impl<B: Borrow<Self>> ops::MulAssign<B> for Polynomial {...} 
impl ops::MulAssign<BIG> for Polynomial {...}

all pretty standard so far. All of this compiles and works as intended.

I am reimplementing this using generics so we can have polynomials of any type that implement Big. Specifically, we have a trait Trait with type big implementing Big. Trait also holds other traits that would need to be consistent with big.

trait Trait {
    type big: Big; // this makes sure big works the same as BIG
    type othertype: OtherType<big = Self::big>;
    ...
}

I attempted to create a generic polynomial for trait as below. Note that _Polynomial must be generic on Trait since some other functions (say for othertype) may need a polynomial using the same kind of Big.

#[derive(Clone, Debug)]
pub struct _Polynomial<T: Trait> {
    pub coefficients: Vec<T::big>,
}

I also tried to recreate the MulAssign functions with headers below, and here is where my issue arises

impl<T: Curve, B: Borrow<Self>> ops::MulAssign<B> for _Polynomial<T> {...}
impl<T: Curve> ops::MulAssign<T::big> for _Polynomial<T> {...}

I get an error "conflicting implementation for polynomial::_Polynomial<_>".

I mostly understand WHY this is happening (Conflicting implementations of trait in Rust, Unable to implement Into trait for a generic struct) but I couldn't get the work around in those posts, or any of the other workarounds I found online.

I am still confused about why the initial version compiles without any errors since the method could be considered overloaded (how do we know "BIG" doesn't implement Borrow?)

I have spent many hours working at this and while I feel like I've learned a lot digging through stack overflow posts I still haven't found a workaround that works.

Any and all help is appreciated, thanks!

isaactfa
  • 5,461
  • 1
  • 10
  • 24
  • 3
    *"how do we know "BIG" doesn't implement Borrow?"* - I assume `BIG` is something defined in your crate and only your crate is allowed to implement `Borrow for BIG` and thus the compiler has full knowledge that it doesn't conflict. – kmdreko Nov 07 '22 at 19:13
  • Correct, BIG is defined in the crate. Confirming my understanding of why the generic case doesn't work, we can't say that we know T::big doesn't implement Borrow<_Polynomial> in the same way since we don't know T and thus T::big at compile time? – gormatron3000 Nov 07 '22 at 19:23

0 Answers0