basecoin-rs
basecoin-rs copied to clipboard
Investigate: `Module::init()` should be type-safe
Currently, Module::init()
takes a json
object as input. We should investigate how to make this type-safe.
A rough sketch for a possibility would be:
trait Module {
type InitData;
fn init(&mut self, init_data: Self::InitData);
}
Why not just take Self::InitData
directly? If the caller already has a value of type Self::InitData
then they won't be able to pass it to init because afaik &T
does not implement Into<T>
, or if it does it will delegate to clone
.
Yep makes a lot more sense. Updated the description.
Although another alternative that might be best is to pass it by reference, and potentially have type InitData: Clone;
?
I don't see the upside of passing it by reference. If you know you will need ownership of the data, I would just pass an owned value and leave to the caller to clone it if needed.
It wasn't clear to me if the callee would need ownership, but I double-checked in the bank
module and we indeed require ownership. We can leave it as is then!