Skip to main content

PromoteComputer

Fill in every member of the handler family from a provider that implements Computer.

info

Generated machinery​

You are not expected to name PromoteComputer directly. #[cgp_computer] wires the provider it generates to PromoteComputer<Self>, so one written function answers the whole handler surface. A context names the bundle only to route its own components through such a provider, or through the entries that serve any Computer, 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 PromoteComputer<Provider> starts from a provider that implements Computer, the synchronous, infallible, owned-input base, and fills in every other member of the handler family by promotion, on a context, the type the implementation runs against. It is a delegation table that routes each remaining handler component to the right single-step promotion. 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 Computer provider. PromoteComputer answers every family member except ComputerComponent, so the base is wired to ComputerComponent directly and the bundle fills in the rest:

delegate_components! {
App {
ComputerComponent: Double,
[
TryComputerComponent,
AsyncComputerComponent,
HandlerComponent,
]: PromoteComputer<Double>,
}
}

Here Double comes from #[cgp_computer], which is what lets the HandlerComponent entry resolve.

Examples​

One #[cgp_computer] function answers four members of the family through the bundle:

use cgp::extra::handler::{CanCompute, CanComputeAsync, CanHandle, CanTryCompute, PromoteComputer};
use cgp::extra::handler::{
AsyncComputer, AsyncComputerComponent, Computer, ComputerComponent, Handler, HandlerComponent,
TryComputer, TryComputerComponent,
};
use cgp::prelude::*;
use cgp::core::error::ErrorTypeProviderComponent;

#[cgp_computer]
pub fn double(value: u64) -> u64 {
value * 2
}

pub struct App;

delegate_components! {
App {
ErrorTypeProviderComponent: UseType<String>,
ComputerComponent: Double,
[
TryComputerComponent,
AsyncComputerComponent,
HandlerComponent,
]: PromoteComputer<Double>,
}
}

check_components! {
App {
[
ComputerComponent,
TryComputerComponent,
AsyncComputerComponent,
HandlerComponent,
]: ((), u64),
}
}

pub async fn demo() {
let code = PhantomData::<()>;

assert_eq!(App.compute(code, 5), 10);
assert_eq!(App.try_compute(code, 5), Ok(10));
assert_eq!(App.compute_async(code, 5).await, 10);
assert_eq!(App.handle(code, 5).await, Ok(10));
}

The fallible members wrap 10 in Ok, and need the error type App wires. App is an environmental context.

When to use it​

Reach for PromoteComputer when a context routes its handler components through a Computer provider generated by #[cgp_computer]. For a hand-written base, use the one-step entries the bundle offers, or spell the lift out with the single-step combinators. Use a different bundle when the base is a different trait: PromoteTryComputer from a Computer returning Result, PromoteProducer from a Producer, PromoteAsyncComputer from an AsyncComputer, and PromoteHandler from an AsyncComputer returning Result.

Under the hood​

PromoteComputer is defined with delegate_components! over a generic inner Provider. It routes the fallible slot to Promote (wrap in Ok), the async slots to PromoteAsync (run synchronously in an async method), and every …Ref slot to PromoteRef (pass the borrow to the base as its input):

delegate_components! {
<Provider>
new PromoteComputer<Provider> {
ComputerRefComponent: PromoteRef<Provider>,
TryComputerComponent: Promote<Provider>,
TryComputerRefComponent: PromoteRef<Provider>,
AsyncComputerComponent: PromoteAsync<Provider>,
AsyncComputerRefComponent: PromoteRef<Provider>,
HandlerComponent: PromoteAsync<Provider>,
HandlerRefComponent: PromoteRef<Provider>,
}
}

The base ComputerComponent is the inner provider itself; the table fills in the other seven. Four of the entries take their step from a sibling rather than from the base: HandlerComponent is PromoteAsync<Provider>, whose Handler impl needs Provider to be a TryComputer, and the TryComputerRef, AsyncComputerRef, and HandlerRef entries need Provider to answer the matching owned member over a borrowed input. ComputerRefComponent takes its step from the base, which must accept the borrow as its input.

Common Mistakes​

Only the one-step entries serve a hand-written Computer. TryComputerComponent and AsyncComputerComponent need only the base, so PromoteComputer<Double> answers them for any Computer, and ComputerRefComponent for any Computer whose input is a borrow. HandlerComponent looks for a TryComputer on Double itself, which a hand-written provider not wired to the bundle lacks, so the check fails:

error[E0277]: the trait bound `Double: DelegateComponent<TryComputerComponent>` is not satisfied
...
= note: required for `Double` to implement `TryComputer<App, (), u64>`
= note: required for `PromoteAsync<Double>` to implement `IsProviderFor<HandlerComponent, App, ((), u64)>`

Spell the two steps out as PromoteAsync<Promote<Double>>, or write the base with #[cgp_computer], which wires it to PromoteComputer<Self>.

The ideas behind it:

  • Handlers — the family and why one written trait can answer all of it.

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.