Skip to main content

Crate protocontract

Crate protocontract 

Source
Expand description

Runtime for the code that protoc-gen-protocontract-buffa generates.

For each protobuf service, the plugin emits:

  • <Service>Sync: each method returns Result<Reply, Error> when the call is done. Streams are BoxIters. The trait is dyn-compatible, so an implementation chosen at run time is held as Arc<dyn GreeterSync>.
  • <Service>Async: each method returns impl Future of the same result, so an implementation writes plain async fn. Streams are Streams. Generic callers (impl GreeterAsync) pay no allocation.
  • Dyn<Service>Async: a cloneable handle that holds any <Service>Async implementation 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 streamingResult<BoxIter<'static, Result<Reply, Error>>, Error>Result<impl Stream<Item = Result<Reply, Error>>, Error>
client streamingtakes BoxIter<'static, Result<Request, Error>>takes impl Stream<Item = Result<Request, Error>>
bidirectionalbothboth

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:

HaveNeedWrapperHow
sync implasync formInlineruns the sync call inside poll; for calls of microseconds
sync implasync formOffload (feature tokio)runs the sync call on tokio’s blocking pool
async implsync formBlocking (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 an enum next to its service; RuntimeCode holds the runtime’s own.
  • Retryable. Whether the same call may succeed later; false unless the fault says otherwise.
  • Message, source, backtrace. Display, std::error::Error::source, and Error::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.
ChannelStream
A Stream of the items another thread sends: the async side of a stream that Offload or Blocking moves between threads.
Code
An error code of any ErrorCode type, 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.
IterStream
A Stream over an iterator: each poll_next takes 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§

RuntimeCode
The failures the runtime itself reports, outside any contract.

Traits§

Context
Adds a stack frame to the error of a Result on its way out.
ErrorCode
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>Async handle returns: boxed, Send, and borrowing for at most 'a.
BoxIter
A stream of a sync streaming method: a boxed Send iterator that borrows for at most 'a.
BoxStream
A stream of an async streaming method called through a Dyn<Service>Async handle: boxed, pinned, and Send.