'How can I make traits from two different libraries compatible?
I have two very similar traits from two different libraries, lets call them trait libA::Field and libB::Field. Both trait requires a significant number of other traits, both trivially derivable, like Eq, Debug, Ord, etc, but also many non derivable ones, like Add, MulAssign, etc.
I need to use a few types implementing libA::Field as libB::Field. This is what I tried so far:
First attempt gives error E0210:
impl<F: libA::Field> libB::Field for F {}
Using an indirection also gives E0210:
trait FieldAdaptor: libA::Field {}
impl<F: FieldAdaptor> libB::Field for F {}
Trying to implement the trait for the specific type also doesn't work, I get error E0117:
impl libB::Field for libA::SomeConcreteField {}
Trying to wrap the libA types inside my own type seems to work, but I'll have to reimplement a ton of trait dependencies errors E0277:
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct FieldWrapper<T>(T);
impl<T: libA::Field> libB::Field for FieldWrapper<T> {}
In this last attempt, the silly thing is that pretty much every trait required by libB::Field is already implemented by T, as it is already required by libA::Field.
Is there a generic way to use types implementing libA::Field as libB::Field without having to reimplement all traits required by libB::Field?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
