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
5 changes: 5 additions & 0 deletions crates/cgp-macro-lib/src/cgp_fn/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syn::token::Comma;
use syn::{Attribute, TypeParamBound, WherePredicate};

use crate::cgp_fn::{FunctionAttributes, UseTypeSpec};
use crate::cgp_impl::UseProviderSpec;
use crate::parse::SimpleType;

pub fn parse_function_attributes(
Expand Down Expand Up @@ -32,6 +33,10 @@ pub fn parse_function_attributes(
let use_type = attribute
.parse_args_with(Punctuated::<UseTypeSpec, Comma>::parse_terminated)?;
parsed_attributes.use_type.extend(use_type);
} else if ident == "use_provider" {
let use_provider = attribute
.parse_args_with(Punctuated::<UseProviderSpec, Comma>::parse_terminated)?;
parsed_attributes.use_provider.extend(use_provider);
} else {
attributes.push(attribute);
}
Expand Down
12 changes: 11 additions & 1 deletion crates/cgp-macro-lib/src/cgp_fn/item_impl.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use quote::quote;
use syn::punctuated::Punctuated;
use syn::token::Plus;
use syn::{Generics, Ident, ItemFn, ItemImpl, TypeParamBound, parse2};
use syn::{Generics, Ident, ItemFn, ItemImpl, TypeParamBound, parse_quote, parse2};

use crate::cgp_fn::{
FunctionAttributes, ImplicitArgField, apply_use_type_attributes_to_item_impl,
build_implicit_args_bounds,
};
use crate::cgp_impl::derive_provider_bounds;

pub fn derive_item_impl(
trait_ident: &Ident,
Expand Down Expand Up @@ -69,5 +70,14 @@ pub fn derive_item_impl(
item_impl = apply_use_type_attributes_to_item_impl(&item_impl, &attributes.use_type)?;
}

if !attributes.use_provider.is_empty() {
let where_clause = item_impl.generics.make_where_clause();

for spec in attributes.use_provider.iter() {
let provider_bounds = derive_provider_bounds(&parse_quote! { Self }, spec)?;
where_clause.predicates.push(provider_bounds);
}
}

Ok(item_impl)
}
2 changes: 2 additions & 0 deletions crates/cgp-macro-lib/src/cgp_fn/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use syn::token::Mut;
use syn::{Ident, Type, TypeParamBound, WherePredicate};

use crate::cgp_fn::UseTypeSpec;
use crate::cgp_impl::UseProviderSpec;
use crate::derive_getter::FieldMode;
use crate::parse::SimpleType;

Expand All @@ -20,4 +21,5 @@ pub struct FunctionAttributes {
pub extend_where: Vec<WherePredicate>,
pub uses: Vec<SimpleType>,
pub use_type: Vec<UseTypeSpec>,
pub use_provider: Vec<UseProviderSpec>,
}
3 changes: 0 additions & 3 deletions crates/cgp-macro-lib/src/cgp_impl/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use syn::{Error, ItemImpl, TypeParamBound, parse_quote, parse2};
use crate::cgp_fn::{apply_use_type_attributes_to_item_impl, build_implicit_args_bounds};
use crate::cgp_impl::attributes::parse_impl_attributes;
use crate::cgp_impl::provider_bounds::derive_provider_bounds;
use crate::cgp_impl::provider_call::transform_provider_call;
use crate::cgp_impl::{ImplProviderSpec, derive_provider_impl, implicit_args};
use crate::derive_provider::{
derive_component_name_from_provider_impl, derive_is_provider_for, derive_provider_struct,
Expand Down Expand Up @@ -58,8 +57,6 @@ pub fn derive_cgp_impl(
let provider_bounds = derive_provider_bounds(&parse_quote! { Self }, spec)?;
where_clause.predicates.push(provider_bounds);
}

transform_provider_call(&mut item_impl)?;
}

if spec.provider_type == parse_quote! { Self } {
Expand Down
3 changes: 2 additions & 1 deletion crates/cgp-macro-lib/src/cgp_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ mod attributes;
mod derive;
mod implicit_args;
mod provider_bounds;
mod provider_call;
mod provider_impl;
mod spec;
mod transform;
mod use_provider;

pub use derive::*;
pub use provider_bounds::*;
pub use provider_impl::*;
pub use spec::*;
pub use transform::*;
pub use use_provider::*;
80 changes: 0 additions & 80 deletions crates/cgp-macro-lib/src/cgp_impl/provider_call.rs

This file was deleted.

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
Expand Up @@ -8,6 +8,7 @@ pub mod multi;
pub mod mutable;
pub mod nested_foreign_type;
pub mod type_equality;
pub mod use_provider;
pub mod use_type;
pub mod use_type_alias;
pub mod uses;
35 changes: 35 additions & 0 deletions crates/cgp-tests/tests/cgp_fn_tests/use_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use cgp::prelude::*;

#[cgp_component(AreaCalculator)]
pub trait CanCalculateArea {
fn area(&self) -> f64;
}

#[cgp_impl(new RectangleAreaCalculator)]
impl AreaCalculator {
fn area(&self, #[implicit] width: f64, #[implicit] height: f64) -> f64 {
width * height
}
}

#[cgp_fn]
#[use_provider(RectangleAreaCalculator: AreaCalculator)]
fn rectangle_area(&self) -> f64 {
RectangleAreaCalculator::area(self)
}

#[derive(HasField)]
pub struct Rectangle {
pub width: f64,
pub height: f64,
}

#[test]
fn test_use_provider() {
let rectangle = Rectangle {
width: 3.0,
height: 4.0,
};

assert_eq!(rectangle.rectangle_area(), 12.0);
}
3 changes: 1 addition & 2 deletions crates/cgp-tests/tests/component_tests/cgp_impl/impl_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub struct Rectangle {
#[use_provider(RectangleArea: AreaCalculator)]
impl CanCalculateArea for Rectangle {
fn area(&self) -> f64 {
#[use_provider(RectangleArea)]
self.area()
RectangleArea::area(self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ impl AreaCalculator {
#[use_provider(Inner: AreaCalculator)]
impl<Inner> AreaCalculator {
fn area(&self, #[implicit] scale_factor: f64) -> f64 {
let base_area = #[use_provider(Inner)]
self.area();

base_area * scale_factor * scale_factor
Inner::area(self) * scale_factor * scale_factor
}
}

Expand Down
Loading