pub struct PluginTreeHead<I: InterfaceData, P: PluginData + 'static> { /* private fields */ }Expand description
The root node of a loaded plugin tree.
Obtained from PluginTree::load. This is the host application’s entry point
for calling into the plugin system. The root socket represents the interface
that the host has access to - all other interfaces are internal and can only
be called by other plugins.
The host acts as a pseudo-plugin: it doesn’t need to be implemented in WASM and has access to system capabilities that plugins don’t.
Implementations§
Source§impl<I: InterfaceData, P: PluginData> PluginTreeHead<I, P>
impl<I: InterfaceData, P: PluginData> PluginTreeHead<I, P>
Sourcepub fn dispatch(
&self,
interface_path: &str,
function: &str,
has_return: bool,
data: &[Val],
) -> Socket<Result<Val, DispatchError<I>>, P::Id>
pub fn dispatch( &self, interface_path: &str, function: &str, has_return: bool, data: &[Val], ) -> Socket<Result<Val, DispatchError<I>>, P::Id>
Invokes a function on all plugins in the root socket.
Dispatches the function call to every plugin implementing the root interface.
The return type is a Socket whose variant matches the interface’s cardinality:
ExactlyOnecardinality →Socket::ExactlyOne(result)AtMostOnecardinality →Socket::AtMostOne(Option<result>)AtLeastOne/Anycardinality →Socket::AtLeastOne/Any(HashMap<PluginId, result>)
§Arguments
interface_path- Full WIT interface path (e.g.,"my:package/interface-name")function- Function name to call as declared in the interfacehas_return- Whether you expect to receive a return valuedata- Arguments to pass to the function as wasmtimeVals
§Example
use wasm_link::{
InterfaceData, InterfaceCardinality, FunctionData, ReturnKind,
PluginData, PluginCtxView, PluginTree, Socket,
Engine, Component, Linker, ResourceTable, Val,
};
#[derive( Clone )]
struct Func { name: String, return_kind: ReturnKind }
impl FunctionData for Func {
/* .. */
}
struct Interface { id: &'static str, funcs: Vec<Func> }
impl InterfaceData for Interface {
/* ... */
}
struct Plugin { id: &'static str, plug: &'static str, resource_table: ResourceTable }
impl PluginData for Plugin {
/* ... */
}
let root_interface_id = "root" ;
let plugins = [ Plugin { id: "foo", plug: root_interface_id, resource_table: ResourceTable::new() }];
let interfaces = [ Interface { id: root_interface_id, funcs: vec![
Func { name: "get-value".to_string(), return_kind: ReturnKind::MayContainResources }
]}];
let ( tree, build_errors ) = PluginTree::new( root_interface_id, interfaces, plugins );
assert!( build_errors.is_empty() );
let engine = Engine::default();
let linker = Linker::new( &engine );
let ( tree_head, load_errors ) = tree.load( &engine, &linker ).unwrap();
assert!( load_errors.is_empty() );
// Dispatch returns a Socket matching the interface's cardinality
let result = tree_head.dispatch( "my:package/example", "get-value", true, &[] );
match result {
Socket::ExactlyOne( Ok( Val::U32( n ))) => assert_eq!( n, 42 ),
Socket::ExactlyOne( Err( e )) => panic!( "dispatch error: {e}" ),
_ => panic!( "unexpected cardinality" ),
}Auto Trait Implementations§
impl<I, P> Freeze for PluginTreeHead<I, P>
impl<I, P> RefUnwindSafe for PluginTreeHead<I, P>
impl<I, P> Send for PluginTreeHead<I, P>
impl<I, P> Sync for PluginTreeHead<I, P>
impl<I, P> Unpin for PluginTreeHead<I, P>
impl<I, P> UnwindSafe for PluginTreeHead<I, P>
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
Mutably borrows from an owned value. Read more
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>
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 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>
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 moreSource§impl<X> Pipe for X
impl<X> Pipe for X
Source§fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Returnwhere
Function: FnOnce(&'a Self) -> Return,
fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Returnwhere
Function: FnOnce(&'a Self) -> Return,
Source§fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Returnwhere
Function: FnOnce(&'a mut Self) -> Return,
fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Returnwhere
Function: FnOnce(&'a mut Self) -> Return,
Source§fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
Apply
f to &self where f takes a single parameter of type Param
and Self implements trait AsRef<Param>. Read moreSource§fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return
fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return
Apply
f to &mut self where f takes a single parameter of type Param
and Self implements trait AsMut<Param>. Read moreSource§fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
Apply
f to &self where f takes a single parameter of type Param
and Self implements trait Deref<Target = Param>. Read moreSource§fn pipe_deref_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function,
) -> Returnwhere
Self: DerefMut<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
fn pipe_deref_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function,
) -> Returnwhere
Self: DerefMut<Target = Param>,
Param: 'a + ?Sized,
Function: FnOnce(&'a mut Param) -> Return,
Apply
f to &mut self where f takes a single parameter of type Param
and Self implements trait DerefMut<Target = Param>. Read moreSource§fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return
fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return
Apply
f to &self where f takes a single parameter of type Param
and Self implements trait Borrow<Param>. Read moreSource§fn pipe_borrow_mut<'a, Param, Return, Function>(
&'a mut self,
f: Function,
) -> Return
fn pipe_borrow_mut<'a, Param, Return, Function>( &'a mut self, f: Function, ) -> Return
Apply
f to &mut self where f takes a single parameter of type Param
and Self implements trait BorrowMut<Param>. Read more