leavehouse / rust-protobuf

Rust implementation of Google protocol buffers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rust-protobuf

Build 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)

How to generate rust code

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

Invoke protoc programmatically with protoc-rust crate (recommended)

Have a look at readme in protoc-rust crate.

Invoke protoc programmatically with protoc crate

Have a look at readme in protoc crate.

With protoc command and protoc-gen-rust plugin

  1. Install protobuf for protoc binary.

On OS X Homebrew can be used:

brew install protobuf

On Ubuntu, protobuf-compiler package can be installed:

apt-get install protobuf-compiler

Protobuf is needed only for code generation, rust-protobuf runtime does not use protobuf library.

  1. Install protoc-gen-rust program (which is protoc plugin)

It can be installed either from source or with cargo install protobuf command.

  1. Add protoc-gen-rust to $PATH

If you installed it with cargo, it should be

PATH="$HOME/.cargo/bin:$PATH"
  1. Generate .rs files:
protoc --rust_out . foo.proto

This will generate .rs files in current directory.

Generated code

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

Rustdoc

docs.rs hosts rustdoc for 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 = "1.3", features = ["with-bytes"] }
  1. Enable bytes option 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.

Related projects

About

Rust implementation of Google protocol buffers

License:BSD 2-Clause "Simplified" License


Languages

Language:Rust 86.6%Language:Protocol Buffer 12.9%Language:Shell 0.2%Language:C++ 0.2%