Skip to content

Commit 726e01d

Browse files
committed
AI-revise section
1 parent 9283386 commit 726e01d

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

content/field-accessors.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ In this updated code, we use the [`bearer_auth`](https://docs.rs/reqwest/latest/
314314

315315
## Traits with Multiple Getter Methods
316316

317-
When creating providers like `ReadMessageFromApi`, which often need to use both `HasApiBaseUrl` and `HasAuthToken`, an alternative design would be to combine these two traits into a single one, containing both accessor methods:
317+
As you build providers that need access to various pieces of data – like a `ReadMessageFromApi` provider that requires both an `api_base_url` and an `auth_token` – you might initially consider grouping these accessors into a single trait.
318+
319+
Here's an example of how you *could* define a trait with multiple getter methods for API client fields:
318320

319321
```rust
320322
# extern crate cgp;
@@ -335,11 +337,15 @@ pub trait HasApiClientFields: HasAuthTokenType {
335337
```
336338

337339

338-
While this approach works, it introduces unnecessary coupling between the `api_base_url` and `auth_token` fields. If a provider only requires `api_base_url` but not `auth_token`, it would still need to include the unnecessary `auth_token` dependency. Additionally, this design prevents us from implementing separate providers that could provide the `api_base_url` and `auth_token` fields independently, each with its own logic.
340+
While this approach is syntactically valid, it introduces unnecessary coupling. If a provider only needs the `api_base_url`, it still inherits a dependency on the `auth_token`. This could potentially impact modularity and reusability.
341+
342+
Furthermore, this design prevents you from defining separate, independent providers for `api_base_url` and `auth_token`, each potentially with its own distinct logic for obtaining that data.
343+
344+
Traits containing only one method also unlock powerful patterns like the [`UseField`](./use-field-pattern.md) pattern (which we'll introduce later). This pattern significantly simplifies the boilerplate required to implement accessor traits and enables the creation of reusable accessor providers.
339345

340-
Furthermore, traits that contain only one method can benefit from the [`UseField`](./use-field-pattern.md) pattern that will be introduced later, which helps to simplify the boilerplate required to implement accessor traits, as well as allowing reusable accessor providers to be defined.
346+
Ultimately, CGP provides the flexibility to design your traits in a way that suits your needs. You are not strictly prevented from defining multiple accessor methods within a single trait, or even mixing getters with associated types or other methods.
341347

342-
Ultimately, CGP does not prevent you from defining multiple accessor methods into one trait, or even mix the trait with other items such as associated types or non-getter methods. It is your own decision of how to design CGP traits. Just keep in mind that it is common in CGP to see accessor traits that each contain only one getter method.
348+
However, you may find that defining accessor traits that each contain only one getter method is a frequently used pattern, often chosen for its potential benefits in modularity, reusability, and compatibility with helper patterns like UseField. You'll see this pattern frequently demonstrated throughout this book.
343349

344350
## Implementing Accessor Providers
345351

0 commit comments

Comments
 (0)