Overview
The serialization system provides three main components:- SerializableMessage trait - Main trait for all serializable message types
- ProtoSerializable trait - Optional protobuf serialization support
- raw module - Low-level functions for Copy type serialization
For a quick start guide with examples, see the Quickstart page.
Quick Reference
| Component | Description | Category |
|---|---|---|
SerializableMessage | Main serialization trait | Core |
to_bytes() | Serialize message to bytes | Core |
from_bytes() | Deserialize message from bytes (with relocatable support) | Core |
ProtoSerializable | Protobuf serialization trait | Protobuf |
to_proto_bytes() | Serialize protobuf message | Protobuf |
from_proto_bytes() | Deserialize protobuf message | Protobuf |
raw::struct_to_bytes() | Convert Copy type to raw bytes | Raw |
raw::bytes_to_struct() | Convert raw bytes to Copy type | Raw |
SerializableMessage Trait
SerializableMessage
SerializableMessage
Signature
Description
The main trait that all message types must implement. This trait is automatically implemented for anyCopy type that also implements Send + Sync + 'static.The trait provides two methods:to_bytes()- Serializes the message to a byte vectorfrom_bytes()- Deserializes a message from bytes, with automatic relocatable message handling
Automatic Implementation
The trait is automatically implemented for allCopy types:Relocatable Message Support
Thefrom_bytes() implementation automatically handles relocatable-wrapped messages:- If bytes are relocatable-wrapped: Extracts the
payloadfield from the relocatable message - If bytes are not relocatable: Uses the bytes directly
Example
See Also
- Copy Types - Learn about automatic Copy type serialization
- raw module - Low-level serialization functions
ProtoSerializable Trait
ProtoSerializable
ProtoSerializable
Signature
Description
Optional trait for types that want to use protobuf serialization instead of raw byte serialization. This is useful for:- Cross-language communication (Rust ↔ Python ↔ C++)
- Schema evolution (adding/removing fields)
- Complex nested message types
- When you need guaranteed compatibility
ProstMessage + Default + Send + Sync + 'static.Automatic Implementation
When to Use
Example
See Also
- Protobuf Guide - Complete guide to protobuf serialization
Raw Module
raw::struct_to_bytes()
raw::struct_to_bytes()
Signature
Description
Converts anyCopy type to raw bytes using its memory representation. This function is used internally by the SerializableMessage implementation for Copy types.Parameters
| Parameter | Type | Description |
|---|---|---|
data | &T | Reference to a Copy type to serialize |
Returns
Vec<u8>- Byte vector containing the serialized data
Example
Implementation Details
The function usesunsafe code to read the memory representation directly:#[repr(C)] structs containing only primitive types and fixed-size arrays.raw::bytes_to_struct()
raw::bytes_to_struct()
Signature
Description
Converts raw bytes back to aCopy type. This function validates that the byte length matches the type size before deserializing.Parameters
| Parameter | Type | Description |
|---|---|---|
bytes | &[u8] | Byte slice to deserialize |
Returns
Ok(T)- Successfully deserialized typeErr(e)- Error if byte length doesn’t match type size
Example
Error Conditions
The function returns an error if:- The byte length doesn’t match
mem::size_of::<T>() - The bytes cannot be safely interpreted as the target type
Implementation Details
The function validates size and usesunsafe code to read the memory:UseProto Marker Trait
UseProto
UseProto
Signature
Description
Marker trait used internally to disambiguate betweenCopy types and ProstMessage types. This trait is typically not used directly by end users.When to Use
Most users don’t need to interact with this trait directly. It’s used internally by the serialization system to determine which serialization method to use.