Expand description
Generates Rust decoders for Protobuf schemas in a build script, without protoc.
The decoders use the rapidproto runtime. Add rapidproto to your dependencies and
rapidproto-build to your build dependencies, both pinned to the same version with =. The
runtime’s example module shows a schema and the code generated for it.
Use Config in build.rs:
// build.rs
rapidproto_build::Config::new("crate::schema")
.include("proto")
.compile(&["proto/message.proto"])
.expect("generate decoders");Config::compile writes message.rs to OUT_DIR, named after the first entry. Include it at the
path you passed to Config::new, and decode with it:
mod schema {
include!(concat!(env!("OUT_DIR"), "/message.rs"));
}
// For `package shop;` and `message Order` in message.proto:
let arena = rapidproto::Arena::new();
let order = schema::arena::shop::Order::decode(&input, &arena)?;Config generates arena decoders by default. It can also generate stream decoders, apply a
decode profile, and implement serde::Serialize. To get the code as a string instead of a
file, use Config::generate. Imports of the well-known types, such as
google/protobuf/timestamp.proto, resolve without the files.
The command-line tool rapidproto-rust generates the same code, for projects that commit it.
Install it with cargo install rapidproto-build --version 0.1.0-alpha.2, and see
rapidproto-rust --help for its options.
§How the generator runs
The generator is written in C++, and two features choose how it runs. With native, this crate’s
build script compiles it, which needs a C++17 compiler. With wasm, it runs as an embedded
WebAssembly module in the wasmi interpreter, which needs no compiler. Both produce the same
output.
Both features are on by default. The native generator is used when it builds, and the wasm one
otherwise, which Config::compile then warns about. If you have a C++ compiler,
default-features = false, features = ["native"] avoids building the interpreter. To use the wasm
generator on purpose, without the warning, enable only wasm.
Cargo builds build-time code without optimization, so the interpreter is slow by default. It takes seconds for a small schema and minutes for a very large one. If you use the wasm generator, optimize build-time code in your workspace:
[profile.dev.build-override]
opt-level = 3
[profile.release.build-override]
opt-level = 3