0

I have a struct I need to implement multiplication for. The multiplication can either be with a value I can turn into some scalar (i8, usize etc.) or the same as the struct

struct Eg<const N: usize, T> ([T; N]); impl<const N: usize, T, Q> std::ops::Mul<Q> for Eg<N, T> where Q: From<isize> { type Output = Self; fn mul(self, rhs: Q) -> Self::Output { } } impl<const N: usize, const P: usize, T> std::ops::Mul<Eg<P, T>> for Eg<N, T> { type Output = Eg<P, T>; fn mul(self, rhs: Self) -> Self::Output { } } 

Since Eg does not implement From<isize> the compiler would have to chose use the second option. Rust isn't a fan of this and says there are conflicting implementations of trait 'std::ops::Mul'.

I am aware of this question. This has not been updated (aside for editing) for over 8 years and I assume things will have changed since then

If there's any way I can specify what implementation the compiler looks at for specific multipliers (rhs) that would be very helpful. Thanks


The full code is fairly large and would be less helpful here than the sample given. the main points (I think) are still maintained:

  • Different number of constants for rach implementation (with a different number of generics in the full code)
  • Different output types
  • Differet multiplier types (Q vs Eg<P, T>)
6
  • What is Q? I don't see it declared. Commented Aug 15, 2022 at 0:02
  • The second impl also has a generic parameter N that is not used anywhere in either the trait or the type it's being implemented on, which is disallowed -- there would be no way to specify this parameter in usage of the implementation. Commented Aug 15, 2022 at 2:03
  • Sorry. Missed them out. The code should be correct now Commented Aug 15, 2022 at 16:31
  • Q is still not declared on the second impl. Commented Aug 17, 2022 at 9:36
  • Q shouldn't really have been there sorry Commented Aug 18, 2022 at 18:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.