spin_factors

Trait RuntimeFactors

Source
pub trait RuntimeFactors:
    Send
    + Sync
    + Sized
    + 'static {
    type AppState: Sync + Send;
    type InstanceState: RuntimeFactorsInstanceState;
    type InstanceBuilders: Send + HasInstanceBuilder;
    type RuntimeConfig: Default;

    // Required methods
    fn init<T: AsInstanceState<Self::InstanceState> + Send + 'static>(
        &mut self,
        linker: &mut Linker<T>,
    ) -> Result<()>;
    fn configure_app(
        &self,
        app: App,
        runtime_config: Self::RuntimeConfig,
    ) -> Result<ConfiguredApp<Self>>;
    fn prepare(
        &self,
        configured_app: &ConfiguredApp<Self>,
        component_id: &str,
    ) -> Result<Self::InstanceBuilders>;
    fn build_instance_state(
        &self,
        builders: Self::InstanceBuilders,
    ) -> Result<Self::InstanceState>;
    fn app_state<F: Factor>(app_state: &Self::AppState) -> Option<&F::AppState>;
    fn instance_builder_mut<F: Factor>(
        builders: &mut Self::InstanceBuilders,
    ) -> Option<Option<&mut F::InstanceBuilder>>;
}
Expand description

A collection of Factors that are initialized and configured together.

Implemented by #[derive(RuntimeFactors)] and should not be implemented manually.

§Example

A typical usage of RuntimeFactors would look something like the following pseudo-code:

#[derive(RuntimeFactors)]
struct MyFactors {
 // ...
}
// Initialize the factors collection
let factors = MyFactors { /* .. */ };
// Initialize each factor with a linker
factors.init(&mut linker)?;
// Configure the factors with an app and runtime config
let configured_app = factors.configure_app(app, runtime_config)?;
// Prepare instance state builders
let builders = factors.prepare(&configured_app, "component-id")?;
// Build the instance state for the factors
let data = factors.build_instance_state(builders)?;
// Initialize a `wasmtime` store with the instance state
let mut store = wasmtime::Store::new(&engine, data);
// Instantiate the component
let instance = linker.instantiate_async(&mut store, &component).await?;

Required Associated Types§

Source

type AppState: Sync + Send

The per application state of all the factors.

Source

type InstanceState: RuntimeFactorsInstanceState

The per instance state of the factors.

Source

type InstanceBuilders: Send + HasInstanceBuilder

The collection of all the InstanceBuilders of the factors.

Source

type RuntimeConfig: Default

The runtime configuration of all the factors.

Required Methods§

Source

fn init<T: AsInstanceState<Self::InstanceState> + Send + 'static>( &mut self, linker: &mut Linker<T>, ) -> Result<()>

Initialize the factors with the given linker.

Each factor’s init is called in turn. Must be called once before RuntimeFactors::prepare.

Source

fn configure_app( &self, app: App, runtime_config: Self::RuntimeConfig, ) -> Result<ConfiguredApp<Self>>

Configure the factors with the given app and runtime config.

Source

fn prepare( &self, configured_app: &ConfiguredApp<Self>, component_id: &str, ) -> Result<Self::InstanceBuilders>

Prepare the factors’ instance state builders.

Source

fn build_instance_state( &self, builders: Self::InstanceBuilders, ) -> Result<Self::InstanceState>

Build the instance state for the factors.

Source

fn app_state<F: Factor>(app_state: &Self::AppState) -> Option<&F::AppState>

Get the app state related to a particular factor.

Source

fn instance_builder_mut<F: Factor>( builders: &mut Self::InstanceBuilders, ) -> Option<Option<&mut F::InstanceBuilder>>

Get the instance builder of a particular factor.

The outer Option is None if the factor has not been registered with this Factors collection, and the inner Option is None if the factor has not been prepared yet.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§