Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions crates/cgp-macro-lib/src/cgp_fn/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::mem;

use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::{Attribute, TypeParamBound, WherePredicate};
Expand All @@ -9,13 +7,12 @@ use crate::cgp_impl::UseProviderSpec;
use crate::parse::SimpleType;

pub fn parse_function_attributes(
attributes: &mut Vec<Attribute>,
) -> syn::Result<FunctionAttributes> {
attributes: Vec<Attribute>,
) -> syn::Result<(FunctionAttributes, Vec<Attribute>)> {
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
Expand All @@ -38,12 +35,12 @@ pub fn parse_function_attributes(
.parse_args_with(Punctuated::<UseProviderSpec, Comma>::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))
}
8 changes: 6 additions & 2 deletions crates/cgp-macro-lib/src/cgp_fn/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ pub fn derive_cgp_fn(trait_ident: &Ident, mut item_fn: ItemFn) -> syn::Result<To

let implicit_args = extract_and_parse_implicit_args(&mut item_fn.sig.inputs)?;

let attributes = parse_function_attributes(&mut item_fn.attrs)?;
let (attributes, trait_attributes) =
parse_function_attributes(core::mem::take(&mut item_fn.attrs))?;

inject_implicit_args(&implicit_args, &mut item_fn.block)?;

let generics = mem::take(&mut item_fn.sig.generics);

let mut item_trait = derive_item_trait(trait_ident, &item_fn, &generics, &attributes)?;
item_trait.attrs.extend(trait_attributes.clone());

let item_impl = derive_item_impl(
let mut item_impl = derive_item_impl(
trait_ident,
&item_fn,
&implicit_args,
&generics,
&attributes,
)?;

item_impl.attrs.extend(trait_attributes);

item_trait.vis = visibility.clone();

let output = quote! {
Expand Down
7 changes: 7 additions & 0 deletions crates/cgp-tests/tests/cgp_fn_tests/async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use cgp::prelude::*;

#[cgp_fn]
#[async_trait]
pub async fn greet(&self, #[implicit] name: &str) -> String {
format!("Hello, {}!", name)
}
1 change: 1 addition & 0 deletions crates/cgp-tests/tests/cgp_fn_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod r#async;
pub mod basic;
pub mod call;
pub mod extend;
Expand Down
5 changes: 3 additions & 2 deletions crates/cgp-tests/tests/cgp_fn_tests/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ pub trait HasBarType {
type Baz;
}

#[allow(unused)]
#[cgp_fn]
#[allow(unused)]
#[async_trait]
#[use_type(<HasFooType<X>>::{Foo as FooX}, <HasFooType<Y>>::{Foo as FooY}, HasBarType::{Bar, Baz})]
pub fn do_foo_bar<X, Y>(
pub async fn do_foo_bar<X, Y>(
&self,
x: X,
#[implicit] foo_x: &FooX,
Expand Down
Loading