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

Clarify the use of PluginRegistrar as a trait object #1

Open
seatonullberg opened this issue Dec 5, 2020 · 1 comment
Open

Clarify the use of PluginRegistrar as a trait object #1

seatonullberg opened this issue Dec 5, 2020 · 1 comment

Comments

@seatonullberg
Copy link

Thank you for making this tutorial, it has been a huge help for me!

My only point of confusion has been with the use of PluginRegistrar as a trait object in random/src/lib.rs:

extern "C" fn register(registrar: &mut dyn PluginRegistrar) {
    registrar.register_function("random", Box::new(Random));
}

Then trait PluginRegistrar is implemented for struct PluginRegistrar in app/src/main.rs:

impl plugins_core::PluginRegistrar for PluginRegistrar {
    // --snip--
}

And that is the only implementation.

It seems to me like it is unnecessary to use a trait object here when the struct PluginRegistrar could be used directly.
This would require that the core module implements and exports both struct FunctionProxy and struct PluginRegistrar to be used in app. Am I missing something that prevents this or is it a matter of preference?

@StevenLu2004
Copy link

StevenLu2004 commented May 23, 2021

@Michael-F-Bryan Thank you so much for making this tutorial!

@seatonullberg I think it comes down to two things. First, having a trait PluginRegistrar allows for alternative implementations that depend on the app. Second, struct PluginRegistrar has a field lib: Rc<Library> that gets cloned to a FunctionProxy for every registered function, which means it depends on the libloading crate, and including the struct directly in plugins_core will add an unnecessary dependency of libloading to every plugin using plugins_core.

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