Skip to main content

PromoteTryComputer

Fill in the handler family from a Computer whose output is a Result.

info

Generated machinery​

You are not expected to name PromoteTryComputer directly. #[cgp_computer] wires the provider it generates for a function returning Result to PromoteTryComputer<Self>. A context names the bundle only to route its own components through such a provider, or through the entries that serve any base, 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 PromoteTryComputer<Provider> starts from a Computer whose output is a Result over the context's error type, the base #[cgp_computer] generates for a function returning Result, and fills in the rest of the handler family on a context, the type the implementation runs against. It reads that Result as the failure path for the fallible slot through TryPromote, and derives the other members from the computer form through PromoteComputer. 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 provider, a Computer returning Result:

delegate_components! {
App {
[TryComputerComponent, HandlerComponent]: PromoteTryComputer<CheckedDouble>,
}
}

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

Examples​

A fallible #[cgp_computer] function answers the fallible members through the bundle:

use cgp::extra::handler::{CanHandle, CanTryCompute, PromoteTryComputer};
use cgp::extra::handler::{Handler, HandlerComponent, TryComputer, TryComputerComponent};
use cgp::prelude::*;
use cgp::core::error::ErrorTypeProviderComponent;

#[cgp_computer]
pub fn checked_double(value: u64) -> Result<u64, String> {
value.checked_mul(2).ok_or_else(|| "overflow".to_owned())
}

pub struct App;

delegate_components! {
App {
ErrorTypeProviderComponent: UseType<String>,
[TryComputerComponent, HandlerComponent]: PromoteTryComputer<CheckedDouble>,
}
}

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

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

assert_eq!(App.try_compute(code, 21), Ok(42));
assert_eq!(App.try_compute(code, u64::MAX), Err("overflow".to_owned()));
assert_eq!(App.handle(code, 21).await, Ok(42));
}

The function's error type is String, and it must equal App's, because TryPromote passes the Err through unconverted. App is an environmental context.

When to use it​

Reach for PromoteTryComputer when a context routes its handler components through a fallible #[cgp_computer] provider. For a plain Computer base use PromoteComputer; for the other bases use PromoteProducer, PromoteAsyncComputer, or PromoteHandler.

Under the hood​

PromoteTryComputer is a delegate_components! table over a generic inner Provider:

delegate_components! {
<Provider>
new PromoteTryComputer<Provider> {
TryComputerComponent: TryPromote<Provider>,
[
ComputerRefComponent,
TryComputerRefComponent,
AsyncComputerComponent,
AsyncComputerRefComponent,
HandlerComponent,
HandlerRefComponent,
] ->
PromoteComputer<Provider>,
}
}

It routes TryComputerComponent to TryPromote, which reads the base's Result output as the failure path, and forwards every remaining key to PromoteComputer's own entry for it, with the -> operator, so those members derive from the computer form.

Common Mistakes​

The HandlerComponent entry does not serve a hand-written base. TryComputerComponent and AsyncComputerComponent take one step from the base, through TryPromote<P> and PromoteAsync<P>, so they serve any Computer returning Result; the async entry keeps the Result as its output. HandlerComponent goes through PromoteComputer<P> to PromoteAsync<P>, which looks for a TryComputer on the base itself. A hand-written base not wired to the bundle fails at the check:

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

Write the base with #[cgp_computer], or wire the handler slot as PromoteAsync<TryPromote<CheckedDouble>>.

The ideas behind it:

  • Handlers — the family and its fallibility axis.

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.