basecoin-rs icon indicating copy to clipboard operation
basecoin-rs copied to clipboard

Investigate: `Module::init()` should be type-safe

Open plafer opened this issue 2 years ago • 4 comments

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);
}

plafer avatar Apr 26 '22 19:04 plafer

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.

romac avatar Apr 27 '22 08:04 romac

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;?

plafer avatar Apr 27 '22 12:04 plafer

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.

romac avatar Apr 27 '22 12:04 romac

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!

plafer avatar Apr 27 '22 19:04 plafer