pub trait Loadable: Sized {
type Instance: StableAbi;
type InputArg;
type CInputArg: StableAbi;
type ArgsType;
// Required methods
fn ident(&self) -> &str;
fn plugin_type() -> &'static str;
fn export_prefix() -> &'static str;
fn new(descriptor: PluginDescriptor<Self>) -> Self;
fn help(&self) -> Result<String>;
fn target_list(&self) -> Result<Vec<TargetInfo>>;
fn instantiate(
&self,
library: CArc<LibContext>,
input: Self::InputArg,
args: Option<&Self::ArgsType>
) -> Result<Self::Instance>;
// Provided methods
fn exists(&self, instances: &[LibInstance<Self>]) -> bool { ... }
fn load(
path: impl AsRef<Path>,
library: &CArc<LibContext>,
export: &str
) -> Result<LibInstance<Self>> { ... }
fn load_all(path: impl AsRef<Path>) -> Result<Vec<LibInstance<Self>>> { ... }
fn load_append(
path: impl AsRef<Path>,
out: &mut Vec<LibInstance<Self>>
) -> Result<()> { ... }
}
Expand description
Defines a common interface for loadable plugins
Required Associated Types§
Required Methods§
fn plugin_type() -> &'static str
sourcefn export_prefix() -> &'static str
fn export_prefix() -> &'static str
Constant prefix for the plugin type
fn new(descriptor: PluginDescriptor<Self>) -> Self
sourcefn target_list(&self) -> Result<Vec<TargetInfo>>
fn target_list(&self) -> Result<Vec<TargetInfo>>
Retrieves the list of available targets for this plugin
sourcefn instantiate(
&self,
library: CArc<LibContext>,
input: Self::InputArg,
args: Option<&Self::ArgsType>
) -> Result<Self::Instance>
fn instantiate( &self, library: CArc<LibContext>, input: Self::InputArg, args: Option<&Self::ArgsType> ) -> Result<Self::Instance>
Creates an Instance
of the library
This function assumes that load
performed necessary safety checks
for validity of the library.
Provided Methods§
sourcefn exists(&self, instances: &[LibInstance<Self>]) -> bool
fn exists(&self, instances: &[LibInstance<Self>]) -> bool
Checks if plugin with the same ident
already exists in input list
fn load( path: impl AsRef<Path>, library: &CArc<LibContext>, export: &str ) -> Result<LibInstance<Self>>
sourcefn load_all(path: impl AsRef<Path>) -> Result<Vec<LibInstance<Self>>>
fn load_all(path: impl AsRef<Path>) -> Result<Vec<LibInstance<Self>>>
Try to load a plugin library
This function will access library
and try to find corresponding entry for the plugin. If
a valid plugins are found, Ok(LibInstance<Self>)
is returned. Otherwise, Err(Error)
is
returned, with appropriate error.
Safety
Loading third party libraries is inherently unsafe and the compiler cannot guarantee that the implementation of the library matches the one specified here. This is especially true if the loaded library implements the necessary interface manually.
It is adviced to use a provided proc macro to define a valid library.
sourcefn load_append(
path: impl AsRef<Path>,
out: &mut Vec<LibInstance<Self>>
) -> Result<()>
fn load_append( path: impl AsRef<Path>, out: &mut Vec<LibInstance<Self>> ) -> Result<()>
Helper function to load a plugin into a list of library instances
This function will try finding appropriate plugin entry, and add it into the list if there isn’t a duplicate entry.
Safety
Loading third party libraries is inherently unsafe and the compiler cannot guarantee that the implementation of the library matches the one specified here.