Skip to content

Commit ba766f3

Browse files
fix: async resource
1 parent a5fa932 commit ba766f3

20 files changed

+422
-301
lines changed

crates/gen-host/src/lib.rs

Lines changed: 121 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -310,135 +310,145 @@ impl Host {
310310
}
311311
}
312312

313-
fn print_add_to_router<'a>(
314-
&self,
315-
mod_ident: &str,
316-
functions: impl Iterator<Item = &'a Function>,
317-
methods: impl Iterator<Item = (&'a str, &'a Function)>,
318-
) -> TokenStream {
319-
let trait_ident = format_ident!("{}", mod_ident.to_upper_camel_case());
320-
321-
let mod_name = mod_ident.to_snake_case();
322-
323-
let functions = functions.map(|func| {
324-
let func_name = func.ident.to_snake_case();
325-
let func_ident = format_ident!("{}", func_name);
326-
327-
let param_decl = match func.params.len() {
328-
0 => quote! { () },
329-
1 => {
330-
let ty = &func.params.first().unwrap().1;
331-
let ty = self.print_ty(ty, &BorrowMode::Owned);
332-
quote! { #ty }
333-
}
334-
_ => {
335-
let tys = func
336-
.params
337-
.iter()
338-
.map(|(_, ty)| { self.print_ty(ty, &BorrowMode::Owned) });
339-
quote! { (#(#tys),*) }
340-
}
341-
};
342-
343-
let param_acc = match func.params.len() {
344-
0 => quote! { },
345-
1 => quote! { p },
346-
_ => {
347-
let ids = func
348-
.params
349-
.iter()
350-
.enumerate()
351-
.map(|(i, _)| {
352-
let i = Literal::usize_unsuffixed(i);
353-
quote! { p.#i }
354-
});
355-
quote! { #(#ids),* }
356-
}
357-
};
313+
fn print_router_fn_definition(&self, mod_name: &str, func: &Function) -> TokenStream {
314+
let func_name = func.ident.to_snake_case();
315+
let func_ident = format_ident!("{}", func_name);
316+
317+
let param_decl = match func.params.len() {
318+
0 => quote! { () },
319+
1 => {
320+
let ty = &func.params.first().unwrap().1;
321+
let ty = self.print_ty(ty, &BorrowMode::Owned);
322+
quote! { #ty }
323+
}
324+
_ => {
325+
let tys = func
326+
.params
327+
.iter()
328+
.map(|(_, ty)| self.print_ty(ty, &BorrowMode::Owned));
329+
quote! { (#(#tys),*) }
330+
}
331+
};
332+
333+
let param_acc = match func.params.len() {
334+
0 => quote! {},
335+
1 => quote! { p },
336+
_ => {
337+
let ids = func.params.iter().enumerate().map(|(i, _)| {
338+
let i = Literal::usize_unsuffixed(i);
339+
quote! { p.#i }
340+
});
341+
quote! { #(#ids),* }
342+
}
343+
};
358344

359-
if self.opts.async_ {
360-
quote! {
361-
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
362-
router.define_async(
363-
#mod_name,
364-
#func_name,
365-
move |ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>, p: #param_decl| {
366-
let get_cx = get_cx.clone();
367-
Box::pin(async move {
368-
let ctx = get_cx(ctx.data());
369-
Ok(ctx.#func_ident(#param_acc).await)
370-
})
371-
})?;
372-
}
373-
} else {
374-
quote! {
375-
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
376-
router.define(
377-
#mod_name,
378-
#func_name,
379-
move |ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>, p: #param_decl| {
345+
if self.opts.async_ {
346+
quote! {
347+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
348+
router.define_async(
349+
#mod_name,
350+
#func_name,
351+
move |ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>, p: #param_decl| {
352+
let get_cx = get_cx.clone();
353+
Box::pin(async move {
380354
let ctx = get_cx(ctx.data());
381-
382-
Ok(ctx.#func_ident(#param_acc))
383-
},
384-
)?;
385-
}
355+
Ok(ctx.#func_ident(#param_acc).await)
356+
})
357+
})?;
386358
}
387-
});
388-
389-
let methods = methods.map(|(resource_name, method)| {
390-
let func_name = method.ident.to_snake_case();
391-
let func_ident = format_ident!("{}", func_name);
392-
393-
let params = self.print_function_params(&method.params, &BorrowMode::Owned);
394-
395-
let param_idents = method
396-
.params
397-
.iter()
398-
.map(|(ident, _)| format_ident!("{}", ident));
399-
400-
let result = match method.result.as_ref() {
401-
Some(FunctionResult::Anon(ty)) => {
402-
let ty = self.print_ty(ty, &BorrowMode::Owned);
359+
} else {
360+
quote! {
361+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
362+
router.define(
363+
#mod_name,
364+
#func_name,
365+
move |ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>, p: #param_decl| {
366+
let ctx = get_cx(ctx.data());
403367

404-
quote! { #ty }
405-
}
406-
Some(FunctionResult::Named(types)) if types.len() == 1 => {
407-
let (_, ty) = &types[0];
408-
let ty = self.print_ty(ty, &BorrowMode::Owned);
368+
Ok(ctx.#func_ident(#param_acc))
369+
},
370+
)?;
371+
}
372+
}
373+
}
409374

410-
quote! { #ty }
411-
}
412-
Some(FunctionResult::Named(types)) => {
413-
let types = types
414-
.iter()
415-
.map(|(_, ty)| self.print_ty(ty, &BorrowMode::Owned));
375+
fn print_router_method_definition(
376+
&self,
377+
mod_name: &str,
378+
resource_name: &str,
379+
method: &Function,
380+
) -> TokenStream {
381+
let func_name = method.ident.to_snake_case();
382+
let func_ident = format_ident!("{}", func_name);
416383

417-
quote! { (#(#types),*) }
418-
}
419-
_ => quote! { () },
420-
};
384+
let param_decl = method
385+
.params
386+
.iter()
387+
.map(|(_, ty)| self.print_ty(ty, &BorrowMode::Owned));
388+
389+
let param_acc = match method.params.len() {
390+
0 => quote! {},
391+
1 => quote! { p.1 },
392+
_ => {
393+
let ids = method.params.iter().enumerate().map(|(i, _)| {
394+
let i = Literal::usize_unsuffixed(i + 1);
395+
quote! { p.#i }
396+
});
397+
quote! { #(#ids),* }
398+
}
399+
};
421400

422-
let mod_name = format!("{mod_name}::resource::{resource_name}");
423-
let get_r_ident = format_ident!("get_{}", resource_name.to_snake_case());
401+
let mod_name = format!("{mod_name}::resource::{resource_name}");
402+
let get_r_ident = format_ident!("get_{}", resource_name.to_snake_case());
424403

404+
if self.opts.async_ {
405+
quote! {
406+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
407+
router.define_async(
408+
#mod_name,
409+
#func_name,
410+
move |ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>, p: (::tauri_bindgen_host::ResourceId, #(#param_decl),*)| {
411+
let get_cx = get_cx.clone();
412+
Box::pin(async move {
413+
let ctx = get_cx(ctx.data());
414+
let r = ctx.#get_r_ident(p.0)?;
415+
Ok(r.#func_ident(#param_acc).await)
416+
})
417+
})?;
418+
}
419+
} else {
425420
quote! {
426421
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
427-
router.func_wrap(
422+
router.define(
428423
#mod_name,
429424
#func_name,
430425
move |
431426
ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>,
432-
this_rid: ::tauri_bindgen_host::ResourceId,
433-
#params
434-
| -> ::tauri_bindgen_host::anyhow::Result<#result> {
427+
p: (::tauri_bindgen_host::ResourceId, #(#param_decl),*)
428+
| {
435429
let ctx = get_cx(ctx.data());
436-
let r = ctx.#get_r_ident(this_rid)?;
437-
438-
Ok(r.#func_ident(#(#param_idents),*))
430+
let r = ctx.#get_r_ident(p.0)?;
431+
Ok(r.#func_ident(#param_acc))
439432
},
440433
)?;
441434
}
435+
}
436+
}
437+
438+
fn print_add_to_router<'a>(
439+
&self,
440+
mod_ident: &str,
441+
functions: impl Iterator<Item = &'a Function>,
442+
methods: impl Iterator<Item = (&'a str, &'a Function)>,
443+
) -> TokenStream {
444+
let trait_ident = format_ident!("{}", mod_ident.to_upper_camel_case());
445+
446+
let mod_name = mod_ident.to_snake_case();
447+
448+
let functions = functions.map(|func| self.print_router_fn_definition(&mod_name, func));
449+
450+
let methods = methods.map(|(resource_name, method)| {
451+
self.print_router_method_definition(&mod_name, resource_name, method)
442452
});
443453

444454
quote! {

crates/gen-host/tests/async/resources.rs

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ pub mod resources {
2121
}
2222
#[::tauri_bindgen_host::async_trait]
2323
pub trait Resources: Sized {
24-
type A: A;
25-
fn get_a_mut(&mut self, id: ::tauri_bindgen_host::ResourceId) -> &mut Self::A;
26-
type B: B;
27-
fn get_b_mut(&mut self, id: ::tauri_bindgen_host::ResourceId) -> &mut Self::B;
24+
type A: A + Send + Sync;
25+
fn get_a(
26+
&self,
27+
id: ::tauri_bindgen_host::ResourceId,
28+
) -> ::tauri_bindgen_host::Result<::std::sync::Arc<Self::A>>;
29+
type B: B + Send + Sync;
30+
fn get_b(
31+
&self,
32+
id: ::tauri_bindgen_host::ResourceId,
33+
) -> ::tauri_bindgen_host::Result<::std::sync::Arc<Self::B>>;
2834
async fn constructor_a(&self) -> ::tauri_bindgen_host::ResourceId;
2935
async fn constructor_b(&self) -> ::tauri_bindgen_host::ResourceId;
3036
}
@@ -63,6 +69,114 @@ pub mod resources {
6369
})
6470
},
6571
)?;
72+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
73+
router
74+
.define_async(
75+
"resources::resource::a",
76+
"f1",
77+
move |
78+
ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>,
79+
p: (::tauri_bindgen_host::ResourceId,)|
80+
{
81+
let get_cx = get_cx.clone();
82+
Box::pin(async move {
83+
let ctx = get_cx(ctx.data());
84+
let r = ctx.get_a(p.0)?;
85+
Ok(r.f1().await)
86+
})
87+
},
88+
)?;
89+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
90+
router
91+
.define_async(
92+
"resources::resource::a",
93+
"f2",
94+
move |
95+
ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>,
96+
p: (::tauri_bindgen_host::ResourceId, u32)|
97+
{
98+
let get_cx = get_cx.clone();
99+
Box::pin(async move {
100+
let ctx = get_cx(ctx.data());
101+
let r = ctx.get_a(p.0)?;
102+
Ok(r.f2(p.1).await)
103+
})
104+
},
105+
)?;
106+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
107+
router
108+
.define_async(
109+
"resources::resource::a",
110+
"f3",
111+
move |
112+
ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>,
113+
p: (::tauri_bindgen_host::ResourceId, u32, u32)|
114+
{
115+
let get_cx = get_cx.clone();
116+
Box::pin(async move {
117+
let ctx = get_cx(ctx.data());
118+
let r = ctx.get_a(p.0)?;
119+
Ok(r.f3(p.1, p.2).await)
120+
})
121+
},
122+
)?;
123+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
124+
router
125+
.define_async(
126+
"resources::resource::b",
127+
"f1",
128+
move |
129+
ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>,
130+
p: (::tauri_bindgen_host::ResourceId,)|
131+
{
132+
let get_cx = get_cx.clone();
133+
Box::pin(async move {
134+
let ctx = get_cx(ctx.data());
135+
let r = ctx.get_b(p.0)?;
136+
Ok(r.f1().await)
137+
})
138+
},
139+
)?;
140+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
141+
router
142+
.define_async(
143+
"resources::resource::b",
144+
"f2",
145+
move |
146+
ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>,
147+
p: (
148+
::tauri_bindgen_host::ResourceId,
149+
::tauri_bindgen_host::ResourceId,
150+
)|
151+
{
152+
let get_cx = get_cx.clone();
153+
Box::pin(async move {
154+
let ctx = get_cx(ctx.data());
155+
let r = ctx.get_b(p.0)?;
156+
Ok(r.f2(p.1).await)
157+
})
158+
},
159+
)?;
160+
let get_cx = ::std::sync::Arc::clone(&wrapped_get_cx);
161+
router
162+
.define_async(
163+
"resources::resource::b",
164+
"f3",
165+
move |
166+
ctx: ::tauri_bindgen_host::ipc_router_wip::Caller<T>,
167+
p: (
168+
::tauri_bindgen_host::ResourceId,
169+
Option<Vec<::tauri_bindgen_host::ResourceId>>,
170+
)|
171+
{
172+
let get_cx = get_cx.clone();
173+
Box::pin(async move {
174+
let ctx = get_cx(ctx.data());
175+
let r = ctx.get_b(p.0)?;
176+
Ok(r.f3(p.1).await)
177+
})
178+
},
179+
)?;
66180
Ok(())
67181
}
68182
}

0 commit comments

Comments
 (0)