Expand description
Provides an encoding framework for serializing various data types into bytes.
The Encodable
trait is the core of this framework, enabling types to define
how they serialize data into bytes. This is essential for transmitting data
between components or systems in a consistent, byte-oriented format.
§Overview
Supports a wide variety of data types, including basic types (e.g., integers,
booleans, and byte arrays) and complex structures. Each type’s encoding logic is
encapsulated in enums like [EncodablePrimitive
] and EncodableField
, enabling
structured and hierarchical data serialization.
§Key Types
Encodable
: Defines methods for converting an object into a byte array or writing it directly to an output stream. It supports both primitive types and complex structures.- [
EncodablePrimitive
]: Represents basic types that can be serialized directly. Includes data types like integers, booleans, and byte arrays. EncodableField
: Extends [EncodablePrimitive
] to support structured and nested data, enabling recursive encoding of complex structures.
§no_std
Compatibility
When compiled with the no_std
feature enabled, this module omits the to_writer
method
to support environments without the standard library. Only buffer-based encoding
(to_bytes
) is available in this mode.
§Error Handling
Errors during encoding are handled through the Error
type. Common failure scenarios include
buffer overflows and type-specific serialization errors. Each encoding method returns an
appropriate error if encoding fails, supporting comprehensive error management.
§Trait Details
§Encodable
to_bytes
: Encodes the instance into a byte slice, returning the number of bytes written or an error if encoding fails.to_writer
(requiresstd
): Encodes the instance into any [Write
] implementor, such as a file or network stream.
§Additional Enums and Methods
Includes utility types and methods for calculating sizes, encoding hierarchical data, and supporting both owned and reference-based data variants.
- [
EncodablePrimitive
]: Handles encoding logic for primitive types, addressing serialization requirements specific to each type. EncodableField
: Extends encoding to support composite types and structured data, enabling recursive encoding of nested structures.
§Summary
Designed for flexibility and extensibility, this module supports a wide range of data
serialization needs through customizable encoding strategies. Implementing the
Encodable
trait for custom types ensures efficient and consistent data serialization
across applications.
Enums§
- Encodable
Field - The
EncodableField
enum defines encodable fields, which may be a primitive or struct. - Encodable
Primitive - The
EncodablePrimitive
enum defines primitive types that can be encoded.
Traits§
- Encodable
- The
Encodable
trait defines the interface for encoding a type into bytes.