protocontract-buffa
Plain Rust traits for protobuf services.
protoc-gen-protocontract-buffa turns each protobuf service into Rust traits
over the message structs buffa
generates. A component in the same process is called by a method call: no
encoding, no transport, no view types. A method can also take or give an
implementation of another service, in a field, a request, or a reply
(References to a service). The runtime crate
protocontract holds the types the generated code names. The runtime and the generated code need
Rust 1.92 or newer; building the plugin from source needs Rust 1.100.0 (the
release binaries need no Rust at all).
What is generated
service Greeter {
// Greet a person by name.
rpc Greet(Person) returns (Greeting);
}
A contract that stays in one process reads best named the way the Rust
code reads it: the service as the trait (Greeter, not GreeterService),
the methods over the domain's own messages (Person, Greeting), not
…Request and …Response wrappers. buf's STANDARD lint asks for the RPC
style; relax SERVICE_SUFFIX, RPC_REQUEST_STANDARD_NAME,
RPC_RESPONSE_STANDARD_NAME, and RPC_REQUEST_RESPONSE_UNIQUE for such
protos. The plugin accepts either style.
For each service, three items:
/// The blocking form. Dyn-compatible: `Arc<dyn GreeterSync>`.
/// The async form. Implementations write plain `async fn`; a generic
/// caller (`impl GreeterAsync`) pays no allocation.
/// Any `GreeterAsync` behind dynamic dispatch, for an implementation
/// chosen at run time. It implements `GreeterAsync` itself; each call
/// boxes its future. Cloning shares the implementation; it also
/// implements `Debug`.
Every method takes its request by value and returns its reply owned; the
error is protocontract::Error (see Errors). The parameter is
named after its message in snake case (Person gives
person), plural for an inbound stream (Item gives items); an
implementation may name it as it likes. Comments in the .proto become
rustdoc.
Implement the form natural to the work: sync for computation, async for work that waits on I/O.
;
let greeter = new;
let reply = greeter.greet.await?;
Streaming
Methods of every kind are generated. The sync form takes and returns
protocontract::BoxIter, the async form protocontract::Stream; the Dyn…Async
handle boxes them as protocontract::BoxStream. Stream items are
Result<_, Error>.
service Feed {
rpc Watch(Limit) returns (stream Item); // server streaming
rpc Collect(stream Item) returns (Summary); // client streaming
rpc Echo(stream Item) returns (stream Item); // bidirectional
}
An Err before the first item fails the call; an Err item ends the
stream. No stream borrows the service: an implementation clones what its
stream needs, and a stream can outlive the call and move between threads.
Errors
Every method of every service fails with one type, protocontract::Error. One
type, not one per service, because:
- an
Arc<dyn …Sync>or aDyn…Asynchandle holds implementations chosen at run time, which must agree on the error type; - the bridges fail on their own (a panic, a runtime that cannot block) and must build an error whatever the service;
?carries a failure from one contract into another without a conversion.
What a caller needs from a failure is said by the error itself, not by its type:
| Part | What it says | Read with |
|---|---|---|
| code | the class of the failure, to branch on | error.is(code), error.code_as::<C>(), error.code() |
| retryable | whether the same call may succeed later | error.retryable() |
| message | what happened, for a developer | Display |
| source | the error that caused it | std::error::Error::source |
| stack | the operations it passed on its way out, outermost first | error.stack() |
| backtrace | where it was made (with RUST_BACKTRACE set) |
error.backtrace() |
1. The contract declares its codes
A contract's failure codes are part of the contract: put them in the
proto, as an enum next to the service, named <Service>ErrorCode.
For service Greeter the enum is GreeterErrorCode:
// Greets people by name.
service Greeter {
rpc Greet(Person) returns (Greeting);
}
// The failures of Greeter a caller may branch on.
enum GreeterErrorCode {
GREETER_ERROR_CODE_UNSPECIFIED = 0;
// The name to greet is empty.
GREETER_ERROR_CODE_EMPTY_NAME = 1;
// The greeter cannot answer now; the same call may succeed later.
GREETER_ERROR_CODE_BUSY = 2;
// The greeter knows no such person.
GREETER_ERROR_CODE_NO_SUCH_PERSON = 3;
}
Every implementation of the service fails with these codes, and every caller branches on them. A code that is not in the enum means nothing to a caller who knows only the contract.
2. The plugin links the service to its codes
The types cannot tie a service to its enum (the error type is shared by all services), so the plugin ties them by name. It looks in the service's package for a top-level enum named after the service, and names it in both traits' rustdoc:
/// Blocking form of `example.v1.Greeter`: … It fails with the codes of
/// `GreeterErrorCode` (`example.v1.GreeterErrorCode`): branch on them with
/// `Error::is` or `Error::code_as`.
So whoever holds a GreeterSync finds its codes in the trait's
documentation. A service without such an enum gets no mention, and is
generated as before.
A service named in the RPC style, GreeterService, finds its codes too:
the plugin looks for GreeterErrorCode first, then
GreeterServiceErrorCode. When the enum is GreeterErrorCode, it also
emits an alias named after the service, so the name still follows from the
trait (GreeterServiceSync → GreeterServiceErrorCode):
pub type GreeterServiceErrorCode = crateGreeterErrorCode;
The alias and the enum are one type; use either. A message or enum that already has the alias' name fails the run.
buffa gives each enum value a short constant beside its proto name:
GreeterErrorCode::EmptyName is GREETER_ERROR_CODE_EMPTY_NAME.
3. An implementation fails with a code
The common case is a code and a message:
use ;
Failure adds the rest, when there is more to say:
let backend = self.backend.lookup.map_err?;
A failure with fields of its own (a path, a limit, the closest match)
implements protocontract::Fault on its own type; ? turns it into an
Error:
4. A caller branches on the code
use Context;
match greeter.greet
or, for every code of the contract at once:
match error.
A code is compared together with its type. GreeterErrorCode value 1 and
FeedErrorCode value 1 are different codes: is is false and
code_as returns None for a code of another enum. A failure whose code
the caller does not know falls through to its last branch; it still
carries its message, source, and stack.
On the way out, context adds an operation to the error's stack, and
Display prints the stack before the code:
let reply = greeter.greet.context?;
// welcome: GREETER_ERROR_CODE_EMPTY_NAME: name is empty
Context works on any Result whose error converts into Error
(.context("…"), or .with_context(|| format!(…)) to build the frame only
on failure). error.downcast_ref::<NoSuchPerson>() reaches an
implementation's own fault type; use it for diagnostics only, since a
caller that branches on it is bound to that implementation.
Codes outside the contract
protocontract::RuntimeCodeholds the runtime's own codes:RuntimeCode::Panicked(a panic underOffload),RuntimeCode::CannotBlock(Blockinginside a current-thread runtime),RuntimeCode::Cancelled(a blocking call dropped by its runtime),RuntimeCode::Unbound(a reference to a service that holds no implementation), andRuntimeCode::NoRuntime(an async implementation asked for in the blocking form where no tokio runtime can run it).Displayprints them asPANICKED,CANNOT_BLOCK,CANCELLED,UNBOUND, andNO_RUNTIME.- A component may have codes of its own, outside any contract: a Rust enum
that implements
protocontract::ErrorCode(to_i32,from_i32,name) works like a protobuf enum.
What the compiler does not check
That an implementation fails only with its contract's codes. Any enum is a valid code, so a provider can return a code its callers do not know; a caller then sees an unknown code and takes its fallback branch. Keep each implementation to its contract's enum, and let its tests check the codes it returns.
Bridges
Each form is also implemented for wrappers around the other, so a caller of one form can use an implementation of the other:
| Wrapper | Gives | How |
|---|---|---|
protocontract::Inline<T> |
a sync impl the async form | runs the call inside poll; for calls of microseconds |
protocontract::Offload<T> |
a sync impl the async form | runs the call on tokio's blocking pool, about 20 µs a call |
protocontract::Blocking<T> |
an async impl the sync form | blocks the caller on a stored tokio runtime handle |
// A sync implementation, called through the async form.
let service = new;
// The same, back in the sync form.
let sync = new;
- Every wrapper covers every kind of method, and each fits in a
Dyn…Asynchandle or anArc<dyn …Sync>. Inlinereads an inbound stream to its end before the sync call, so a bidirectional call through it answers only after the last request.OffloadandBlockinganswer each request as it arrives.Blockingworks from any thread outside a runtime and inside a multi-thread runtime; inside a current-thread runtime it fails withRuntimeCode::CannotBlockrather than block that runtime's only thread.Offloadturns a panic of the sync implementation intoRuntimeCode::Panicked.
References to a service
Protobuf has data types only: a message cannot hold an interface. So a method that takes or gives an implementation (a port a host hands a component, an object a factory returns) is written with a reference message: an empty message that names the service it stands for.
import "protocontract/v1/options.proto";
// Tells the time.
service Clock {
rpc Now(google.protobuf.Empty) returns (Tick);
}
// A clock to read the time from.
message ClockRef {
option (protocontract.v1.service) = "example.v1.Clock";
}
message Alarm {
string label = 1;
ClockRef clock = 2; // data and an implementation in one message
repeated ClockRef backups = 3;
}
service Alarms {
rpc UseClock(ClockRef) returns (google.protobuf.Empty);
rpc CurrentClock(google.protobuf.Empty) returns (ClockRef);
}
For each reference message the plugin emits a type in a module named after it, next to the service's traits:
Map the message to that type with the same extern_path in both plugins;
every field, request, and reply of ClockRef then holds it:
- local: protoc-gen-buffa
opt:
- local: protoc-gen-protocontract-buffa
opt:
// The caller hands over an implementation in its own form.
alarms.use_clock?;
alarms.use_clock?;
// The receiver takes the form it needs.
let clock = clock_ref.to_sync?;
let tick = clock.now?;
-
Native when the forms match. A reference holds the implementation as it was given. Asked for that same form, it gives back the same implementation, with no wrapper. A bridge runs only when the forms differ, and only the one the receiver names:
to_synccalls an async implementation throughBlockingon the tokio runtime current at the call;to_async_inlineandto_async_offloadcall a blocking one throughInline(insidepoll, for calls of microseconds) orOffload(on tokio's blocking pool). See Bridges. -
One process. A reference encodes as an empty message. Decoded, and as the default, it holds no implementation, and taking a form fails with
RuntimeCode::Unbound. It has no JSON form: serializing one fails, and so does reading one; a message without it reads and writes as before. -
Equality is identity: two references are equal when they hold the same implementation.
-
No data. A reference message declares no field, oneof, nested type, or extension; the run fails if it does. It is a top-level message, and it names a service of its own package. Its type sits in its own module (
clock_ref), which the run checks is free. -
With buffa's
json=true, passjson=trueto this plugin too, and depend onserde. -
With protovalidate.
protoc-gen-protovalidate-buffawrites avalidate()for every message, and it callsvalidate()on each message field, a reference included. Passvalidate=trueto this plugin next to it: the reference type then implementsValidate(fromprotovalidate-buffa) and is always valid, since it carries no data. Rules on the field that holds a reference apply as to any message field:import "buf/validate/validate.proto"; message Alarm { string label = 1 [(buf.validate.field) = { required: true string: {min_len: 1} }]; ClockRef clock = 2 [(buf.validate.field).required = true]; repeated ClockRef backups = 3 [(buf.validate.field).repeated.max_items = 3]; }use Validate; alarm.validate.map_err?;A rule sees the field, not the implementation:
requiredis met by any reference that is set, bound or not, and an unbound one still fails withUNBOUNDwhen a form is taken. Pair the plugin with the runtime it generates for (protoc-gen-protovalidate-buffa0.11 withprotovalidate-buffa0.8, as this repository does); without protovalidate, leavevalidateoff and the generated code does not depend on it.
options.proto ships inside the protocontract crate, under proto/
(proto/protocontract/v1/options.proto), so whatever has the crate has
the file. Add that directory to the buf module that compiles your
contract, next to your own protos.
examples/refs
shows all of it in use, protovalidate's rules included;
examples/glossary
hands a provider to its consumer through a reference, from a catalog
service (Glossaries) that keeps glossaries by topic.
Example
examples/glossary is a whole contract in use: the
proto, the generated crate, a provider that implements it, a consumer that
calls it without knowing the provider, and the application that wires the
two. cargo run -p glossary-app runs it.
examples/refs is a contract whose messages and methods carry
references to a service; cargo test -p refs-api runs it.
Use
Get the plugin from the
Releases:
static binaries for Linux, macOS, and Windows on amd64 and arm64, named
protoc-gen-protocontract-buffa-<tag>-<os>-<arch>, with their sha256 in
checksums-sha256.txt. Releases up to v0.2.2 name the plugin
protoc-gen-contract-rust. Or build it from crates.io:
Run it next to protoc-gen-buffa, each into its own out directory:
version: v2
plugins:
- local: protoc-gen-buffa
out: src/generated/buffa
strategy: all
- local: protoc-gen-buffa-packaging
out: src/generated/buffa
strategy: all
- local: protoc-gen-protocontract-buffa
out: src/generated/contract
strategy: all
opt:
- local: protoc-gen-buffa-packaging
out: src/generated/contract
strategy: all
opt:
Mount both trees. Name the traits' module anything but protocontract:
next to the crate protocontract the path protocontract::… would be
ambiguous.
The crate depends on the runtime with its tokio feature, since the
generated code implements the traits for Offload and Blocking, and on
what buffa's output needs (buffa, and buffa-types for well-known
types):
[]
= { = "0.4", = ["tokio"] }
To keep tokio optional, pass gate_tokio_feature to the plugin: the
Offload and Blocking impls then sit behind a tokio feature of your
crate, which turns on the runtime's:
[]
= "0.4"
[]
= ["protocontract/tokio"]
protoc-gen-connect-rust may run on the same protos into the same crate,
even into the same modules: the plugin takes no name and no file that
connect-rust takes, and the run fails if a generated name meets an
existing one, naming both proto elements.
Parameters
| Parameter | Meaning |
|---|---|
buffa_module=<path> |
where the buffa output is mounted; shorthand for extern_path=.=<path> |
extern_path=<proto>=<path> |
maps a proto package prefix to a Rust module; repeatable, longest prefix wins |
file_per_package |
one <dotted.pkg>.rs per package instead of per-proto files and a stitcher; drop the packaging step and mount each file yourself |
element_memory_limit=<bytes|unlimited> |
decode bound of the request, as for protoc-gen-buffa |
runtime=<path> |
path of the runtime crate; default ::protocontract |
gate_tokio_feature[=<name>] |
Offload and Blocking impls under #[cfg(feature = "<name>")]; default tokio |
json=<true|false> |
buffa runs with json=true: reference types get serde impls too |
validate=<true|false> |
protoc-gen-protovalidate-buffa runs too: reference types implement Validate |
Paths are absolute (::…, crate, or crate::…). Well-known types map
to ::buffa_types on their own. Any other parameter fails the run.
Status
Before 1.0, a minor version may break the generated code or the runtime; CHANGELOG.md says what changed and what a consumer changes. The design, its stages, and its decisions are in DESIGN.md.
The traits are for calls inside one process. Across processes, use
connect-rust's stubs directly. Validation is not part of the plugin: run
protoc-gen-protovalidate-buffa next to it, and call validate() on the
owned messages where you decide to.
Build
mise installs buf, the protoc plugins next to this
one, cargo-deny, and markdownlint at the versions in mise.toml, checked
against mise.lock; Rust comes from rust-toolchain.toml through rustup.
The release binary of the plugin, as the release workflow builds it:
The dist profile sets the optimisation (fat LTO, stripped symbols); the
target makes the binary static, with the flags in .cargo/config.toml
(RUSTFLAGS would replace them, so it is unset):
| Platform | Target | Linking |
|---|---|---|
| Linux | x86_64-unknown-linux-musl, aarch64-unknown-linux-musl |
fully static: musl libc linked in (a static glibc is not supported) |
| Windows | x86_64-pc-windows-msvc, aarch64-pc-windows-msvc |
C runtime linked in (+crt-static): no Visual C++ redistributable |
| macOS | x86_64-apple-darwin, aarch64-apple-darwin |
system libraries only (Apple supports no static binary) |
An annotated version tag vX.Y.Z on main runs the release workflow:
the gate and the plugin's static binary on Linux, macOS, and
Windows, each on amd64 and arm64; the runtime and the generated code built
and tested with the minimum Rust version; then a GitHub Release with the
six binaries and checksums-sha256.txt. The tag's message is the release
notes.
License
Apache-2.0.