Expand description
§Procedural Macros for Automatic Serialization and Deserialization
Provides procedural macros for deriving serialization and deserialization
traits on structs used in binary protocol communication. The macros Encodable
and Decodable
generate implementations of encoding and decoding behaviors, making it simpler to work with
binary data by automatically handling field parsing, sizing, and transformation.
§Overview
These macros parse struct definitions to produce code that supports efficient and type-safe serialization and deserialization. Each field within a struct is processed based on its type and associated generics, allowing for custom encoding schemes and alignment with protocol requirements. Additionally, the macros enable flexible handling of lifetimes and static references to ensure compatibility across different use cases.
§Available Macros
-
Encodable
: Automatically implements encoding logic, converting a struct’s fields into a binary format.- Attributes:
#[already_sized]
(optional) to specify that a struct’s size is fixed at compile-time. - Generated Traits:
EncodableField
(field-by-field encoding) andGetSize
(size calculation).
- Attributes:
-
Decodable
: Automatically implements decoding logic, allowing a struct to be reconstructed from binary data.- Generated Methods:
get_structure
(defines field structure) andfrom_decoded_fields
(builds the struct from decoded fields).
- Generated Methods:
§Internal Structure
§is_already_sized
Checks if the #[already_sized]
attribute is present on the struct, allowing certain
optimizations in generated code for fixed-size structs.
§get_struct_properties
Parses and captures a struct’s name, generics, and field data, enabling custom encoding and decoding functionality.
§Custom Implementations
The Encodable
macro generates an EncodableField
implementation by serializing each field,
while Decodable
constructs the struct from binary data. Both macros provide support for
structs with or without lifetimes, ensuring versatility in applications that require efficient,
protocol-level data handling.
Derive Macros§
- Decodable
- Derives the
Decodable
trait, generating implementations for deserializing a struct from a byte stream, including its structure, field decoding, and a method for creating a static version. - Encodable
- Derives the
Encodable
trait, generating implementations for serializing a struct into an encoded format, including methods for field serialization, calculating the encoded size, and handling cases where the struct is already sized.