PromoteProducer
Fill in the handler family from a provider that implements Producer.
Generated machinery
You are not expected to name PromoteProducer directly.
#[cgp_producer] wires the provider it generates to
PromoteProducer<Self>. A context names the bundle only to route its own components through such a
provider, or through the one entry that serves any producer, as Common Mistakes
sets out. This page explains what the bundle emits.
Overview
Context-Generic Programming (CGP) lets one provider answer several related trait calls
through wiring, and PromoteProducer<Provider> starts from a
Producer, a provider that takes no input. It fills in
every input-taking member of the handler family on a
context, the type the implementation runs against. The
single produced value flows out of every handler shape regardless of the input, which the promotion
discards. Like every CGP provider, it carries no runtime value.
Usage
It is not in the prelude; import it from cgp::extra::handler. It takes one type parameter, the
base Producer provider. The base answers ProducerComponent, and PromoteProducer answers
ComputerComponent and the rest of the family by discarding the input and calling the producer:
delegate_components! {
App {
ProducerComponent: DefaultPort,
[ComputerComponent, TryComputerComponent]: PromoteProducer<DefaultPort>,
}
}
Examples
A #[cgp_producer] function answers the producer and two input-taking members:
use cgp::extra::handler::{CanCompute, CanProduce, CanTryCompute, PromoteProducer};
use cgp::extra::handler::{
Computer, ComputerComponent, Producer, ProducerComponent, TryComputer, TryComputerComponent,
};
use cgp::prelude::*;
use cgp::core::error::ErrorTypeProviderComponent;
#[cgp_producer]
pub fn default_port() -> u16 {
8080
}
pub struct App;
delegate_components! {
App {
ErrorTypeProviderComponent: UseType<String>,
ProducerComponent: DefaultPort,
[ComputerComponent, TryComputerComponent]: PromoteProducer<DefaultPort>,
}
}
check_components! {
App {
ProducerComponent: (),
[ComputerComponent, TryComputerComponent]: ((), u64),
}
}
pub fn demo() {
let code = PhantomData::<()>;
assert_eq!(App.produce(code), 8080);
assert_eq!(App.compute(code, 999_u64), 8080);
assert_eq!(App.try_compute(code, 999_u64), Ok(8080));
}
Every input-taking member ignores the 999 it is given and returns the produced 8080. App is an
environmental context.
When to use it
Reach for PromoteProducer when a context routes input-taking components through a producer,
usually one generated by #[cgp_producer]. For an input-taking base
use PromoteComputer, PromoteTryComputer,
PromoteAsyncComputer, or PromoteHandler.
Under the hood
PromoteProducer is a delegate_components! table over a
generic inner Provider:
delegate_components! {
<Provider>
new PromoteProducer<Provider> {
ComputerComponent: Promote<Provider>,
[
ComputerRefComponent,
TryComputerComponent,
TryComputerRefComponent,
AsyncComputerComponent,
AsyncComputerRefComponent,
HandlerComponent,
HandlerRefComponent,
] ->
PromoteComputer<Provider>,
}
}
It routes ComputerComponent to Promote, which discards the computer's input and
calls the producer, and forwards every remaining key to PromoteComputer's
own entry, so each is derived from the producer's Computer form. Because that computer accepts an
input of any type, including a borrow, the …Ref members work too.
Common Mistakes
Only the ComputerComponent entry serves a hand-written producer. Every other entry goes
through PromoteComputer<P>, which looks for a Computer on the producer itself. A hand-written
producer not wired to the bundle has none, so wiring TryComputerComponent to
PromoteProducer<MagicNumber> fails at the check:
error[E0277]: the trait bound `MagicNumber: DelegateComponent<ComputerComponent>` is not satisfied
...
= note: required for `MagicNumber` to implement `Computer<App, (), ()>`
Write the producer with #[cgp_producer], or wire the hand-written producer to the bundle itself;
the Producer page shows both.
Related constructs
#[cgp_producer]— generates aProducerprovider and wires it toPromoteProducer.Promote— the lift that discards a computer's input and calls the producer.PromoteComputer— the bundle this forwards to.Producer,Computer— the base and the family it fills in.
The ideas behind it:
- Handlers — the family, including the input-free producer.
Source
An AI agent wrote this page using the CGP knowledge base. Its content was verified against the library's source. See How AI is used in this project.