@@ -3,28 +3,26 @@ use core::fmt::Display;
33use cgp:: prelude:: * ; // Import all CGP constructs
44
55// Derive CGP provider traits and blanket implementations
6- #[ cgp_component {
7- name: GreeterComponent , // Name of the CGP component
8- provider: Greeter , // Name of the provider trait
9- } ]
6+ #[ cgp_component( Greeter ) ]
107pub trait CanGreet // Name of the consumer trait
118{
129 fn greet ( & self ) ;
1310}
1411
1512// Declare a CGP abstract type `Name`
16- cgp_type ! ( Name ) ;
13+ #[ cgp_type]
14+ pub trait HasNameType {
15+ type Name ;
16+ }
1717
1818// A getter trait representing a dependency for `name` value
1919#[ cgp_auto_getter] // Derive blanket implementation
2020pub trait HasName : HasNameType {
2121 fn name ( & self ) -> & Self :: Name ;
2222}
2323
24- // A provider that implements `Greeter`
25- pub struct GreetHello ;
26-
2724// Implement `Greeter` that is generic over `Context`
25+ #[ cgp_new_provider]
2826impl < Context > Greeter < Context > for GreetHello
2927where
3028 Context : HasName , // Inject the `name` dependency from `Context`
@@ -37,24 +35,20 @@ where
3735}
3836
3937// A concrete context that uses CGP components
38+ #[ cgp_context]
4039#[ derive( HasField ) ] // Deriving `HasField` automatically implements `HasName`
4140pub struct Person {
4241 pub name : String ,
4342}
4443
45- // The CGP components mapping for `Person`
46- pub struct PersonComponents ;
47-
48- // Set CGP components used by `Person` to be `PersonComponents`
49- impl HasComponents for Person {
50- type Components = PersonComponents ;
51- }
52-
5344// Compile-time wiring of CGP components
54- delegate_components ! {
45+ delegate_and_check_components ! {
46+ CanUsePerson for Person ; // Define check trait to check implementation on Person
5547 PersonComponents {
56- NameTypeComponent : UseType <String >,
57- GreeterComponent : GreetHello , // Use `GreetHello` to provide `Greeter`
48+ NameTypeProviderComponent :
49+ UseType <String >, // Instantiate the `Name` type to `String`
50+ GreeterComponent :
51+ GreetHello , // Use `GreetHello` to provide `Greeter`
5852 }
5953}
6054
0 commit comments