ComposeHandlers
Run two handlers back to back, feeding the output of the first as the input of the second.
Overview
Context-Generic Programming (CGP) enables pluggable trait implementations in Rust, and
ComposeHandlers<ProviderA, ProviderB> is the fundamental sequencing combinator for them. It runs
ProviderA, then runs ProviderB on ProviderA's output, under one
context, the type the implementation runs against, and one
Code tag. It implements the four owned-input members of the handler family, Computer,
TryComputer, AsyncComputer, and Handler, by threading the intermediate value through both
providers; it has no …Ref or Producer impl. Only the value flowing between the two changes type.
Like every CGP provider, it carries no runtime value; the two inner providers ride in PhantomData.
Usage
Import it from cgp::extra::handler; it is not in the prelude. It takes two provider type
parameters, the first handler and the second:
use cgp::extra::handler::ComposeHandlers;
delegate_components! {
App {
ComputerComponent: ComposeHandlers<Double, AddOne>,
}
}
The second provider's input type is pinned to the first's output type, so the two must line up:
ProviderB computes over ProviderA::Output, and the composite output is ProviderB::Output. For
composing more than two handlers, reach for PipeHandlers, which takes a list
and folds it into nested ComposeHandlers.
Examples
Two computers composed and wired as one:
use cgp::extra::handler::{Computer, ComputerComponent};
use cgp::prelude::*;
use cgp::extra::handler::{CanCompute, ComposeHandlers};
#[cgp_computer]
pub fn double(value: u64) -> u64 {
value * 2
}
#[cgp_computer]
pub fn add_one(value: u64) -> u64 {
value + 1
}
pub struct App;
delegate_components! {
App {
ComputerComponent: ComposeHandlers<Double, AddOne>,
}
}
check_components! {
App {
ComputerComponent: ((), u64),
}
}
pub fn demo() {
assert_eq!(App.compute(PhantomData::<()>, 5), 11); // (5 * 2) + 1
}
Computing over 5 runs Double first and AddOne on its output. App is an
environmental context, and the same composite
serves as a TryComputer, AsyncComputer, or Handler wherever both stages support that shape.
When to use it
Reach for ComposeHandlers to sequence exactly two handlers. For three or more, use
PipeHandlers, which reads better as a list and folds to the same nested
composition. For stages whose output carries the branch to stop on, such as an Err value or the
first success, use PipeMonadic, which runs the next stage only on the
continue branch.
Under the hood
ComposeHandlers carries the two inner providers in PhantomData:
pub struct ComposeHandlers<ProviderA, ProviderB>(pub PhantomData<(ProviderA, ProviderB)>);
Its Computer impl requires ProviderA: Computer<Context, Code, Input> and
ProviderB: Computer<Context, Code, ProviderA::Output>, and sets the composite Output to
ProviderB::Output:
#[cgp_provider]
impl<Context, Code, Input, ProviderA, ProviderB> Computer<Context, Code, Input>
for ComposeHandlers<ProviderA, ProviderB>
where
ProviderA: Computer<Context, Code, Input>,
ProviderB: Computer<Context, Code, ProviderA::Output>,
{
type Output = ProviderB::Output;
fn compute(context: &Context, code: PhantomData<Code>, input: Input) -> Self::Output {
let intermediary = ProviderA::compute(context, code, input);
ProviderB::compute(context, code, intermediary)
}
}
The same shape is implemented for TryComputer, AsyncComputer, and Handler. The fallible
variants use ? to short-circuit on the first provider's error, so they require the context to have
an error type; the async variants .await each step.
Related constructs
PipeHandlers— generalizes this to a list, folding to nestedComposeHandlers.ReturnInput— the identity that composes with any handler without changing it.Computer,Handler— the family it implements.PipeMonadic— composition that short-circuits through a monad.
The ideas behind it:
- Handlers — the computation family these combinators build.
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.