betterproto-rust-codec (Rust-backed Protobuf Codec)
betterproto-rust-codec provides a high-performance codec for `betterproto` messages, leveraging a Rust implementation to serialize and deserialize Protobuf wire format. It significantly speeds up conversion compared to Python-native alternatives, especially for large or frequently processed messages. The current version is 0.1.1, and releases are generally ad-hoc, often in response to `betterproto` updates or targeted performance improvements.
Warnings
- breaking This library has strict dependency requirements for `betterproto`. Ensure your `betterproto` version meets the minimum requirement specified by `betterproto-rust-codec` (currently `>=2.0.0b6`). Using incompatible versions can lead to runtime errors or incorrect serialization.
- breaking Similarly, `betterproto-rust-codec` also has a minimum requirement for the `protobuf` library itself (currently `>=3.20.0`). Incompatible `protobuf` versions might cause issues with underlying type conversions or wire format interpretation.
- gotcha This codec is designed exclusively for `betterproto` messages. It will not work directly with messages generated by `protoc` or other standard `protobuf` Python libraries without first converting them to `betterproto` types.
- gotcha While providing significant performance benefits, `betterproto-rust-codec` introduces a Rust dependency (compiled into a wheel). For extremely simple use cases or environments where Rust compilation might be an issue (e.g., highly restricted embedded systems, though pre-built wheels usually mitigate this), the overhead might not be justified if performance isn't a critical concern.
Install
-
pip install betterproto-rust-codec
Imports
- ProtobufCodec
from betterproto_rust_codec import ProtobufCodec
Quickstart
import betterproto
from betterproto.lib.google.protobuf import Timestamp
from betterproto_rust_codec import ProtobufCodec
import datetime
@betterproto.dataclass
class MyMessage(betterproto.Message):
name: str = betterproto.string_field(1)
value: int = betterproto.int32_field(2)
timestamp: Timestamp = betterproto.message_field(3, wraps=True)
# Create an instance of the codec
codec = ProtobufCodec()
# Create a betterproto message
now_timestamp = Timestamp().from_datetime(datetime.datetime.now(datetime.timezone.utc))
original_message = MyMessage(
name="Test Name",
value=123,
timestamp=now_timestamp
)
print(f"Original message: {original_message}")
# Encode the message
encoded_bytes = codec.encode(original_message)
print(f"Encoded bytes length: {len(encoded_bytes)}")
# Decode the message
decoded_message = codec.decode(MyMessage, encoded_bytes)
# Verify
print(f"Decoded message: {decoded_message}")
assert original_message.name == decoded_message.name
assert original_message.value == decoded_message.value
assert original_message.timestamp.seconds == decoded_message.timestamp.seconds
print("\nEncode/Decode successful!")