@@ -22,7 +22,7 @@ The separation of provider traits from consumer traits allows multiple context-g
22
22
provider implementations to be defined, bypassing Rust's trait system's original restriction
23
23
that forbids overlapping implementations.
24
24
25
- ## Expressive Ways to Write Code
25
+ ## Highly Expressive Code
26
26
27
27
With CGP, one can easily write _ abstract programs_ that is generic over
28
28
a context, together with all its associated types and methods. CGP allows such
@@ -91,7 +91,11 @@ First, we would import `cgp` and define a greeter component as follows:
91
91
``` rust
92
92
use cgp :: prelude :: * ;
93
93
94
- #[derive_component(GreeterComponent , Greeter <Context >)]
94
+ #[cgp_component {
95
+ name: GreeterComponent ,
96
+ provider: Greeter ,
97
+ context: Context ,
98
+ }]
95
99
pub trait CanGreet {
96
100
fn greet (& self );
97
101
}
@@ -119,7 +123,7 @@ pub struct GreetHello;
119
123
120
124
impl <Context > Greeter <Context > for GreetHello
121
125
where
122
- Context : HasField <symbol ! ("name "), Field : Display >,
126
+ Context : HasField <symbol ! ("name "), Value : Display >,
123
127
{
124
128
fn greet (context : & Context ) {
125
129
println! (
@@ -137,14 +141,14 @@ but with additional constraints (or dependencies) imposed on the
137
141
context.
138
142
139
143
In this example case, the constraint
140
- ` HasField<symbol!("name"), Field : Display> ` means that ` GreetHello `
144
+ ` HasField<symbol!("name"), Value : Display> ` means that ` GreetHello `
141
145
expects ` Context ` to be a struct with a field named ` name ` , with
142
146
the field type being any type that implements ` Display ` .
143
147
144
148
The trait ` HasField ` is a CGP getter trait for accessing fields in a
145
149
struct. The ` symbol! ` macro is used to convert any string literal
146
150
into types, so that they can be used as type argument. The
147
- associated type ` Field ` is implemented as the type of the field in
151
+ associated type ` Value ` is implemented as the type of the field in
148
152
the struct.
149
153
150
154
The ` HasField ` trait provides a ` get_field ` method,
@@ -165,7 +169,7 @@ _dependency injection_, as extra dependencies are "injected" into
165
169
the provider through the context.
166
170
167
171
Compared to other languages, CGP can not only inject methods into
168
- a provider, but also _ types_ , as we seen with the ` Field ` associated
172
+ a provider, but also _ types_ , as we seen with the ` Value ` associated
169
173
type in ` HasField ` .
170
174
171
175
## Person Context
@@ -194,7 +198,7 @@ delegate_components! {
194
198
195
199
The ` Person ` context is defined to be a struct containing a ` name ` field,
196
200
which is of type ` String ` . The CGP macro ` derive(HasField) ` is used to
197
- automatically implement ` Person: HasField<symbol!("name"), Field = String> ` ,
201
+ automatically implement ` Person: HasField<symbol!("name"), Value = String> ` ,
198
202
so that it can be used by ` GreetHello ` .
199
203
200
204
Additionally, we also define an empty struct ` PersonComponents ` , which
@@ -240,7 +244,7 @@ The method `greet` is called from the consumer trait `CanGreet`, which
240
244
is implemented by ` Person ` via ` PersonComponents ` , which implements
241
245
` Greeter ` via delegation of ` GreeterComponent ` to ` GreetHello ` ,
242
246
which implements ` Greeter ` given that ` Person ` implements
243
- ` HasField<symbol!("name"), Field : Display> ` .
247
+ ` HasField<symbol!("name"), Value : Display> ` .
244
248
That is a lot of indirection going on!
245
249
246
250
Hopefully by the end of this tutorial, you have gotten a sense of how
0 commit comments