Skip to main content

Plugin

Struct Plugin 

Source
pub struct Plugin<Ctx: 'static> { /* private fields */ }
Expand description

A WASM component bundled with its runtime context, ready for instantiation.

The component’s exports (its plug) and imports (its sockets) are defined through the crate::Binding, not by this struct.

The context is consumed during linking to become the wasmtime Store’s data.

§Type Parameters

  • Ctx: User context type that will be stored in the wasmtime Store

§Example

let engine = Engine::default();
let linker = Linker::new( &engine );

let plugin = Plugin::new(
	Component::new( &engine, "(component)" )?,
	Ctx { resource_table: ResourceTable::new() },
).instantiate( &engine, &linker )?;

Implementations§

Source§

impl<Ctx> Plugin<Ctx>
where Ctx: PluginContext + 'static,

Source

pub fn new(component: Component, context: Ctx) -> Self

Creates a new plugin declaration.

Note that the plugin ID is not specified here - it’s provided when constructing the cardinality wrapper that holds this plugin. This is done to prevent duplicate ids.

Source

pub fn with_fuel_limiter( self, limiter: impl FnMut(&mut Store<Ctx>, &str, &str, &Function) -> u64 + Send + 'static, ) -> Self

Sets a closure that determines the fuel limit for each function call.

The closure receives the store, the interface path (e.g., "my:package/api"), the function name, and the Function metadata. It returns the fuel to set.

Warning: Fuel consumption must be enabled in the Engine via Config::consume_fuel. If not enabled, dispatch will fail with a RuntimeException at call time.

let plugin = Plugin::new( component, Ctx { resource_table: ResourceTable::new() })
	.with_fuel_limiter(| _store, _interface, _function, _metadata | 100_000 );
Source

pub fn with_epoch_limiter( self, limiter: impl FnMut(&mut Store<Ctx>, &str, &str, &Function) -> u64 + Send + 'static, ) -> Self

Sets a closure that determines the epoch deadline for each function call.

The closure receives the store, the interface path (e.g., "my:package/api"), the function name, and the Function metadata. It returns the epoch deadline in ticks.

Warning: Epoch interruption must be enabled in the Engine via Config::epoch_interruption. If not enabled, the deadline is silently ignored.

let plugin = Plugin::new( component, Ctx { resource_table: ResourceTable::new() })
	.with_epoch_limiter(| _store, _interface, _function, _metadata | 5 );
Source

pub fn with_memory_limiter( self, limiter: impl FnMut(&mut Ctx) -> &mut dyn ResourceLimiter + Send + Sync + 'static, ) -> Self

Sets a closure that returns a mutable reference to a ResourceLimiter embedded in the plugin context.

The limiter is installed into the wasmtime Store once at instantiation and controls memory and table growth for the lifetime of the plugin.

The ResourceLimiter must be stored inside the context type Ctx so that wasmtime can access it through a &mut Ctx reference.

let plugin = Plugin::new( component, Ctx { resource_table: ResourceTable::new(), limiter: MyLimiter })
	.with_memory_limiter(| ctx | &mut ctx.limiter );

Links this plugin with its socket bindings and instantiates it.

Takes ownership of the linker because socket bindings are added to it. If you need to reuse the same linker for multiple plugins, clone it before passing it in.

§Type Parameters
  • PluginId: Must implement Into<Val> so plugin IDs can be passed to WASM when dispatching to multi-plugin sockets (the ID identifies which plugin produced each result).
§Errors

Returns an error if linking or instantiation fails.

Source

pub fn instantiate( self, engine: &Engine, linker: &Linker<Ctx>, ) -> Result<PluginInstance<Ctx>, Error>

A convenience alias for Plugin::link with 0 sockets

§Errors

Returns an error if instantiation fails.

Trait Implementations§

Source§

impl<Ctx: Debug + 'static> Debug for Plugin<Ctx>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Ctx> Freeze for Plugin<Ctx>
where Ctx: Freeze,

§

impl<Ctx> !RefUnwindSafe for Plugin<Ctx>

§

impl<Ctx> Send for Plugin<Ctx>
where Ctx: Send,

§

impl<Ctx> !Sync for Plugin<Ctx>

§

impl<Ctx> Unpin for Plugin<Ctx>
where Ctx: Unpin,

§

impl<Ctx> UnsafeUnpin for Plugin<Ctx>
where Ctx: UnsafeUnpin,

§

impl<Ctx> !UnwindSafe for Plugin<Ctx>

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.