Skip to content

Commit 7f3c989

Browse files
authored
feat(tauri): add plugin_boxed methods (#13837)
* feat(tauri): add `plugin_dyn` methods * refactor: rename `plugin_dyn` to `plugin_boxed`
1 parent bda8304 commit 7f3c989

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

.changes/plugin-boxed.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tauri: "minor:enhance"
3+
---
4+
5+
Add `AppHandle::plugin_boxed` and `Builder::plugin_boxed` methods to allow adding plugins in the form of boxed trait objects.

crates/tauri/src/app.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,16 @@ impl<R: Runtime> AppHandle<R> {
484484
/// Ok(())
485485
/// });
486486
/// ```
487-
#[cfg_attr(feature = "tracing", tracing::instrument(name = "app::plugin::register", skip(plugin), fields(name = plugin.name())))]
488487
pub fn plugin<P: Plugin<R> + 'static>(&self, plugin: P) -> crate::Result<()> {
489-
let mut plugin = Box::new(plugin) as Box<dyn Plugin<R>>;
488+
self.plugin_boxed(Box::new(plugin))
489+
}
490490

491+
/// Adds a Tauri application plugin.
492+
///
493+
/// This method is similar to [`Self::plugin`],
494+
/// but accepts a boxed trait object instead of a generic type.
495+
#[cfg_attr(feature = "tracing", tracing::instrument(name = "app::plugin::register", skip(plugin), fields(name = plugin.name())))]
496+
pub fn plugin_boxed(&self, mut plugin: Box<dyn Plugin<R>>) -> crate::Result<()> {
491497
let mut store = self.manager().plugins.lock().unwrap();
492498
store.initialize(&mut plugin, self, &self.config().plugins)?;
493499
store.register(plugin);
@@ -1683,8 +1689,17 @@ tauri::Builder::default()
16831689
/// .plugin(plugin::init());
16841690
/// ```
16851691
#[must_use]
1686-
pub fn plugin<P: Plugin<R> + 'static>(mut self, plugin: P) -> Self {
1687-
self.plugins.register(Box::new(plugin));
1692+
pub fn plugin<P: Plugin<R> + 'static>(self, plugin: P) -> Self {
1693+
self.plugin_boxed(Box::new(plugin))
1694+
}
1695+
1696+
/// Adds a Tauri application plugin.
1697+
///
1698+
/// This method is similar to [`Self::plugin`],
1699+
/// but accepts a boxed trait object instead of a generic type.
1700+
#[must_use]
1701+
pub fn plugin_boxed(mut self, plugin: Box<dyn Plugin<R>>) -> Self {
1702+
self.plugins.register(plugin);
16881703
self
16891704
}
16901705

0 commit comments

Comments
 (0)