Skip to content

Commit a755d23

Browse files
authored
refactor(core): more bounds for better errors from #1623 (#1632)
1 parent 181e132 commit a755d23

File tree

6 files changed

+77
-33
lines changed

6 files changed

+77
-33
lines changed

.changes/simplify-tag-label-usage.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,22 @@
22
"tauri": patch
33
---
44

5-
Simplify usage of app event and window label types.
5+
Simplify usage of app event and window label types. The following functions now
6+
accept references the `Tag` can be borrowed as. This means an `&str` can now be
7+
accepted for functions like `Window::emit`. This is a breaking change for the
8+
following items, which now need to take a reference. Additionally, type inference
9+
for `&"event".into()` will no longer work, but `&"event".to_string()` will. The
10+
solution for this is to now just pass `"event"` because `Borrow<str>` is implemented
11+
for the default event type `String`.
12+
13+
* **Breaking:** `Window::emit` now accepts `Borrow` for the event.
14+
* **Breaking:** `Window::emit_others` now accepts `Borrow` for the event
15+
* **Breaking:** `Window::trigger` now accepts `Borrow` for the event.
16+
* **Breaking:** `Manager::emit_all` now accepts `Borrow` for the event.
17+
* **Breaking:** `Manager::emit_to` now accepts `Borrow` for both the event and window label.
18+
* **Breaking:** `Manager::trigger_global` now accepts `Borrow` for the event.
19+
* **Breaking:** `Manager::get_window` now accepts `Borrow` for the window label.
20+
* _(internal):_ `trait tauri::runtime::tag::TagRef` helper for accepting tag references.
21+
Any time you want to accept a tag reference, that trait will handle requiring the reference
22+
to have all the necessary bounds, and generate errors when the exposed function doesn't
23+
set a bound like `P::Event: Borrow<E>`.

core/tauri/src/event.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,14 @@ impl<Event: Tag, Window: Tag> Listeners<Event, Window> {
192192
}
193193

194194
/// Triggers the given global event with its payload.
195-
pub(crate) fn trigger<E>(&self, event: &E, window: Option<Window>, payload: Option<String>)
196-
where
197-
E: TagRef<Event> + ?Sized,
195+
pub(crate) fn trigger<E: ?Sized>(
196+
&self,
197+
event: &E,
198+
window: Option<Window>,
199+
payload: Option<String>,
200+
) where
198201
Event: Borrow<E>,
202+
E: TagRef<Event>,
199203
{
200204
let mut maybe_pending = false;
201205
match self.inner.handlers.try_lock() {

core/tauri/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,27 @@ pub trait Manager<P: Params>: sealed::ManagerBase<P> {
139139
}
140140

141141
/// Emits a event to all windows.
142-
fn emit_all<E, S>(&self, event: &E, payload: Option<S>) -> Result<()>
142+
fn emit_all<E: ?Sized, S>(&self, event: &E, payload: Option<S>) -> Result<()>
143143
where
144-
E: TagRef<P::Event> + ?Sized,
144+
P::Event: Borrow<E>,
145+
E: TagRef<P::Event>,
145146
S: Serialize + Clone,
146147
{
147148
self.manager().emit_filter(event, payload, |_| true)
148149
}
149150

150151
/// Emits an event to a window with the specified label.
151-
fn emit_to<E, L, S: Serialize + Clone>(
152+
fn emit_to<E: ?Sized, L: ?Sized, S: Serialize + Clone>(
152153
&self,
153154
label: &L,
154155
event: &E,
155156
payload: Option<S>,
156157
) -> Result<()>
157158
where
158-
L: TagRef<P::Label> + ?Sized,
159-
E: TagRef<P::Event> + ?Sized,
159+
P::Label: Borrow<L>,
160+
P::Event: Borrow<E>,
161+
L: TagRef<P::Label>,
162+
E: TagRef<P::Event>,
160163
{
161164
self
162165
.manager()
@@ -193,10 +196,10 @@ pub trait Manager<P: Params>: sealed::ManagerBase<P> {
193196
}
194197

195198
/// Trigger a global event.
196-
fn trigger_global<E>(&self, event: &E, data: Option<String>)
199+
fn trigger_global<E: ?Sized>(&self, event: &E, data: Option<String>)
197200
where
198-
E: TagRef<P::Event> + ?Sized,
199201
P::Event: Borrow<E>,
202+
E: TagRef<P::Event>,
200203
{
201204
self.manager().trigger(event, None, data)
202205
}
@@ -207,10 +210,10 @@ pub trait Manager<P: Params>: sealed::ManagerBase<P> {
207210
}
208211

209212
/// Fetch a single window from the manager.
210-
fn get_window<L>(&self, label: &L) -> Option<Window<P>>
213+
fn get_window<L: ?Sized>(&self, label: &L) -> Option<Window<P>>
211214
where
212-
L: TagRef<P::Label> + ?Sized,
213215
P::Label: Borrow<L>,
216+
L: TagRef<P::Label>,
214217
{
215218
self.manager().get_window(label)
216219
}

core/tauri/src/runtime/manager.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,15 @@ impl<P: Params> WindowManager<P> {
493493
window
494494
}
495495

496-
pub fn emit_filter<E, S, F>(&self, event: &E, payload: Option<S>, filter: F) -> crate::Result<()>
496+
pub fn emit_filter<E: ?Sized, S, F>(
497+
&self,
498+
event: &E,
499+
payload: Option<S>,
500+
filter: F,
501+
) -> crate::Result<()>
497502
where
498-
E: TagRef<P::Event> + ?Sized,
503+
P::Event: Borrow<E>,
504+
E: TagRef<P::Event>,
499505
S: Serialize + Clone,
500506
F: Fn(&Window<P>) -> bool,
501507
{
@@ -519,10 +525,10 @@ impl<P: Params> WindowManager<P> {
519525
self.inner.listeners.unlisten(handler_id)
520526
}
521527

522-
pub fn trigger<E>(&self, event: &E, window: Option<P::Label>, data: Option<String>)
528+
pub fn trigger<E: ?Sized>(&self, event: &E, window: Option<P::Label>, data: Option<String>)
523529
where
524-
E: TagRef<P::Event> + ?Sized,
525530
P::Event: Borrow<E>,
531+
E: TagRef<P::Event>,
526532
{
527533
self.inner.listeners.trigger(event, window, data)
528534
}
@@ -578,10 +584,10 @@ impl<P: Params> WindowManager<P> {
578584
.remove(&uuid)
579585
}
580586

581-
pub fn get_window<L>(&self, label: &L) -> Option<Window<P>>
587+
pub fn get_window<L: ?Sized>(&self, label: &L) -> Option<Window<P>>
582588
where
583-
L: TagRef<P::Label> + ?Sized,
584589
P::Label: Borrow<L>,
590+
L: TagRef<P::Label>,
585591
{
586592
self.windows_lock().get(label).cloned()
587593
}

core/tauri/src/runtime/tag.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,17 @@ impl<T, E: Debug> Tag for T where
9191
/// * [`ToOwned`] to make sure we can clone it into the owned tag in specific cases.
9292
/// * [`PartialEq`] so that we can compare refs to the owned tags easily.
9393
/// * [`Hash`] + [`Eq`] because we want to be able to use a ref as a key to internal hashmaps.
94-
pub trait TagRef<T: Tag>: Display + ToOwned<Owned = T> + PartialEq<T> + Eq + Hash {}
94+
pub trait TagRef<T: Tag>: Display + ToOwned<Owned = T> + PartialEq<T> + Eq + Hash
95+
where
96+
T: std::borrow::Borrow<Self>,
97+
{
98+
}
9599

96100
/// Automatically implement [`TagRef`] for all types that fit the requirements.
97-
impl<T: Tag, R> TagRef<T> for R where
98-
R: Display + ToOwned<Owned = T> + PartialEq<T> + Eq + Hash + ?Sized
101+
impl<T: Tag, R> TagRef<T> for R
102+
where
103+
T: std::borrow::Borrow<R>,
104+
R: Display + ToOwned<Owned = T> + PartialEq<T> + Eq + Hash + ?Sized,
99105
{
100106
}
101107

core/tauri/src/runtime/window.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,14 @@ pub(crate) mod export {
196196
&self.window.label
197197
}
198198

199-
pub(crate) fn emit_internal<E, S>(&self, event: &E, payload: Option<S>) -> crate::Result<()>
199+
pub(crate) fn emit_internal<E: ?Sized, S>(
200+
&self,
201+
event: &E,
202+
payload: Option<S>,
203+
) -> crate::Result<()>
200204
where
201-
E: TagRef<P::Event> + ?Sized,
205+
P::Event: Borrow<E>,
206+
E: TagRef<P::Event>,
202207
S: Serialize,
203208
{
204209
let js_payload = match payload {
@@ -218,20 +223,22 @@ pub(crate) mod export {
218223
}
219224

220225
/// Emits an event to the current window.
221-
pub fn emit<E, S>(&self, event: &E, payload: Option<S>) -> crate::Result<()>
226+
pub fn emit<E: ?Sized, S>(&self, event: &E, payload: Option<S>) -> crate::Result<()>
222227
where
223-
E: TagRef<P::Event> + ?Sized,
228+
P::Event: Borrow<E>,
229+
E: TagRef<P::Event>,
224230
S: Serialize,
225231
{
226232
self.emit_internal(event, payload)
227233
}
228234

229235
/// Emits an event on all windows except this one.
230-
pub fn emit_others<E: TagRef<P::Event> + ?Sized, S: Serialize + Clone>(
231-
&self,
232-
event: &E,
233-
payload: Option<S>,
234-
) -> crate::Result<()> {
236+
pub fn emit_others<E: ?Sized, S>(&self, event: &E, payload: Option<S>) -> crate::Result<()>
237+
where
238+
P::Event: Borrow<E>,
239+
E: TagRef<P::Event>,
240+
S: Serialize + Clone,
241+
{
235242
self.manager.emit_filter(event, payload, |w| w != self)
236243
}
237244

@@ -254,10 +261,10 @@ pub(crate) mod export {
254261
}
255262

256263
/// Triggers an event on this window.
257-
pub fn trigger<E>(&self, event: &E, data: Option<String>)
264+
pub fn trigger<E: ?Sized>(&self, event: &E, data: Option<String>)
258265
where
259-
E: TagRef<P::Event> + ?Sized,
260266
P::Event: Borrow<E>,
267+
E: TagRef<P::Event>,
261268
{
262269
let label = self.window.label.clone();
263270
self.manager.trigger(event, Some(label), data)

0 commit comments

Comments
 (0)