Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate: Module::init() should be type-safe #50

Open
plafer opened this issue Apr 26, 2022 · 4 comments
Open

Investigate: Module::init() should be type-safe #50

plafer opened this issue Apr 26, 2022 · 4 comments

Comments

@plafer
Copy link
Contributor

plafer commented Apr 26, 2022

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);
}
@romac
Copy link
Member

romac commented Apr 27, 2022

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.

@plafer
Copy link
Contributor Author

plafer commented Apr 27, 2022

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

@romac
Copy link
Member

romac commented Apr 27, 2022

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.

@plafer
Copy link
Contributor Author

plafer commented Apr 27, 2022

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants