|
| 1 | +# Provider |
1 | 2 |
|
2 | | -# Provider |
| 3 | +In CGP, a _provider_ is a piece of code that _implements_ certain functionality |
| 4 | +for a context. At its most basic, a provider is consist of an `impl` block for |
| 5 | +a trait. |
| 6 | + |
| 7 | +```rust |
| 8 | +trait HasName { |
| 9 | + fn name(&self) -> &str; |
| 10 | +} |
| 11 | + |
| 12 | +struct Person { name: String } |
| 13 | + |
| 14 | +impl HasName for Person { |
| 15 | + fn name(&self) -> &str { |
| 16 | + &self.name |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +In the above example, we implement the `HasName` for the `Person` struct. |
| 22 | +The block `impl HasName for Person` is a _provider_ of the `HasName` trait |
| 23 | +for the `Person` context. |
| 24 | + |
| 25 | +Similar to the concept of a consumer, the use of provider is common in any |
| 26 | +Rust code that implements a trait. However, compared to cosumers, there |
| 27 | +are limitations on how providers can be defined in Rust. |
| 28 | + |
| 29 | +For this example, the `impl` block is a _context-specific_ provider for the |
| 30 | +`Person` context. Furthermore, due to the restrictions of Rust's trait system, |
| 31 | +there can be at most one provider of `HasName` for the `Person` context. |
| 32 | +Another common restriction is that the provider has to be defined in the same |
| 33 | +crate as either the trait or the context. |
| 34 | + |
| 35 | +The asymetry between what can be done with a provider, as compared to a consumer, |
| 36 | +is often a source of complexity in many Rust programs. As we will learn in later chapters, |
| 37 | +one of the goals of CGP is to break this asymetry, and make it easy to implement |
| 38 | +_context-generic providers_. |
| 39 | + |
| 40 | +## Providers as Consumers |
| 41 | + |
| 42 | +Although we have providers and consumers as distinct concepts, it is common to |
| 43 | +have code that serve as _both_ providers and consumers. |
| 44 | + |
| 45 | +```rust |
| 46 | +# trait HasName { |
| 47 | +# fn name(&self) -> &str; |
| 48 | +# } |
| 49 | +# |
| 50 | +# struct Person { name: String } |
| 51 | +# |
| 52 | +# impl HasName for Person { |
| 53 | +# fn name(&self) -> &str { |
| 54 | +# &self.name |
| 55 | +# } |
| 56 | +# } |
| 57 | +# |
| 58 | +trait CanGreet { |
| 59 | + fn greet(&self); |
| 60 | +} |
| 61 | + |
| 62 | +impl CanGreet for Person { |
| 63 | + fn greet(&self) { |
| 64 | + println!("Hello, {}!", self.name()); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +The example above shows a new `CanGreet` trait, which provides a `greet` method. |
| 70 | +We then implement `CanGreet` for `Person`, with the `greet` implementation using |
| 71 | +`self.name()` to print out the name to be greeted. |
| 72 | + |
| 73 | +Here, the block `impl CanGreet for Person` is a provider of `CanGreet` for the `Person` |
| 74 | +context. At the same time, it is also the _consumer_ of `HasName` for the `Person` context. |
| 75 | +In terms of genericity, the example code is _context-specific_ to the `Person` context for both |
| 76 | +the consumer and provider side. |
| 77 | + |
| 78 | +As we will see in later chapters, a powerful idea introduced by CGP is that a piece of code |
| 79 | +can have _multiple spectrums_ of genericity on the consumer and provider sides. |
0 commit comments