crate_plugin_kit/store/load.rs
1//! Loading an installed plugin with `dlopen`.
2//!
3//! `load` is the one call that makes [`CratePluginKit::uninstall`] refuse to delete a
4//! plugin: once the library is open in this process, the crate name is recorded so the
5//! two cannot disagree.
6
7use crate::error::KitResult;
8use crate::loader::{self, LoadedPlugin};
9
10use super::CratePluginKit;
11
12impl<T> CratePluginKit<T> {
13 /// Loads a plugin and returns a `*const T`.
14 ///
15 /// Once loaded, [`Self::uninstall`] refuses to delete it until the process exits.
16 pub fn load(&self, name: &str) -> KitResult<LoadedPlugin<T>> {
17 let crate_name = self.cfg.normalize_crate_name(name);
18
19 let manifest = self.manifest_of(&crate_name)?;
20 let dir = self.paths.plugin_dir(&crate_name);
21 let stem = manifest.effective_lib_stem(&self.cfg, &crate_name);
22 let lib_path = loader::find_library(&dir, &stem)?;
23
24 // SAFETY: `lib_path` was installed by this kit; whether the layout of `T`
25 // matches the struct it exports is covered by the host's abi_version field —
26 // that is the host contract crate's job, not this crate's.
27 let plugin = unsafe { loader::open::<T>(&lib_path, &self.cfg.entry_symbol)? };
28
29 self.loaded.borrow_mut().insert(crate_name);
30 Ok(plugin)
31 }
32}