Implement a trait for a struct from a package

I am using the package bigdecimal

It has a struct bigdecimal::BigDecimal, now I want to use it in a certain situation where it needs the #[derive(sbor::Decode, sbor::Encode)] methods. How do I "override" this to be able to use it in this context. I hope that makes sense, I don't posses the rustacean vocabulary yet :wink:

Is sbor your own crate? I don’t see it on Maybe you want to enable the serde feature of bigdecimal and use a serde compatible library for encoding/decoding?

I am using an experimental rust library that isn't public yet, I have found a workaround, but I still would like to learn how to do stuff like this, if it is posible.

The term you are looking for are the "orphan rules".

The general idea is that you aren't allowed to implement a foreign trait on a foreign type because a different crate could theoretically do the same thing. This would be bad, because both crates could compile just fine by themselves, but if you made a program that uses both crates it would fail with "unable to figure out which ForeignTrait implementation to use for ForeignType because it is impelmented in both crates A and B".

For your situation, that means your sbor crate would need to implement the Decode trait for bigdecimal::BigDecimal or the bigdecimal crate would need to write the implementation. If neither are possible, your crate will need to create some temporary wrapper around BigDecimal and implement Decode manually.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.