DEV Community

Cover image for ๐Ÿš€ Turbocharge Your Rust Project with plugins using plugy!
Njuguna Mureithi
Njuguna Mureithi

Posted on

๐Ÿš€ Turbocharge Your Rust Project with plugins using plugy!

Get ready to supercharge your Rust projects with plugy, the dynamic and playful plugin system that seamlessly integrates Rust-based plugins into your application. By providing a runtime environment for loading and executing WebAssembly (Wasm) plugins, plugy brings a new level of excitement and flexibility to your Rust development journey.

Features that Spark Joy in Your Rust Projects

  • Load and Execute Wasm Plugins: plugy lets you load and execute plugins compiled to WebAssembly, turning your Rust applications into dynamic powerhouses!

  • Flexible Runtime Management: Enjoy the freedom of flexible runtime management for your plugins, bringing dynamic extensibility without breaking a sweat.

  • Async Plugin Function Calls: plugy supports asynchronous plugin function calls, ensuring a smooth and entertaining integration of plugins into your async Rust applications.

  • Easy-to-Use Macros: Simplify the generation of plugin interfaces with plugy's snazzy macros, making the process of working with dynamic plugins as fun as a game.

๐ŸŽฎ Let the plugy Adventure Begin!

1. Define Your Plugin Trait

#[plugy::plugin]
trait Greeter {
    fn greet(&self) -> String;
}
Enter fullscreen mode Exit fullscreen mode

2. Implement Your First Plugin

#[derive(Debug, Deserialize)]
struct FooPlugin;

#[plugin_impl]
impl Greeter for FooPlugin {
    fn greet(&self) -> String {
        "Hello From Foo Plugin".to_owned()
    }
}
Enter fullscreen mode Exit fullscreen mode

Compile your plugin:

cargo build --target wasm32-unknown-unknown
Enter fullscreen mode Exit fullscreen mode

3. Import and Run

#[plugin_import(file = "target/wasm32-unknown-unknown/debug/foo_plugin.wasm")]
struct FooPlugin;

#[tokio::main]
async fn main() {
    let runtime = Runtime::<Box<dyn Greeter>>::new().unwrap();
    let handle = runtime.load(FooPlugin).await.unwrap();
    let res = handle.greet().await;
    assert_eq!(res, "Hello From Foo Plugin")
}
Enter fullscreen mode Exit fullscreen mode

And just like that, you're all set to embark on a dynamic extensibility adventure in your Rust project!

๐Ÿš€ Examples for Practical Application

Explore the examples directory to find a thrilling use case that showcase the power and versatility of plugy in action.

๐Ÿ”“ Unlocking Functionality with plugy

plugy comprises three essential crates, each playing a distinct role in crafting dynamic plugin systems with Rust and WebAssembly:

  • core: This crate provides foundational components such as bitwise utilities and the guest module, forming the basis of plugy's functionality.

  • runtime: Orchestrating the execution of your plugin system, the runtime crate seamlessly integrates plugins into your applications, offering a smooth and efficient experience.

  • macros: The macros crate offers a collection of macros that simplify the generation of bindings and interfaces, making it easier to work with dynamic plugins.

๐ŸŽ‰ Conclusion

With plugy, you're not just coding; you're on a journey of dynamic extensibility, unlocking new possibilities and embracing the flexibility that plugy brings to your Rust development. Happy coding and have a blast! ๐Ÿš€โœจ

Github

https://github.com/geofmureithi/plugy

Image of Timescale

๐Ÿš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsโ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up