Skip to content

Commit 1a26ff4

Browse files
authored
Allow context types to delegate components directly (#175)
* Use DelegateComponent in consumer blanket implementation directly * Remove HasCgpProvider trait * Remove HasCgpProvider from comments * Move component tests to separate test unit * Add basic consumer delegate test * Test generic consumer delegate * Move preset tests into separate test unit * Test consumer delegation in preset
1 parent d194a24 commit 1a26ff4

File tree

59 files changed

+189
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+189
-96
lines changed

crates/cgp-component/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
mod traits;
99
mod types;
1010

11-
pub use traits::{CanUseComponent, DelegateComponent, HasCgpProvider, IsProviderFor};
11+
pub use traits::{CanUseComponent, DelegateComponent, IsProviderFor};
1212
pub use types::{UseContext, UseDelegate, UseFields, WithContext, WithProvider};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use crate::{HasCgpProvider, IsProviderFor};
1+
use crate::{DelegateComponent, IsProviderFor};
22

33
/**
44
This is a convenient type alias that is used in the same way as [`IsProviderFor`],
55
but with the `Self` type being the `Context` type rather than the `Provider` type
66
that implements the provider trait.
77
88
The `CanUseComponent` trait is automatically implemented for any CGP `Context` type
9-
that implements the `HasCgpProvider` trait, and when `Contex::CgpProvider` implements
10-
`IsProviderFor<Component, Context, Params>`.
9+
that implements the `DelegateComponent<Component>` trait, and when `Contex::Delegate`
10+
implements `IsProviderFor<Component, Context, Params>`.
1111
1212
This trait is used by `check_components!` to check whether a `Context` implements
1313
a given `Component` through its provider. When there are unsatisfied constraints,
@@ -17,7 +17,7 @@ pub trait CanUseComponent<Component, Params: ?Sized = ()> {}
1717

1818
impl<Context, Component, Params: ?Sized> CanUseComponent<Component, Params> for Context
1919
where
20-
Context: HasCgpProvider,
21-
Context::CgpProvider: IsProviderFor<Component, Context, Params>,
20+
Context: DelegateComponent<Component>,
21+
Context::Delegate: IsProviderFor<Component, Context, Params>,
2222
{
2323
}

crates/cgp-component/src/traits/has_provider.rs

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
mod can_use_component;
22
mod delegate_component;
3-
mod has_provider;
43
mod is_provider;
54

65
pub use can_use_component::*;
76
pub use delegate_component::DelegateComponent;
8-
pub use has_provider::HasCgpProvider;
97
pub use is_provider::*;

crates/cgp-core/src/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ pub use core::marker::PhantomData;
22

33
pub use cgp_async_macro::async_trait;
44
pub use cgp_component::{
5-
CanUseComponent, DelegateComponent, HasCgpProvider, IsProviderFor, UseContext, UseDelegate,
6-
UseFields, WithContext, WithProvider,
5+
CanUseComponent, DelegateComponent, IsProviderFor, UseContext, UseDelegate, UseFields,
6+
WithContext, WithProvider,
77
};
88
pub use cgp_error::{CanRaiseError, CanWrapError, HasErrorType};
99
pub use cgp_field::impls::{IsMut, IsNothing, IsPresent, IsRef, IsVoid, UseField};

crates/cgp-error/src/traits/can_raise_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cgp_component::{DelegateComponent, HasCgpProvider, IsProviderFor, UseContext, UseDelegate};
1+
use cgp_component::{DelegateComponent, IsProviderFor, UseContext, UseDelegate};
22
use cgp_macro::cgp_component;
33

44
use crate::traits::has_error_type::HasErrorType;

crates/cgp-error/src/traits/can_wrap_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cgp_component::{DelegateComponent, HasCgpProvider, IsProviderFor, UseContext, UseDelegate};
1+
use cgp_component::{DelegateComponent, IsProviderFor, UseContext, UseDelegate};
22
use cgp_macro::cgp_component;
33

44
use crate::traits::HasErrorType;

crates/cgp-error/src/traits/has_error_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::fmt::Debug;
22

3-
use cgp_component::{DelegateComponent, HasCgpProvider, IsProviderFor, UseContext, WithProvider};
3+
use cgp_component::{DelegateComponent, IsProviderFor, UseContext, WithProvider};
44
use cgp_macro::cgp_type;
55
use cgp_type::{ProvideType, UseType};
66

crates/cgp-macro-lib/src/derive_component/consumer_impl.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use alloc::vec::Vec;
33

44
use proc_macro2::Span;
55
use quote::{ToTokens, quote};
6+
use syn::punctuated::Punctuated;
67
use syn::spanned::Spanned;
7-
use syn::token::{Brace, Eq, For, Impl};
8+
use syn::token::{Brace, Comma, Eq, For, Impl};
89
use syn::{
910
Error, GenericParam, Generics, Ident, ImplItem, ImplItemConst, ItemImpl, ItemTrait, Path,
1011
TraitItem, TypeParamBound, Visibility, parse2,
@@ -17,6 +18,8 @@ pub fn derive_consumer_impl(
1718
consumer_trait: &ItemTrait,
1819
provider_name: &Ident,
1920
context_type: &Ident,
21+
component_name: &Ident,
22+
component_params: &Punctuated<Ident, Comma>,
2023
) -> syn::Result<ItemImpl> {
2124
let consumer_name = &consumer_trait.ident;
2225

@@ -61,7 +64,7 @@ pub fn derive_consumer_impl(
6164

6265
{
6366
let has_component_constraint: TypeParamBound = parse2(quote! {
64-
HasCgpProvider
67+
DelegateComponent< #component_name < #component_params > >
6568
})?;
6669

6770
let provider_constraint: TypeParamBound = parse2(quote! {
@@ -75,14 +78,14 @@ pub fn derive_consumer_impl(
7578
})?);
7679

7780
where_clause.predicates.push(parse2(quote! {
78-
#context_type :: CgpProvider : #provider_constraint
81+
#context_type :: Delegate : #provider_constraint
7982
})?);
8083
}
8184
_ => {
8285
generics.where_clause = Some(parse2(quote! {
8386
where
8487
#context_type : #has_component_constraint,
85-
#context_type :: CgpProvider : #provider_constraint
88+
#context_type :: Delegate : #provider_constraint
8689
})?);
8790
}
8891
}
@@ -98,7 +101,7 @@ pub fn derive_consumer_impl(
98101
TraitItem::Fn(trait_fn) => {
99102
let impl_fn = derive_delegated_fn_impl(
100103
&trait_fn.sig,
101-
&parse2(quote!(#context_type :: CgpProvider))?,
104+
&parse2(quote!(#context_type :: Delegate))?,
102105
)?;
103106

104107
impl_items.push(ImplItem::Fn(impl_fn));
@@ -121,7 +124,7 @@ pub fn derive_consumer_impl(
121124
let impl_type = derive_delegate_type_impl(
122125
trait_type,
123126
parse2(quote!(
124-
< #context_type :: CgpProvider as #provider_name < #provider_type_generics > > :: #type_name #type_generics
127+
< #context_type :: Delegate as #provider_name < #provider_type_generics > > :: #type_name #type_generics
125128
))?,
126129
);
127130

@@ -132,7 +135,7 @@ pub fn derive_consumer_impl(
132135
let (_, type_generics, _) = trait_item_const.generics.split_for_impl();
133136

134137
let impl_expr = parse2(quote! {
135-
< #context_type :: CgpProvider as #provider_name < #provider_type_generics > > :: #const_ident #type_generics
138+
< #context_type :: Delegate as #provider_name < #provider_type_generics > > :: #const_ident #type_generics
136139
})?;
137140

138141
let impl_item_const = ImplItemConst {

crates/cgp-macro-lib/src/derive_component/derive.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ pub fn derive_component_with_ast(
3131
context_type,
3232
)?;
3333

34-
let consumer_impl = derive_consumer_impl(&consumer_trait, provider_name, context_type)?;
34+
let consumer_impl = derive_consumer_impl(
35+
&consumer_trait,
36+
provider_name,
37+
context_type,
38+
component_name,
39+
component_params,
40+
)?;
3541

3642
let provider_impl = derive_provider_impl(
3743
context_type,

0 commit comments

Comments
 (0)