Expand description
Runtime for the code that protoc-gen-protocontract-buffa generates.
For each protobuf service, the plugin emits:
<Service>Sync: each method returnsResult<Reply, Error>when the call is done. Streams areBoxIters. The trait is dyn-compatible, so an implementation chosen at run time is held asArc<dyn GreeterSync>.<Service>Async: each method returnsimpl Futureof the same result, so an implementation writes plainasync fn. Streams areStreams. Generic callers (impl GreeterAsync) pay no allocation.Dyn<Service>Async: a cloneable handle that holds any<Service>Asyncimplementation behind dynamic dispatch and implements the trait itself. Each call through it boxes the future (BoxFuture) and the reply stream (BoxStream).
Every method takes its request by value and returns its reply owned; a call in the same process is a plain method call, with no encoding and no transport.
Streaming methods, per form:
| Method | <Service>Sync | <Service>Async |
|---|---|---|
| server streaming | Result<BoxIter<'static, Result<Reply, Error>>, Error> | Result<impl Stream<Item = Result<Reply, Error>>, Error> |
| client streaming | takes BoxIter<'static, Result<Request, Error>> | takes impl Stream<Item = Result<Request, Error>> |
| bidirectional | both | both |
An Err before the first item fails the whole call; an Err item ends
a stream with that failure. Inbound items are Results because a stream
that crosses a process boundary can break, and the implementation must
see that rather than a stream that merely ends early. No stream borrows
the service, so a stream can outlive the call and move between threads.
§References to a service
An empty message marked option (protocontract.v1.service) = "pkg.Service"; stands for an implementation of that service: the
plugin emits a type for it that a field, a request, or a reply carries,
holding the implementation in the form it was given. The option is
declared in proto/protocontract/v1/options.proto, which ships in this
crate. A reference does not survive encoding: taking a form of a
decoded one fails with RuntimeCode::Unbound, and asking for an
async implementation in the blocking form where no tokio runtime can run
it fails with RuntimeCode::NoRuntime. The generated code uses the
hidden module reference; nothing there is named by hand.
§Bridges
The plugin also implements each form for wrappers around the other:
| Have | Need | Wrapper | How |
|---|---|---|---|
| sync impl | async form | Inline | runs the sync call inside poll; for calls of microseconds |
| sync impl | async form | Offload (feature tokio) | runs the sync call on tokio’s blocking pool |
| async impl | sync form | Blocking (feature tokio) | blocks the calling thread on a stored runtime handle |
Each wrapper implements the form itself, so it fits in a
Dyn<Service>Async handle or an Arc<dyn <Service>Sync>.
§Errors
Every method fails with one type, Error, whatever the service: an
implementation chosen at run time, a bridge, and a broken stream all
produce it, and ? carries it across contracts. An Error holds a
Fault and the stack of operations it passed (Context).
- Code. The class of the failure, which a caller branches on. Codes
are open: any
ErrorCode, which every protobuf enum is. A contract declares the codes its callers may rely on as anenumnext to itsservice;RuntimeCodeholds the runtime’s own. - Retryable. Whether the same call may succeed later;
falseunless the fault says otherwise. - Message, source, backtrace.
Display,std::error::Error::source, andError::backtrace.
Failure is the ready-made fault; an implementation with more to say
implements Fault on a type of its own.
Structs§
- Blocking
- The sync form of an async implementation: each call blocks the calling thread until the async method, run on the runtime behind the stored handle, is done.
- Channel
Stream - A
Streamof the items another thread sends: the async side of a stream thatOffloadorBlockingmoves between threads. - Code
- An error code of any
ErrorCodetype, with that type erased. - Error
- The failure of a call, as every generated method returns it.
- Failure
- The ready-made
Fault: a code, a message, whether to retry, an optional cause, and a backtrace. - Inline
- The async form of a sync implementation, without a thread hop: each call
runs the sync method inside the future’s
poll. - Iter
Stream - A
Streamover an iterator: eachpoll_nexttakes the iterator’s next item in place, without waiting. - Offload
- The async form of a sync implementation: each call runs the sync method on tokio’s blocking pool, so the async side never blocks.
Enums§
- Runtime
Code - The failures the runtime itself reports, outside any contract.
Traits§
- Context
- Adds a stack frame to the error of a
Resulton its way out. - Error
Code - A type whose values are error codes.
- Fault
- A failure an implementation reports: what a type must say to travel as
an
Error. - Stream
- A stream of values produced asynchronously.
Type Aliases§
- BoxFuture
- The future a call through a
Dyn<Service>Asynchandle returns: boxed,Send, and borrowing for at most'a. - BoxIter
- A stream of a sync streaming method: a boxed
Senditerator that borrows for at most'a. - BoxStream
- A stream of an async streaming method called through a
Dyn<Service>Asynchandle: boxed, pinned, andSend.