Skip to main content

HostPlugin

Trait HostPlugin 

Source
pub trait HostPlugin:
    Any
    + Send
    + Sync
    + 'static {
    // Required methods
    fn id(&self) -> &'static str;
    fn world(&self) -> WitWorld;

    // Provided methods
    fn start<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn on_workload_bind<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _workload: &'life1 UnresolvedWorkload,
        _interfaces: HashSet<WitInterface>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_component_bind<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _component: &'life1 mut WorkloadComponent,
        _interfaces: HashSet<WitInterface>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn on_workload_resolved<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        _workload: &'life1 ResolvedWorkload,
        _component_id: &'life2 str,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn on_workload_unbind<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _workload: &'life1 ResolvedWorkload,
        _interfaces: HashSet<WitInterface>,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn stop<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

The HostPlugin trait provides an interface for implementing built-in plugins for the host. A plugin is primarily responsible for implementing a specific WitWorld as a collection of imports and exports that will be directly linked to the workload’s wasmtime::component::Linker.

For example, the runtime doesn’t implement wasi:keyvalue, but it’s a key capability for many component applications. This crate provides a wasi_keyvalue::WasiKeyvalue built-in that persists key-value data in-memory and implements the component imports of wasi:keyvalue atomics, batch and store.

You can supply your own HostPlugin implementations to the crate::host::HostBuilder::with_plugin function.

Required Methods§

Source

fn id(&self) -> &'static str

Returns the unique identifier for this plugin.

This ID must be unique across all plugins registered with a host. It’s used to retrieve plugin instances and avoid conflicts.

§Returns

A static string slice containing the plugin’s unique identifier.

Source

fn world(&self) -> WitWorld

Returns the WIT interfaces that this plugin provides.

The returned WitWorld contains the imports and exports that this plugin implements. The plugin’s bind_component method will only be called if a workload requires one of these interfaces.

§Returns

A WitWorld containing the plugin’s imports and exports.

Provided Methods§

Source

fn start<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Called when the plugin is started during host initialization.

This method allows plugins to perform any necessary setup before accepting workloads. The default implementation does nothing.

§Returns

Ok if the plugin started successfully.

§Errors

Returns an error if the plugin fails to initialize, which will prevent the host from starting.

Source

fn on_workload_bind<'life0, 'life1, 'async_trait>( &'life0 self, _workload: &'life1 UnresolvedWorkload, _interfaces: HashSet<WitInterface>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a workload is binding to this plugin.

This method is invoked when a workload is in the process of being bound to the plugin, allowing the plugin to perform any necessary setup or validation before the binding is finalized. The default implementation does nothing.

§Arguments
  • workload - The unresolved workload that is being bound.
  • interfaces - The set of WIT interfaces that the workload requires from this plugin.
§Returns

Ok if the binding preparation succeeded.

§Errors

Returns an error if the plugin cannot support the requested binding.

Source

fn on_component_bind<'life0, 'life1, 'async_trait>( &'life0 self, _component: &'life1 mut WorkloadComponent, _interfaces: HashSet<WitInterface>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a WorkloadComponent is being bound to this plugin.

This method is called when a workload requires interfaces that this plugin provides. The plugin should configure the component’s linker with the necessary implementations.

§Arguments
  • component - The workload component to bind to this plugin
  • interfaces - The specific WIT interfaces the component requires
§Returns

Ok if binding succeeded.

§Errors

Returns an error if the plugin cannot bind to the component.

Source

fn on_workload_resolved<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _workload: &'life1 ResolvedWorkload, _component_id: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Called when a workload has been fully resolved and is ready for use.

This optional callback allows plugins to perform actions after a workload has been successfully bound and resolved. The default implementation does nothing.

§Arguments
  • workload - The fully resolved workload
  • component_id - The ID of the specific component within the workload
§Returns

Ok if the callback completed successfully.

§Errors

Returns an error if the plugin fails to handle the resolved workload.

Source

fn on_workload_unbind<'life0, 'life1, 'async_trait>( &'life0 self, _workload: &'life1 ResolvedWorkload, _interfaces: HashSet<WitInterface>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Called when a workload is being stopped or unbound from this plugin.

This method allows plugins to clean up any resources associated with the workload. The default implementation does nothing.

§Arguments
  • workload - The workload being unbound
  • interfaces - The interfaces that were bound
§Returns

Ok if unbinding succeeded.

§Errors

Returns an error if cleanup fails.

Source

fn stop<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Called when the plugin is being stopped during host shutdown.

This method allows plugins to perform cleanup before the host stops. The default implementation does nothing.

§Returns

Ok if the plugin stopped successfully.

§Errors

Returns an error if cleanup fails (errors are logged but don’t prevent shutdown).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§