quick-protobuf
A pure Rust library to serialize/deserialize protobuf files.
Description
This crate intends to provide a simple yet fast (minimal allocations) protobuf parser implementation. In general, you should probably NOT need to use this crate directly, else, you should use the modules automatically generated by pb-rs tool.
Example
-
- Install pb-rs binary to convert your proto file into a quick-protobuf compatible source code
# will generate a
# /path/to/your/protobuf/file.rs
-
- Add a dependency to quick-protobuf
# Cargo.toml
[]
= "0.6.2"
-
- Have fun
extern crate quick_protobuf;
// (see 1.)
use Reader;
// We will suppose here that Foo and Bar are two messages defined in the .proto file
// and converted into rust structs
//
// FooBar is the root message defined like this:
// message FooBar {
// repeated Foo foos = 1;
// repeated Bar bars = 2;
// }
// FooBar is a message generated from a proto file
// in parcicular it contains a `from_reader` function
use FooBar;
use ;
Message <-> struct
The best way to check for all kind of generated code is to look for the codegen_example data:
- definition: data_types.proto
- generated code: data_types.rs
Proto definition
enum FooEnum {
FIRST_VALUE = 1;
SECOND_VALUE = 2;
}
message BarMessage {
required int32 b_required_int32 = 1;
}
message FooMessage {
optional int32 f_int32 = 1;
optional int64 f_int64 = 2;
optional uint32 f_uint32 = 3;
optional uint64 f_uint64 = 4;
optional sint32 f_sint32 = 5;
optional sint64 f_sint64 = 6;
optional bool f_bool = 7;
optional FooEnum f_FooEnum = 8;
optional fixed64 f_fixed64 = 9;
optional sfixed64 f_sfixed64 = 10;
optional fixed32 f_fixed32 = 11;
optional sfixed32 f_sfixed32 = 12;
optional double f_double = 13;
optional float f_float = 14;
optional bytes f_bytes = 15;
optional string f_string = 16;
optional FooMessage f_self_message = 17;
optional BarMessage f_bar_message = 18;
repeated int32 f_repeated_int32 = 19;
repeated int32 f_repeated_packed_int32 = 20 [ packed = true ];
}
Generated structs
Leverage rust module system
Nested Messages
message A {
message B {
// ...
}
}
As rust does not allow a struct and a module to share the same name, we use mod_Name
for the nested messages.
Package
package a.b;
Here we could have used the same name, but for consistency with nested messages, modules are prefixed with mod_
as well.
Why not rust-protobuf
This library is an alternative to the widely used rust-protobuf.
Pros / Cons
-
Pros
- Much faster, in particular when working with string, bytes and repeated packed fixed size fields (no extra allocation)
- No need to install
protoc
on your machine - No trait objects: faster/simpler parser
- Very simple generated modules (~10x smaller) so you can easily understand what is happening
-
Cons
- Less popular
- most rust-protobuf tests have been migrated here (see v2 and v3)
- quick-protobuf is being used by many people now and is very reliable
- some missing functionalities
- Not a drop-in replacement of rust-protobuf
- everything being explicit you have to handle more things yourself (e.g.
Option
unwrapping,Cow
management)
- everything being explicit you have to handle more things yourself (e.g.
- Less popular
Contribution
Any help is welcomed! (Pull requests of course, bug report, missing functionality etc...)
Licence
MIT