#bin #google #protocol-buffers #protobuf #runtime #protocol #buffers #code

bin+lib protoc-bin-vendored

protoc binaries compiled by Google and bundled in this crate

18 stable releases

new 2.24.1 Jun 11, 2021
2.22.1 Mar 18, 2021
2.18.1 Nov 22, 2020
2.16.2 Jul 6, 2020

#81 in Network programming

Download history 152/week @ 2021-02-20 311/week @ 2021-02-27 167/week @ 2021-03-06 216/week @ 2021-03-13 251/week @ 2021-03-20 232/week @ 2021-03-27 272/week @ 2021-04-03 322/week @ 2021-04-10 408/week @ 2021-04-17 628/week @ 2021-04-24 930/week @ 2021-05-01 878/week @ 2021-05-08 775/week @ 2021-05-15 637/week @ 2021-05-22 440/week @ 2021-05-29 719/week @ 2021-06-05

2,164 downloads per month
Used in lockjaw_processor

MIT license

10MB
106 lines

rust-protobuf

GitHub Workflow Status crates.io version License

Protobuf implementation in Rust.

  • Written in pure rust
  • Generate rust code
  • Has runtime library for generated code (Coded{Input|Output}Stream impl)
  • Supports both Protobuf versions 2 and 3

List of crates

rust-protobuf — repository provides multiple crates:

  • protobuf — protobuf runtime
  • protobuf-codegen — protobuf codegen engine and protoc-gen-rust plugin for protoc command
  • protoc — programmatically work with protoc command
  • protoc-rust — codegen which can be invoked programmatically using protoc binary (e. g. from build.rs)
  • protobuf-codegen-pure — pure rust codegen
  • protoc-bin-vendoredprotoc binary packaged as crate, can be used with protoc or protoc-rust crates

About versions and branches

  • 2.*.* is the latest stable version. 2.*.* versions follow semver conventions
  • versions below 2 are no longer supported

See CHANGELOG.md for a list of changes and compatility issues between versions.

How to generate rust code

There are several ways to generate rust code from .proto files:

Generated code

Have a look at generated files, used internally in rust-protobuf:

Rustdoc

docs.rs hosts rustdoc for protobuf.

Getting help

Feel free to open an issue if you need help with rust-protobuf.

Copy-on-write

Rust-protobuf can be used with bytes crate.

To enable Bytes you need to:

  1. Enable with-bytes feature in rust-protobuf:
[dependencies]
protobuf = { version = "2", features = ["with-bytes"] }
  1. Enable bytes option

with Customize when codegen is invoked programmatically:

With stable rust-protobuf:

protoc_rust::run(protoc_rust::Args {
    ...
    customize: Customize {
        carllerche_bytes_for_bytes: Some(true),
        carllerche_bytes_for_string: Some(true),
        ..Default::default()
    },
 });

With rust-protobuf from master:

protoc_rust::Args::new()
    ...
    .customize(Customize {
        carllerche_bytes_for_bytes: Some(true),
        carllerche_bytes_for_string: Some(true),
        ..Default::default()
    })
    .run()?;

or in .proto file:

import "rustproto.proto";

option (rustproto.carllerche_bytes_for_bytes_all) = true;
option (rustproto.carllerche_bytes_for_string_all) = true;

With these options enabled, fields of type bytes or string are generated as Bytes or Chars respectively. When CodedInputStream is constructed from Bytes object, fields of these types get subslices of original Bytes object, instead of being allocated on heap.

serde_derive support

(Only in master, not released yet)

Rust-protobuf can be used with serde.

To enable serde you need to:

  1. Enable serde option

with Customize when codegen is invoked programmatically:

with stable rust-protobuf:

protoc_rust::run(protoc_rust::Args {
    ...
    customize: Customize {
        serde_derive: Some(true),
        ..Default::default()
    },
});

with rust-protobuf from master:

protoc_rust::Args::new()
    ...
    .customize(Customize {
        serde_derive: Some(true),
        ..Default::default()
    })
    .run()?;

or in .proto file:

import "rustproto.proto";

option (rustproto.serde_derive_all) = true;

You may now Serialize and Deserialize messages:

let my_message = MyMessage::new();
serde_json::to_string(&my_message).unwrap();

Related projects

No runtime deps