Monad providers
Turn a list of handlers and a choice of monad into a single handler that short-circuits on the appropriate branch.
Overview
Context-Generic Programming (CGP) enables pluggable trait implementations in Rust, and the
monad providers build monadic handler composition on top of the
Computer family. They let a sequence of handlers whose
outputs carry a "continue" case and a "stop" case be chained without pattern-matching each step by
hand: the monad decides which case threads forward and which short-circuits. A built pipeline is
itself a provider for Computer, AsyncComputer, TryComputer, and Handler, so it wires into a
context, the type the implementation runs against, exactly
like any other handler. None of the names on these pages is in the prelude: PipeMonadic is
imported from cgp::extra::monad::providers, and the markers and bind providers from
cgp::extra::monad::monadic::{ident, ok, err}.
The providers divide into three groups:
PipeMonadicis the pipeline builder, the provider a user wires or invokes.- The monad markers below are the zero-sized types that select the short-circuiting behavior.
BindOkandBindErrare the per-step bind providers thatPipeMonadiccomposes internally, and that can also be used directly.
The monad markers
A monad marker selects which branch of a step's output continues the pipeline and which stops it. CGP
defines three base markers and a transformer form for the two that branch on Result. They are not
providers, so they have no page of their own; they are the M argument the providers above take.
IdentMonadic is the identity monad. It threads every value forward and never short-circuits, so a
PipeMonadic<IdentMonadic, …> is equivalent to plain composition with
PipeHandlers.
OkMonadic short-circuits on Ok and continues on Err. It is the mirror behavior, running until
something succeeds, which suits a fallback chain.
ErrMonadic short-circuits on Err and continues on Ok. This is the ordinary early-return-on-error
behavior, the same as Rust's ? operator.
The naming reads the opposite of the behavior, and getting it wrong produces a pipeline that runs
exactly when you expected it to stop: OkMonadic continues on Err, and ErrMonadic continues on
Ok. ErrMonadic is the one that behaves like ?.
OkMonadicTrans<M> and ErrMonadicTrans<M> are transformer forms that apply the same behavior
on top of a base monad M, so monads can stack over nested result types. In
OkMonadicTrans<ErrMonadic>, the err monad handles the outer Result of each output and the ok layer
the inner one, so over Result<Result<T, E1>, E2> it stops on an outer Err or an inner Ok and
continues with the E1 of an Ok(Err(e1)). A single layer of branching needs no explicit
transformer: used on its own, each bare marker binds as its transformer over IdentMonadic. The trait layer that gives a marker its meaning is documented under
MonadicBind and MonadicTrans.
Related constructs
PipeMonadic— the pipeline builder that consumes a marker and a handler list.BindOk,BindErr— the per-step bind providers.PipeHandlers— non-monadic composition, whichPipeMonadic<IdentMonadic, …>reduces to.MonadicBind,MonadicTrans,ContainsValue,LiftValue— the four traits a marker implements.Product!— the type-level list a pipeline's steps are given in.
The ideas behind it:
- Monadic handlers — why a pipeline short-circuits and how the monads compose.
Source
- The pipeline provider is in
providers/pipe_monadic.rs, and the markers and bind providers inmonadic/.
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.