From 558c213b2ea5065e43f38afa2fc4bedab6b8a5aa Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Thu, 26 Mar 2026 21:19:43 +0100 Subject: [PATCH 1/2] Move function attributes in `#[cgp_fn]` to trait and impl --- crates/cgp-macro-lib/src/cgp_fn/attributes.rs | 17 +++++++---------- crates/cgp-macro-lib/src/cgp_fn/derive.rs | 8 ++++++-- crates/cgp-tests/tests/cgp_fn_tests/multi.rs | 5 +++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/crates/cgp-macro-lib/src/cgp_fn/attributes.rs b/crates/cgp-macro-lib/src/cgp_fn/attributes.rs index 433edc08..3a7653a5 100644 --- a/crates/cgp-macro-lib/src/cgp_fn/attributes.rs +++ b/crates/cgp-macro-lib/src/cgp_fn/attributes.rs @@ -1,5 +1,3 @@ -use core::mem; - use syn::punctuated::Punctuated; use syn::token::Comma; use syn::{Attribute, TypeParamBound, WherePredicate}; @@ -9,13 +7,12 @@ use crate::cgp_impl::UseProviderSpec; use crate::parse::SimpleType; pub fn parse_function_attributes( - attributes: &mut Vec, -) -> syn::Result { + attributes: Vec, +) -> syn::Result<(FunctionAttributes, Vec)> { let mut parsed_attributes = FunctionAttributes::default(); + let mut rest_attributes = Vec::new(); - let in_attributes = mem::take(attributes); - - for attribute in in_attributes.into_iter() { + for attribute in attributes.into_iter() { if let Some(ident) = attribute.path().get_ident() { if ident == "extend" { let extend_bound = attribute @@ -38,12 +35,12 @@ pub fn parse_function_attributes( .parse_args_with(Punctuated::::parse_terminated)?; parsed_attributes.use_provider.extend(use_provider); } else { - attributes.push(attribute); + rest_attributes.push(attribute); } } else { - attributes.push(attribute); + rest_attributes.push(attribute); } } - Ok(parsed_attributes) + Ok((parsed_attributes, rest_attributes)) } diff --git a/crates/cgp-macro-lib/src/cgp_fn/derive.rs b/crates/cgp-macro-lib/src/cgp_fn/derive.rs index 14c1dd3f..6850c04f 100644 --- a/crates/cgp-macro-lib/src/cgp_fn/derive.rs +++ b/crates/cgp-macro-lib/src/cgp_fn/derive.rs @@ -15,15 +15,17 @@ pub fn derive_cgp_fn(trait_ident: &Ident, mut item_fn: ItemFn) -> syn::Result syn::Result>::{Foo as FooX}, >::{Foo as FooY}, HasBarType::{Bar, Baz})] -pub fn do_foo_bar( +pub async fn do_foo_bar( &self, x: X, #[implicit] foo_x: &FooX, From bf9e26a1dcf40dd59d85088cbddbde719b3ebe33 Mon Sep 17 00:00:00 2001 From: Soares Chen Date: Thu, 26 Mar 2026 21:21:21 +0100 Subject: [PATCH 2/2] Add test for async function --- crates/cgp-tests/tests/cgp_fn_tests/async.rs | 7 +++++++ crates/cgp-tests/tests/cgp_fn_tests/mod.rs | 1 + 2 files changed, 8 insertions(+) create mode 100644 crates/cgp-tests/tests/cgp_fn_tests/async.rs diff --git a/crates/cgp-tests/tests/cgp_fn_tests/async.rs b/crates/cgp-tests/tests/cgp_fn_tests/async.rs new file mode 100644 index 00000000..a4110246 --- /dev/null +++ b/crates/cgp-tests/tests/cgp_fn_tests/async.rs @@ -0,0 +1,7 @@ +use cgp::prelude::*; + +#[cgp_fn] +#[async_trait] +pub async fn greet(&self, #[implicit] name: &str) -> String { + format!("Hello, {}!", name) +} diff --git a/crates/cgp-tests/tests/cgp_fn_tests/mod.rs b/crates/cgp-tests/tests/cgp_fn_tests/mod.rs index 5db1140a..829f72ac 100644 --- a/crates/cgp-tests/tests/cgp_fn_tests/mod.rs +++ b/crates/cgp-tests/tests/cgp_fn_tests/mod.rs @@ -1,3 +1,4 @@ +pub mod r#async; pub mod basic; pub mod call; pub mod extend;