I have several different kinds of structs that I'm serializing and deserializing. I created a trait (DoesSomething
below) to capture this behavior so I don't have to reimplement it for each struct, I can just add it with a one line impl DoesSomething for MyType {}
Some of these types include a CRC (CanDoSomethingElse
), others don't. For the ones that do include the CRC, I want to check the CRC after deserializing, or calculate the CRC before serializing. I wanted to reimplement the methods of DoesSomething
to include the CRC checking when implemented on a type that implements the CRC trait CanDoSomethingElse
However, I'm getting "conflicting implementations" -- what's the right way to reimplement the methods of DoesSomething
depending on the other traits of the struct that DoesSomething
is implemented on?
Here's a toy example
trait CanDoSomething {}
trait CanDoSomethingElse {}
pub trait DoesSomething {}
impl<T> DoesSomething for T where T: CanDoSomething {}
impl<T> DoesSomething for T where T: CanDoSomethingElse {}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `DoesSomething`:
--> src/lib.rs:9:1
|
7 | impl<T> DoesSomething for T where T: CanDoSomething {}
| --------------------------------------------------- first implementation here
8 |
9 | impl<T> DoesSomething for T where T: CanDoSomethingElse {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation
error: aborting due to previous error
For more information about this error, try `rustc --explain E0119`.
error: could not compile `playground`
To learn more, run the command again with --verbose.