I am working on a project relying on starknet-rs which defines a lot of types (enum and structs) that we want to reuse.
However, it's not straightforward to implement a trait on an external type (Why does Rust prevent implementing an external trait for an external struct?)
So currently, we end up having things like
enum OpcodeWrapper {
NOp,
AssertEq,
Call,
Ret,
}
impl From<Opcode> for OpcodeWrapper {
fn from(value: Opcode) -> Self {
match value {
Opcode::AssertEq => Self::AssertEq,
Opcode::Call => Self::Call,
Opcode::NOp => Self::NOp,
Opcode::Ret => Self::Ret,
}
}
}
impl From<OpcodeWrapper> for Opcode {
fn from(value: OpcodeWrapper) -> Self {
match value {
OpcodeWrapper::AssertEq => Self::AssertEq,
OpcodeWrapper::Call => Self::Call,
OpcodeWrapper::NOp => Self::NOp,
OpcodeWrapper::Ret => Self::Ret,
}
}
}
so that we can do whatever we want with the Wrapper
type. Is there any way to avoid this sort of copy/pasta? And since the two types are not the "same", all the subsequent .into()
?