pub struct Runtime<T, P = Plugin> { /* 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;
use plugy_runtime::Plugin;
trait Greeter {
fn greet(&self);
}
let runtime = Runtime::<Box<dyn Greeter>>::new();
// Load and manage plugins...
Implementations§
Source§impl<T, D: Send> Runtime<T, Plugin<D>>
impl<T, D: Send> Runtime<T, Plugin<D>>
Sourcepub async fn load_with<P: Send + PluginLoader + Into<Plugin<D>>>(
&self,
plugin: P,
) -> Result<T::Output>where
T: IntoCallable<P, D>,
pub async fn load_with<P: Send + PluginLoader + Into<Plugin<D>>>(
&self,
plugin: P,
) -> Result<T::Output>where
T: IntoCallable<P, 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 thePluginLoader
trait, 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::Plugin;
use plugy_core::PluginLoader;
use plugy_macros::*;
use std::future::Future;
use std::pin::Pin;
#[plugy_macros::plugin]
trait Greeter {
fn do_stuff(&self, input: &str);
}
// impl Plugin for MyPlugin goes to the wasm file
#[plugin_import(file = "target/wasm32-unknown-unknown/debug/my_plugin.wasm")]
struct MyPlugin;
impl From<MyPlugin> for Plugin {
fn from(val: MyPlugin) -> Self {
Plugin {
name: "MyPlugin".to_string(),
data: Default::default(),
plugin_type: "MyPlugin".to_string(),
}
}
}
async fn example(runtime: &Runtime<Box<dyn Greeter>>) {
let plugin = runtime.load(MyPlugin).await.unwrap();
// ...
}
Sourcepub fn get_plugin_by_name<P: Send + PluginLoader>(
&self,
name: &str,
) -> Result<T::Output>where
T: IntoCallable<P, D>,
pub fn get_plugin_by_name<P: Send + PluginLoader>(
&self,
name: &str,
) -> Result<T::Output>where
T: IntoCallable<P, D>,
Retrieves the callable plugin instance with the specified name.
This function returns a callable instance of the loaded plugin with the
specified name. 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 fn get_plugin<P: Send + PluginLoader>(&self) -> Result<T::Output>where
T: IntoCallable<P, D>,
pub fn get_plugin<P: Send + PluginLoader>(&self) -> Result<T::Output>where
T: IntoCallable<P, 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.
Source§impl<T> Runtime<T>
impl<T> Runtime<T>
Sourcepub async fn load<P: Send + PluginLoader + Into<Plugin>>(
&self,
plugin: P,
) -> Result<T::Output>
pub async fn load<P: Send + PluginLoader + Into<Plugin>>( &self, plugin: P, ) -> Result<T::Output>
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 thePluginLoader
trait, 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::Plugin as KasukuPlugin;
use plugy_runtime::Runtime;
use plugy_core::PluginLoader;
use plugy_macros::*;
use std::future::Future;
use std::pin::Pin;
#[plugy_macros::plugin]
trait Plugin {
fn do_stuff(&self, input: &str);
}
// impl Plugin for MyPlugin goes to the wasm file
#[plugin_import(file = "target/wasm32-unknown-unknown/debug/my_plugin.wasm")]
struct MyPlugin;
impl From<MyPlugin> for KasukuPlugin {
fn from(val: MyPlugin) -> Self {
KasukuPlugin {
name: "MyPlugin".to_string(),
data: Default::default(),
plugin_type: "MyPlugin".to_string(),
}
}
}
async fn example(runtime: &Runtime<Box<dyn Plugin>>) -> anyhow::Result<()> {
let plugin = runtime.load(MyPlugin).await?;
Ok(())
}
Source§impl<T, P> Runtime<T, P>
impl<T, P> Runtime<T, P>
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.
Source§impl<T, D> Runtime<T, Plugin<D>>
impl<T, D> Runtime<T, Plugin<D>>
Sourcepub fn context<C: Context<D>>(self, ctx: C) -> Self
pub fn context<C: Context<D>>(self, ctx: C) -> Self
Allows exposing methods that will run on the runtime side
use plugy_runtime::Runtime;
trait Greeter {
fn greet(&self, text: &str);
}
#[derive(Debug)]
pub struct Logger;
#[plugy::macros::context(data = Data)]
impl Logger {
pub async fn log(_: &mut plugy::runtime::Caller<'_>, text: &str) {
dbg!(text);
}
}
let mut runtime = Runtime::<Box<dyn Greeter>>::new().unwrap();
let runtime = runtime
.context(Logger);
Auto Trait Implementations§
impl<T, P> Freeze for Runtime<T, P>
impl<T, P = Plugin> !RefUnwindSafe for Runtime<T, P>
impl<T, P> Send for Runtime<T, P>
impl<T, P> Sync for Runtime<T, P>
impl<T, P> Unpin for Runtime<T, P>where
T: Unpin,
impl<T, P = Plugin> !UnwindSafe for Runtime<T, P>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more