diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-05-16 16:24:55 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-05-16 16:24:55 +0800 |
| commit | a5647e89bc5110a07c8f3c695c3c9582aa31d354 (patch) | |
| tree | 63c5be475c53b578c7ff5fe0d454899c0aa315b1 /mingling_macros/src/chain.rs | |
| parent | f7964129a5b5e41fd6da0f191a7e37e93bd36814 (diff) | |
Simplify example imports to use prelude and add resources example
Add a new example demonstrating global resource injection in chain
functions,
and update all existing examples to import from `mingling::prelude`
instead
of individual macro paths. Also add `example-resources` to the workspace
exclude list.
Diffstat (limited to 'mingling_macros/src/chain.rs')
| -rw-r--r-- | mingling_macros/src/chain.rs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/mingling_macros/src/chain.rs b/mingling_macros/src/chain.rs index 496a0a4..9d3a901 100644 --- a/mingling_macros/src/chain.rs +++ b/mingling_macros/src/chain.rs @@ -213,6 +213,19 @@ pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream { quote! { #group_name } }; + // Check for async fn + &mut combination, which is not supported + #[cfg(feature = "async")] + if is_async_fn { + if let Some(mut_res) = resources.iter().find(|r| r.is_mut) { + return syn::Error::new( + mut_res.var_name.span(), + "Cannot use `&mut` resource injection in async chain function. ", + ) + .to_compile_error() + .into(); + } + } + // Separate resources into immutable refs and mutable refs let immut_resources: Vec<_> = resources.iter().filter(|r| !r.is_mut).collect(); let mut_resources: Vec<_> = resources.iter().filter(|r| r.is_mut).collect(); @@ -243,9 +256,9 @@ pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream { }) .collect(); - // Build nested __modify_res_and_return_any wrappers for mutable references - // The innermost layer is the original function body, wrapping outward for each mutable resource. - // The function returns a value, so the return values need to be properly chained. + // Build nested __modify_res_and_return_any wrappers for mutable references. + // The innermost layer is the original function body, wrapping outward for each + // mutable resource. let body_stmts = &fn_body.stmts; let mut wrapped_body = quote! { #(#body_stmts)* @@ -253,7 +266,6 @@ pub fn chain_attr(attr: TokenStream, item: TokenStream) -> TokenStream { // Wrap from inside to outside: the first mutable parameter becomes the outermost wrapper, // and the last mutable parameter becomes the innermost wrapper. - // Therefore iterate mut_resources and wrap outward. for res in mut_resources.iter() { let var_name = &res.var_name; let inner_type = &res.inner_type; |
