#grpc #compiler #protobuf #grpc-rs

protoc-grpcio

API for programatically invoking the grpcio (grpc-rs) gRPC compiler

13 releases (7 stable)

Uses old Rust 2015

3.0.0 Dec 9, 2020
2.0.0 Jun 22, 2020
1.3.0 Jun 17, 2020
1.1.0 Sep 26, 2019
0.1.0 Jan 25, 2018

#1620 in Network programming

Download history 2581/week @ 2024-01-06 2153/week @ 2024-01-13 1733/week @ 2024-01-20 1483/week @ 2024-01-27 1648/week @ 2024-02-03 1537/week @ 2024-02-10 1634/week @ 2024-02-17 2140/week @ 2024-02-24 1416/week @ 2024-03-02 1681/week @ 2024-03-09 1487/week @ 2024-03-16 1388/week @ 2024-03-23 1969/week @ 2024-03-30 1162/week @ 2024-04-06 1385/week @ 2024-04-13 1106/week @ 2024-04-20

5,903 downloads per month
Used in 12 crates (8 directly)

Apache-2.0

16KB
250 lines

protoc-grpcio

crates.io Build Status License

A programmatic API to the grpc-rs compiler.

Requirements

  • You must have Google's Protocol Buffer compiler (protoc) installed and in PATH.

Example build.rs

For a project laid out like so:

$ tree
.
├── build.rs
├── Cargo.toml
└── src
    ├── client.rs
    ├── protos
       ├── example
       │   └── diner.proto
       └── mod.rs
    └── server.rs

3 directories, 7 files

The build.rs might look like:

extern crate protoc_grpcio;

fn main() {
    let proto_root = "src/protos";
    println!("cargo:rerun-if-changed={}", proto_root);
    protoc_grpcio::compile_grpc_protos(
        &["example/diner.proto"],
        &[proto_root],
        &proto_root,
        None
    ).expect("Failed to compile gRPC definitions!");
}

Example Cargo.toml

And the Cargo.toml might look like:

[package]
# ...
build = "build.rs"

[lib]
name = "protos"
path = "src/protos/mod.rs"

[[bin]]
name = "server"
path = "src/server.rs"

[[bin]]
name = "client"
path = "src/client.rs"

[dependencies]
futures = "0.1.16"
grpcio = "0.4.3"
protobuf = "~2"

[build-dependencies]
protoc-grpcio = "1.0.2"

You can inspect this example under example/ by compiling and running the example server in one shell session:

cargo run --manifest-path example/Cargo.toml --bin server
...
    Finished dev [unoptimized + debuginfo] target(s) in 27.97 secs
     Running `example/target/debug/server`
listening on 127.0.0.1:34431

And then running the client in another:

$ cargo run --manifest-path example/Cargo.toml --bin client 34431
...
    Finished dev [unoptimized + debuginfo] target(s) in 1.28 secs
     Running `example/target/debug/client 34431`
Ate items: SPAM items: EGGS and got charged $0.30

Credits

Credit to both the TiKV project developers for (grpc-rs) and Stepan Koltsov (@stepancheg, rust-protobuf) for their amazing work bringing Protocol Buffers and gRPC support to Rust.

Dependencies

~3–12MB
~152K SLoC