Import Functions
Learn how to import functions from other plugins written in different languages and use them in your own.
To use functions from another plugin in your Rust plugin, you need to generate language-specific bindings. These bindings provide the necessary wrappers to call functions exported by other plugins. This guide explains how to generate these bindings and how to use them in your Rust plugin.
Generating Rust Bindings
Plugify provides a unified generator tool to automatically generate Rust code for importing functions from other plugins. These bindings include wrapper functions that handle the function calls and parameter passing.
Steps to Generate Rust Bindings
Using the Online Generator:
Visit plugify-gen tool to generate bindings through a user-friendly web interface. Simply upload your plugin manifest file (.pplugin) and select Rust as the target language to generate the corresponding .rs file.
Using the Command-Line Tool:
You can also download and use the generator tool locally from the plugify-gen repository.
Example usage:
Integrate the Generated Module:
- The tool will generate a module folder (e.g.,
plugin_from_another_language/) in the specified output directory. - Copy the entire generated module folder to your project's
src/directory. - Declare the module in your
src/lib.rs.
Project Integration
After generating the module, integrate it into your Rust project:
Copy Module to Project
Copy the generated module folder to your project's src/ directory:
Declare Module
Declare the module in your src/lib.rs:
Import in Source Files
Import and use the generated module in your Rust files:
Using Generated Wrapper Functions
The generated module contains wrapper functions that allow you to call functions from the other plugin. These wrappers handle the function address lookup and parameter passing.
Example Generated Bindings
Here's an example of a generated Rust binding file for a plugin named plugin_from_another_language:
How It Works
- The wrapper function (
ParamCallback) retrieves the address of the exported function using the Plugify API. - The
____plugin_from_another_language_ParamCallbackdelegate is set by the language module during plugin load. - The wrapper function directly passes the parameters to the exported function.
Example: Using the Generated Bindings
Here's how you can use the generated bindings in your Rust plugin:
Working with PlgTypes
Plugify uses special types for cross-language compatibility:
Str
Arr<T>
Var
When is Binding Generation Necessary?
Binding generation is essential when importing functions from plugins written in other languages. Without these bindings, you cannot safely call the exported functions. The generator ensures type safety and proper ABI compatibility.
Best Practices
- Use the Generator Tool: Always use the Plugify generator tool (online or command-line) to generate bindings for imported functions.
- Include Generated Modules: Place generated files in a modules directory and include them in your plugin.
- Handle Errors Safely: Check for null function pointers before calling imported functions.
- Document Dependencies: Clearly document the plugins and functions your plugin depends on.
- Use PlgTypes: Always use
Str,Arr<T>, andVarfor cross-language compatibility. - Respect Ownership: Be careful with borrowed references vs owned values.
Conclusion
Importing functions from another plugin in Rust is straightforward when you use the Plugify generator tool to generate the necessary bindings. These bindings provide safe wrapper functions that handle function address lookup and parameter passing, making it easy to integrate functionality from other plugins. By following the steps and best practices outlined in this guide, you can create robust and interoperable plugins in the Plugify ecosystem with Rust's safety guarantees.