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

**URL:** <https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441>\
**Category:** help\
**Created:** [March 13, 2020, 3:17pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441 "2020-03-13T15:17:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wazzaps](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/wazzaps/32/12158_2.png) [@Wazzaps](https://users.rust-lang.org/u/Wazzaps)\
**Post date:** [March 13, 2020, 3:17pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441/1 "2020-03-13T15:17:50Z")

</div>

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:

> <https://gist.github.com/Wazzaps/3c10a6cf03dfd87d6d8c3a83eb0e0a5d>

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:

> <https://gist.github.com/Wazzaps/b8b28c7123820874c5455bf5a07ef979>

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 🙂

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [March 13, 2020, 4:00pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441/2 "2020-03-13T16:00:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wazzaps](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/wazzaps/32/12158_2.png) [@Wazzaps](https://users.rust-lang.org/u/Wazzaps)\
**Post date:** [March 13, 2020, 4:24pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441/3 "2020-03-13T16:24:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AndrewGaspar](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/andrewgaspar/32/6036_2.png) [@AndrewGaspar](https://users.rust-lang.org/u/AndrewGaspar)\
**Post date:** [March 13, 2020, 5:33pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441/4 "2020-03-13T17:33:54Z")

</div>

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](https://github.com/AndrewGaspar/rust-plugin-example)

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

---

<div class="post-metadata">

**Author:** ![Yandros](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/yandros/32/8136_2.png) [@Yandros](https://users.rust-lang.org/u/Yandros)\
**Post date:** [March 13, 2020, 7:08pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441/5 "2020-03-13T19:08:47Z")

</div>

cc @Lokathor @matt1992

---

<div class="post-metadata">

**Author:** ![Wazzaps](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/wazzaps/32/12158_2.png) [@Wazzaps](https://users.rust-lang.org/u/Wazzaps)\
**Post date:** [March 13, 2020, 7:19pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441/6 "2020-03-13T19:19:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AndrewGaspar](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/andrewgaspar/32/6036_2.png) [@AndrewGaspar](https://users.rust-lang.org/u/AndrewGaspar)\
**Post date:** [March 13, 2020, 7:34pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441/7 "2020-03-13T19:34:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [June 11, 2020, 7:34pm UTC](https://users.rust-lang.org/t/trying-to-build-a-plugin-system-that-uses-rust-abi-all-compiled-using-the-same-compiler/39441/8 "2020-06-11T19:34:24Z")

</div>

Closed on June 11, 2020, 7:34pm UTC

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