Struct plugy_runtime::Runtime
source · pub struct Runtime<P, D = ()> { /* private fields */ }Expand description
A runtime environment for managing plugins and instances.
The Runtime struct provides a runtime environment for managing plugins
and their instances. It allows you to load, manage, and interact with plugins
written in WebAssembly (Wasm). The runtime maintains a collection of loaded modules,
instances, and associated data for efficient plugin management.
The generic parameter P represents the trait that your plugins must implement.
This trait defines the methods that can be called on the plugins using their instances.
Example
use plugy::runtime::Runtime;
trait Plugin {
fn greet(&self);
}
let runtime = Runtime::<Box<dyn Plugin>>::new();
// Load and manage plugins...Implementations§
source§impl<P, D: Default + Send> Runtime<P, D>
impl<P, D: Default + Send> Runtime<P, D>
sourcepub async fn load<T: PluginLoader>(&self, loader: T) -> Result<P::Output>where
P: IntoCallable<T, D>,
pub async fn load<T: PluginLoader>(&self, loader: T) -> Result<P::Output>where P: IntoCallable<T, D>,
Loads a plugin using the provided loader and returns the plugin instance.
This asynchronous function loads a plugin by calling the load method on
the provided PluginLoader instance. It then prepares the plugin for execution,
instantiates it, and returns the plugin instance wrapped in the appropriate
callable type.
Parameters
loader: An instance of a type that implements thePluginLoadertrait, responsible for loading the plugin’s Wasm module data.
Returns
Returns a Result containing the loaded plugin instance on success,
or an anyhow::Error if the loading and instantiation process encounters any issues.
Examples
use plugy_runtime::Runtime;
use plugy_runtime::PluginLoader;
use plugy_macros::*;
use std::future::Future;
use std::pin::Pin;
#[plugy_macros::plugin]
trait Plugin {
fn do_stuff(&self);
}
// impl Plugin for MyPlugin goes to the wasm file
#[plugin_import(file = "target/wasm32-unknown-unknown/debug/my_plugin.wasm")]
struct MyPlugin;
async fn example(runtime: &Runtime<Box<dyn Plugin>>) -> anyhow::Result<()> {
let plugin = runtime.load(MyPlugin).await?;
Ok(())
}source§impl<P, D: Send> Runtime<P, D>
impl<P, D: Send> Runtime<P, D>
sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Creates a new instance of the Runtime with default configuration.
This function initializes a Runtime instance using the default configuration
settings for the underlying wasmtime::Config. It sets up the engine and linker,
preparing it to load and manage plugin modules.
Returns
Returns a Result containing the initialized Runtime instance on success,
or an anyhow::Error if the creation process encounters any issues.
sourcepub fn get_plugin<T>(&self) -> Result<P::Output>where
P: IntoCallable<T, D>,
pub fn get_plugin<T>(&self) -> Result<P::Output>where P: IntoCallable<T, D>,
Retrieves the callable plugin instance for the specified type.
This function returns a callable instance of the loaded plugin for the
specified type T. The plugin must have been previously loaded using
the load method or similar means.
Returns
Returns a Result containing the callable plugin instance on success,
or an anyhow::Error if the instance retrieval encounters any issues.
sourcepub async fn load_with<T: PluginLoader>(
&mut self,
loader: T,
data_fn: impl Fn(&T) -> D
) -> Result<P::Output>where
P: IntoCallable<T, D>,
D: Context,
pub async fn load_with<T: PluginLoader>( &mut self, loader: T, data_fn: impl Fn(&T) -> D ) -> Result<P::Output>where P: IntoCallable<T, D>, D: Context,
Loads a plugin using the provided loader, but can customize the data stored and returns the plugin instance.
This asynchronous function loads a plugin by calling the load method on
the provided PluginLoader instance. It then prepares the plugin for execution,
instantiates it, and returns the plugin instance wrapped in the appropriate
callable type.
Parameters
-
loader: An instance of a type that implements thePluginLoadertrait, responsible for loading the plugin’s Wasm module data. -
data_fn: A function that takes in the module and returns data to be used in that modules functions
Returns
Returns a Result containing the loaded plugin instance on success,
or an anyhow::Error if the loading and instantiation process encounters any issues.
Examples
use plugy_runtime::Runtime;
use plugy_runtime::PluginLoader;
use plugy_runtime::Context;
use plugy_runtime::Linker;
use plugy_macros::*;
use std::future::Future;
use std::pin::Pin;
#[plugy_macros::plugin]
trait Plugin {
fn do_stuff(&self);
}
// impl Plugin for MyPlugin goes to the wasm file
#[plugin_import(file = "target/wasm32-unknown-unknown/debug/my_plugin.wasm")]
struct MyPlugin;
struct Addr {
//eg actix or xtra
}
impl Context for Addr {
fn link(&self, linker: &mut Linker<Self>) {
//expose methods here
}
}
async fn example(runtime: &mut Runtime<Box<dyn Plugin>, Addr>) -> anyhow::Result<()> {
let plugin = runtime.load_with(MyPlugin, |_plugin| Addr {}).await?;
Ok(())
}