Trying to build a plugin system that uses Rust ABI (All compiled using the same compiler)

I'm trying to build an app and trying to optimize memory footprint by separating the logic into plugins.
The app will be compiled along will all the plugins. I'll verify that the plugins were compiled together with the app somehow (.build_id perhaps).

The important part is being able to use Rust types in the parameters and name mangling.
Here's the ideal implementation:

I'm trying to avoid dropping to a C ABI, especially since I don't have to do language interop.

Here's what I have so far:

This is especially fragile because the link_name part changes whenever you change things in Cargo.toml (it's a hash), and no IDE will deal with this properly (i.e. go to definition).

Thanks for the help and sorry for my english :slight_smile:

You should use #[no_mangle] on fn hello() to prevent name mangling.

I want name mangling, I want both abc::my_func() and xyz::my_func() to coexist.
I want to avoid the whole extern "Rust" { ... } part, and simply use abc::my_func;

I made a demonstration of implementing a plugin system in Rust here: GitHub - AndrewGaspar/rust-plugin-example: An example of implementing a plugin model in Rust

In short: You need exactly one #[no_mangle], but the rest of your plugin can just be straight Rust.

2 Likes

cc @Lokathor @matt1992

This looks awesome! I'll play with it, if I don't manage to make it work without a "core" module i'll mark yours as the answer

1 Like

I suppose you could instead have you primary crate produce both a bin and a library, and then have the plugins depend on the lib. But I'm not sure that's a wise choice.

Ultimately you should keep that core crate pretty lean and generally communicate through trait objects. This reduces the coupling of the plugins to the application and increases the chance that you can introduce new capabilities without having to rebuild all the plugins, if that's important to you. But if you just pass structs around, any time you change anything in the layout of that struct, you risk having to rebuild all the plugins.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.