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 wasmtimeStore
§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,
impl<Ctx> Plugin<Ctx>where
Ctx: PluginContext + 'static,
Sourcepub fn new(component: Component, context: Ctx) -> Self
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.
Sourcepub fn with_fuel_limiter(
self,
limiter: impl FnMut(&mut Store<Ctx>, &str, &str, &Function) -> u64 + Send + 'static,
) -> Self
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 );Sourcepub fn with_epoch_limiter(
self,
limiter: impl FnMut(&mut Store<Ctx>, &str, &str, &Function) -> u64 + Send + 'static,
) -> Self
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 );Sourcepub fn with_memory_limiter(
self,
limiter: impl FnMut(&mut Ctx) -> &mut dyn ResourceLimiter + Send + Sync + 'static,
) -> Self
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 );Sourcepub fn link<PluginId, Sockets>(
self,
engine: &Engine,
linker: Linker<Ctx>,
sockets: Sockets,
) -> Result<PluginInstance<Ctx>, Error>
pub fn link<PluginId, Sockets>( self, engine: &Engine, linker: Linker<Ctx>, sockets: Sockets, ) -> Result<PluginInstance<Ctx>, Error>
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 implementInto<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.
Sourcepub fn instantiate(
self,
engine: &Engine,
linker: &Linker<Ctx>,
) -> Result<PluginInstance<Ctx>, Error>
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§
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> 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