PromoteAsyncComputer
Fill in the async members of the handler family from a provider that implements AsyncComputer.
Generated machinery
You are not expected to name PromoteAsyncComputer directly.
#[cgp_computer] wires the provider it generates for an async fn
returning a plain value to PromoteAsyncComputer<Self>. A context names the bundle to route its own
components through such a provider, or through its HandlerComponent entry, which serves any
AsyncComputer. This page explains what the bundle emits.
Overview
Context-Generic Programming (CGP) lets one provider answer several related trait calls
through wiring, and PromoteAsyncComputer<Provider> starts from a provider that implements
AsyncComputer, the asynchronous infallible base. It fills in the other async members of the
handler family on a context, the type the implementation
runs against. It is the async-base counterpart of PromoteComputer, and it
fills in only async members, because the synchronous ones cannot be derived from an async base. 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 AsyncComputer provider. It answers HandlerComponent, AsyncComputerRefComponent, and
HandlerRefComponent but not AsyncComputerComponent, so the base is wired to
AsyncComputerComponent directly and the bundle fills in the rest:
delegate_components! {
App {
AsyncComputerComponent: DoubleLater,
HandlerComponent: PromoteAsyncComputer<DoubleLater>,
}
}
Examples
An async #[cgp_computer] function is the base, and the bundle answers handle from it:
use cgp::extra::handler::{CanComputeAsync, CanHandle, PromoteAsyncComputer};
use cgp::extra::handler::{AsyncComputer, AsyncComputerComponent, Handler, HandlerComponent};
use cgp::prelude::*;
use cgp::core::error::ErrorTypeProviderComponent;
#[cgp_computer]
pub async fn double_later(value: u64) -> u64 {
value * 2
}
pub struct App;
delegate_components! {
App {
ErrorTypeProviderComponent: UseType<String>,
AsyncComputerComponent: DoubleLater,
HandlerComponent: PromoteAsyncComputer<DoubleLater>,
}
}
check_components! {
App {
[AsyncComputerComponent, HandlerComponent]: ((), u64),
}
}
pub async fn demo() {
let code = PhantomData::<()>;
assert_eq!(App.compute_async(code, 5).await, 10);
assert_eq!(App.handle(code, 5).await, Ok(10));
}
handle wraps the awaited 10 in Ok, and needs the error type App wires. The
HandlerComponent entry is one step from the base, so it serves a hand-written AsyncComputer too.
App is an environmental context.
When to use it
Reach for PromoteAsyncComputer when the base is an infallible AsyncComputer, generated from
an async fn or written by hand. For a synchronous base use
PromoteComputer, and for an async base returning Result use
PromoteHandler.
Under the hood
PromoteAsyncComputer is a delegate_components! table over
a generic inner Provider:
delegate_components! {
<Provider>
new PromoteAsyncComputer<Provider> {
AsyncComputerRefComponent: PromoteRef<Provider>,
HandlerComponent: Promote<Provider>,
HandlerRefComponent: PromoteRef<Provider>,
}
}
It routes HandlerComponent to Promote, which wraps the awaited value in Ok, and
the two by-reference members to PromoteRef, which passes the borrow to an owned
member as its input. AsyncComputerRefComponent asks that of the base, which must accept the
borrow. HandlerRefComponent asks it of the base's Handler, a sibling entry, so it serves only a
base wired to the bundle.
Related constructs
PromoteComputer— the synchronous-base counterpart.PromoteHandler— the counterpart for an async base returningResult, which forwards its by-reference members here.Promote,PromoteRef— the lifts this table wires.AsyncComputer,Handler— the base and the family members it fills in.
The ideas behind it:
- Handlers — the family and its sync-to-async ordering.
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.