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>

source

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

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

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.

source

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 the PluginLoader trait, 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(())
}

Auto Trait Implementations§

§

impl<P, D = ()> !RefUnwindSafe for Runtime<P, D>

§

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

§

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

§

impl<P, D> Unpin for Runtime<P, D>where P: Unpin,

§

impl<P, D = ()> !UnwindSafe for Runtime<P, D>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.