Struct Runtime

Source
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>>

Source

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 the PluginLoader 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();
    // ...
}
Source

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.

Source

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>

Source

pub async fn load<P: Send + PluginLoader + Into<Plugin>>( &self, plugin: P, ) -> Result<T::Output>
where T: IntoCallable<P, Vec<u8>>,

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 the PluginLoader 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>

Source

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

Source

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>
where T: Send, P: Send + Sync,

§

impl<T, P> Sync for Runtime<T, P>
where T: Sync, P: Send + Sync,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.