#rpc #grpc #async #codegen #protobuf

build tonic-build

Codegen module of tonic gRPC implementation

14 unstable releases (4 breaking)

0.4.0 Jan 15, 2021
0.3.1 Aug 20, 2020
0.3.0 Jul 13, 2020
0.1.1 Jan 20, 2020
0.1.0-alpha.6 Nov 10, 2019

#8 in Network programming

Download history 24065/week @ 2020-11-02 24730/week @ 2020-11-09 27618/week @ 2020-11-16 23975/week @ 2020-11-23 26871/week @ 2020-11-30 29997/week @ 2020-12-07 28189/week @ 2020-12-14 17293/week @ 2020-12-21 15185/week @ 2020-12-28 28149/week @ 2021-01-04 31825/week @ 2021-01-11 32195/week @ 2021-01-18 35511/week @ 2021-01-25 37947/week @ 2021-02-01 38616/week @ 2021-02-08 36461/week @ 2021-02-15

107,603 downloads per month
Used in 94 crates (73 directly)

MIT license

42KB
900 lines

tonic-build

Compiles proto files via prost and generates service stubs and proto definitiones for use with tonic.

Features

  • rustfmt: This feature enables the use of rustfmt to format the output code this makes the code readable and the error messages nice. This requires that rustfmt is installed. This is enabled by default.

Required dependencies

[dependencies]
tonic = <tonic-version>
prost = <prost-version>

[build-dependencies]
tonic-build = <tonic-version>

Examples

Simple

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tonic_build::compile_protos("proto/service.proto")?;
    Ok(())
}

Configuration

fn main() -> Result<(), Box<dyn std::error::Error>> {
   tonic_build::configure()
        .build_server(false)
        .compile(
            &["proto/helloworld/helloworld.proto"],
            &["proto/helloworld"],
        )?;
   Ok(())
}

lib.rs:

tonic-build compiles proto files via prost and generates service stubs and proto definitiones for use with tonic.

Features

  • rustfmt: This feature enables the use of rustfmt to format the output code this makes the code readable and the error messages nice. This requires that rustfmt is installed. This is enabled by default.

Required dependencies

[dependencies]
tonic = <tonic-version>
prost = <prost-version>

[build-dependencies]
tonic-build = <tonic-version>

Examples

Simple

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tonic_build::compile_protos("proto/service.proto")?;
    Ok(())
}

Configuration

fn main() -> Result<(), Box<dyn std::error::Error>> {
   tonic_build::configure()
        .build_server(false)
        .compile(
            &["proto/helloworld/helloworld.proto"],
            &["proto/helloworld"],
        )?;
   Ok(())
}

NixOS related hints

On NixOS, it is better to specify the location of PROTOC and PROTOC_INCLUDE explicitly.

$ export PROTOBUF_LOCATION=$(nix-env -q protobuf --out-path --no-name)
$ export PROTOC=$PROTOBUF_LOCATION/bin/protoc
$ export PROTOC_INCLUDE=$PROTOBUF_LOCATION/include
$ cargo build

The reason being that if prost_build::compile_protos fails to generate the resultant package, the failure is not obvious until the include!(concat!(env!("OUT_DIR"), "/resultant.rs")); fails with No such file or directory error.

Dependencies

~0.4–3.5MB
~29K SLoC