Module combinators

Source
Expand description

Combinators for composing and building complex encodables.

This module provides utility types for building complex Encodable types from simpler ones. These combinators allow common operations for encoding values, such as conditional encoding, prefixes or custom byte ordering.

These building blocks follow the Encodable trait interface and can be nested or chained together to define rich encoding behavior with minimal boilerplate.

§Categories of Encodables

§Primitive and Built-in Encodables

These are the basic types that can be directly encoded.

TypeDescription
(...)Encodes a tuple by encoding each element in order
i8 and u8Encodes a single byte
charEncodes a character as its UTF-8 byte representation
strEncodes a UTF-8 string slice
CStrEncodes a C string slice, including the null terminator (\0)
[u8; N]Encodes a byte array
[u8]Encodes a byte slice
[bool; 8]Encodes a set of flags as a single byte
ArgumentsEncodes formatted data from format_args with zero allocations

§Composition and Flow Combinators

These types wrap or transform other encodables, allowing conditional encoding, iteration, or control over the format and structure of the output.

TypeDescription
OptionEncodes the inner value if Some; does nothing if None
ResultEncodes the value on Ok; returns the error on Err
CondEncodes a value only if a condition is met
LEEncodes a number in little-endian order
BEEncodes a number in big-endian order
LengthPrefixEncodes a length prefixed value (TLV)
SeparatedEncodes a sequence of encodables separated by a given delimiter
IterEncodes a sequence of encodables
FromErrorTransforms the error type of an encodable.

§Alloc Encodables (requires the alloc OR std features)

These types are supported when the alloc or std feature is enabled.

TypeDescription
Vec<u8>Encodes a byte vector as a contiguous sequence of bytes
StringEncodes a heap-allocated UTF-8 string
CStringEncodes a C-style string including the null terminator (\0)
Box<T>Encodes the value pointed to by a Box, as if it were directly encoded

Structs§

BE
Encodes a number in big-endian order.
Cond
Encodes a value only if a condition is met
FlagsDeprecated
Encodes a set of bit flags packed into a single byte
FromError
Transforms the error type of an encodable.
Iter
Encodes a sequence of encodables.
LE
Encodes a number in little-endian order.
LengthPrefix
Encodes a length prefixed value (TLV).
Separated
Encodes a sequence of encodables separated by a given delimiter.