Skip to content

Commit c035d64

Browse files
committed
Add section for #[cgp_context] macro
1 parent 3981b41 commit c035d64

1 file changed

Lines changed: 34 additions & 8 deletions

File tree

content/component-macros.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,38 @@ delegate_components! {
257257
}
258258
```
259259

260+
## `#[cgp_context]` Macro
261+
262+
The `#[cgp_context]` macro can be applied on a context struct, to automatically define the provider struct for the context and implement `HasCgpProvider` for the context.
263+
264+
Given the following context definition:
265+
266+
```rust,ignore
267+
#[cgp_context(MyContextComponents)]
268+
pub struct MyContext {
269+
...
270+
}
271+
```
272+
273+
The macro will generate the following constructs:
274+
275+
```rust,ignore
276+
pub struct MyContextComponents;
277+
278+
impl HasCgpProvider for MyContext {
279+
type CgpProvider = MyContextComponents;
280+
}
281+
```
282+
283+
If the context provider name follows the pattern `{ContextName}Components`, then the macro attribute argument can be omitted, and the code can be simplified to:
284+
285+
```rust,ignore
286+
#[cgp_context]
287+
pub struct MyContext {
288+
...
289+
}
290+
```
291+
260292
## `#[cgp_provider]` Macro
261293

262294
When implementing a provider, the `#[cgp_provider]` macro needs to be used to automatically implement the `IsProviderFor` implementation, with all constraints within the `impl` block copied over.
@@ -464,19 +496,13 @@ where
464496
}
465497

466498
// Concrete context and wiring
467-
499+
#[cgp_context]
468500
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
469501
pub struct Person {
470502
pub first_name: String,
471503
pub last_name: String,
472504
}
473505

474-
pub struct PersonComponents;
475-
476-
impl HasCgpProvider for Person {
477-
type CgpProvider = PersonComponents;
478-
}
479-
480506
delegate_and_check_components! {
481507
CanUsePerson for Person;
482508
PersonComponents {
@@ -519,4 +545,4 @@ how context-generic programming can be done in Rust, and how it can help
519545
build better Rust applications.
520546

521547
In the chapters that follow, we will make heavy use of `cgp` and its
522-
macros to dive further into the world of context-generic programming.
548+
macros to dive further into the world of context-generic programming.

0 commit comments

Comments
 (0)