Convert Figma logo to code with AI

stepancheg logorust-protobuf

Rust implementation of Google protocol buffers

2,949
397
2,949
90

Top Related Projects

[Deprecated] Protocol Buffers for Go with Gadgets

Plugin and runtime library for using protobuf with Swift

Quick Overview

rust-protobuf is a Protocol Buffers implementation for Rust. It provides a complete set of tools for working with Protocol Buffers in Rust, including code generation, serialization, and deserialization. The project aims to offer a native Rust experience while maintaining compatibility with the official protobuf implementation.

Pros

  • Native Rust implementation, providing better integration with Rust ecosystems
  • Supports both proto2 and proto3 syntax
  • Offers good performance and memory efficiency
  • Actively maintained with regular updates

Cons

  • May have some compatibility issues with certain protobuf features or edge cases
  • Documentation could be more comprehensive
  • Learning curve for developers new to both Rust and Protocol Buffers
  • Some advanced protobuf features might not be fully supported

Code Examples

  1. Defining a message in .proto file:
syntax = "proto3";

message Person {
  string name = 1;
  int32 age = 2;
  repeated string hobbies = 3;
}
  1. Generating Rust code from .proto file:
protoc --rust_out=. person.proto
  1. Using generated code to create and serialize a message:
use person::Person;
use protobuf::Message;

let mut person = Person::new();
person.set_name("Alice".to_string());
person.set_age(30);
person.set_hobbies(vec!["reading".to_string(), "cycling".to_string()]);

let bytes = person.write_to_bytes().unwrap();
  1. Deserializing a message:
use person::Person;
use protobuf::Message;

let person = Person::parse_from_bytes(&bytes).unwrap();
println!("Name: {}", person.get_name());
println!("Age: {}", person.get_age());
println!("Hobbies: {:?}", person.get_hobbies());

Getting Started

  1. Add the following to your Cargo.toml:
[dependencies]
protobuf = "3.0"
  1. Install the protobuf compiler:
sudo apt-get install protobuf-compiler  # For Ubuntu/Debian
brew install protobuf  # For macOS
  1. Generate Rust code from your .proto files:
protoc --rust_out=. your_proto_file.proto
  1. Include the generated code in your Rust project and start using Protocol Buffers!

Competitor Comparisons

[Deprecated] Protocol Buffers for Go with Gadgets

Pros of gogo/protobuf

  • Written in Go, offering better performance and integration with Go projects
  • Provides additional features like custom tags and more efficient marshaling/unmarshaling
  • Actively maintained with frequent updates and a large community

Cons of gogo/protobuf

  • Limited to Go language, while rust-protobuf is specific to Rust
  • May have a steeper learning curve due to additional features and customization options
  • Potentially larger binary size due to extra functionality

Code Comparison

gogo/protobuf:

import "github.com/gogo/protobuf/proto"

func main() {
    data, _ := proto.Marshal(&myMessage)
    proto.Unmarshal(data, &myMessage)
}

rust-protobuf:

use protobuf::Message;

fn main() {
    let data = my_message.write_to_bytes().unwrap();
    let my_message = MyMessage::parse_from_bytes(&data).unwrap();
}

Both libraries provide similar functionality for serializing and deserializing Protocol Buffer messages. The main differences lie in the language-specific implementations and additional features offered by gogo/protobuf for Go projects.

Plugin and runtime library for using protobuf with Swift

Pros of swift-protobuf

  • Official Apple support, ensuring compatibility with Swift ecosystem
  • Optimized for Swift performance and language features
  • Comprehensive documentation and examples

Cons of swift-protobuf

  • Limited to Swift language, less versatile than rust-protobuf
  • Smaller community and fewer third-party integrations

Code Comparison

swift-protobuf:

import SwiftProtobuf

let message = MyMessage()
message.id = 12345
message.title = "Hello, Protocol Buffers!"

let data = try message.serializedData()

rust-protobuf:

use protobuf::Message;

let mut message = MyMessage::new();
message.set_id(12345);
message.set_title("Hello, Protocol Buffers!".to_string());

let data = message.write_to_bytes().unwrap();

Both libraries provide similar functionality for working with Protocol Buffers, but with syntax and patterns specific to their respective languages. swift-protobuf leverages Swift's strong typing and optionals, while rust-protobuf utilizes Rust's ownership model and error handling.

The choice between these libraries largely depends on the target language and ecosystem of the project. swift-protobuf is ideal for iOS and macOS development, while rust-protobuf offers broader platform support and integration with Rust's growing ecosystem.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

rust-protobuf

crates.io version docs.rs GitHub Workflow Status License

End of life

This implementation is approaching end of life. Official protobuf implementation (not based on this implementation) is on the way, that will be released as protobuf=4 soon. Follow progress in protobuf repository.

Original readme

Protobuf implementation in Rust.

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

Where is documentation

Documentation is hosted on docs.rs.

Versions and branches

Version 3

Version 3 is current stable version. Compared to version 2 it implements:

  • runtime reflection
  • JSON and text format parsing and printing
  • dynamic messages (messages which can be created from .proto file on the fly without code generation)

Version 2

Version 2 is previous stable version. Only most critical bugfixes will be applied to 2.x version, otherwise it won't be maintained.

Help

The crate needs help:

  • a new maintainer, but also
  • testing
  • documentation
  • examples to be used as documentation
  • feedback on API design
  • feedback on implementation
  • pull requests

Changelog

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

Related projects

  • prost — another protobuf implementation in Rust, also has gRPC implementation
  • quick-protobuf — alternative protobuf implementation in Rust
  • grpc-rs — another gRPC implementation for Rust
  • grpc-rust — incomplete implementation of gRPC based on this library