Expand description
A binary codec framework for encoding and decoding Rust types to and from byte slices.
It provides traits and implementations for codecs, encoders, decoders, measurers, and fixed-size measurers.
The library is designed to be extensible and efficient, allowing users to define custom codecs for their types while leveraging existing implementations for common types.
§Features
std: Enables standard library support (enabled by default). Disable forno_stdenvironments.alloc: Enables types that require allocation (Vec,Box,String). Enabled by default viastd, but can be used independently inno_stdenvironments with an allocator.anyhow: Integration with theanyhowerror handling crate (enabled by default, requiresstd).derive: Enables procedural macros for deriving self-coded trait implementations for structs and enums (enabled by default).
§Built-in Codecs
| Codec Type | Default for | #[byten(…)] | Codes |
|---|---|---|---|
U8Codec | u8 | u8 | u8 type |
I8Codec | i8 | i8 | i8 type |
U8ArrayCodec | [u8; N] | [u8; N] | fixed-size arrays of u8 of size N |
U8ArrayRefCodec | &[u8; N] | &[u8; N] | references to fixed-size arrays of u8 of size N |
BoolCodec | bool | bool | bool type |
BoxCodec | Box<T> | Box<T> or ... box | Box<T> where t_codec is the codec for T |
ArrayCodec | ... [] | fixed-size arrays, [Item; N] | |
EndianCodec | ... $be or ... $le | primitive types with little/big endian byte orders | |
SelfCoded | {SelfCoded::<T>::new()} | derived type T | |
UTF8Codec | $utf8 | &str type using the provided bytes codec | |
CStrCodec | CStr | CStr or &CStr | C-style strings (CStr and &CStr) |
OwnedCodec | ... $own | owned data by cloning from borrowed data | |
PrefixedCodec | ... for[len] | Collections of Items using the provided item and length codecs | |
RemainingCodec | .. | all remaining bytes in the input | |
UVarBECodec | ... $uvarbe | unsigned variable-length big-endian integers | |
OptionCodec | Option<T> | ... ? | Option<T> using the provided codec for T |
BytesCodec | $bytes[len] | byte slices with length prefixed by the provided codec | |
PhantomCodec | = expr | phantom codec with 0 size, codes the given constant value | |
TupleNCodec | (a, ...N) | (a, ...N) | tuple codecs for tuples of size N |
§Usage in no_std Environments
§Core only (no allocator)
[dependencies]
byten = { version = "0.0", default-features = false }Provides: primitives, arrays, slices, borrowed data (&str, &[u8], &CStr)
§With allocator
[dependencies]
byten = { version = "0.0", default-features = false, features = ["alloc"] }Adds: Vec<T>, Box<T>, owned strings, variable-length encoding
Macros§
Structs§
- Array
Codec - A codec for fixed-size arrays of fixed/dynamic sized elements.
- Bool
Codec - A codec for the
booltype. - BoxCodec
- A codec that boxes the decoded value of another codec.
- Bytes
Codec - A codec for byte slices with a length prefix. The length of the byte slice is encoded/decoded using the provided length codec.
- CStr
Codec - A codec for C-style null-terminated strings.
- Endian
Codec - A codec that encodes and decodes values using specified endianness.
- HeaplessC
String Codec - A codec for heapless C-style null-terminated strings.
- I8Codec
- A codec for the
i8type. - Option
Codec - A codec for optional values. The presence of a value is indicated by a preceding boolean flag. If the flag is true, the value is absent (None). If the flag is false, the value is present (Some).
- Owned
Codec - A codec that decodes to owned data by first decoding to borrowed data and then cloning it.
- Phantom
Codec - A codec that always encodes and decodes to a constant value. It does not read or write any bytes. And it measures to fixed zero bytes.
- Prefixed
Codec - Remaining
Codec - A codec that draws all remaining bytes from the input during decoding, and writes all bytes during encoding.
- Self
Coded - A wrapper codec that re-uses the Decode, Encode, Measure, and MeasureFixed traits of the decoded type.
- Tuple1
Codec - Tuple2
Codec - Tuple3
Codec - Tuple4
Codec - Tuple5
Codec - Tuple6
Codec - Tuple7
Codec - U8Array
Codec - A codec for fixed-size arrays of
u8. - U8Array
RefCodec - A codec for references to fixed-size arrays of
u8. - U8Codec
- A codec for the
u8type. - UTF8
Codec - A codec for UTF-8 encoded strings.
- UVarBE
Codec - A codec for unsigned variable-length big-endian encoded integers. The integer is represented as a series of 7-bit septets, where the most significant bit of each septet indicates whether there are more septets to follow.
- Unit
Codec - Codec for the unit type
(). The unit codec encodes and decodes the unit type without consuming any bytes.
Enums§
Traits§
- BitStream
- Constant
Coder - A trait for codecs that always encode and decode to a constant value.
- Decode
- A trait for types that can decode themselves from a byte slice. The lifetime ’encoded refers to the lifetime of the byte slice being decoded.
- Decode
Default - Decode
Owned - A trait for types that can decode themselves from a byte slice, returning an owned instance.
- Decoder
- A codec that can decode values of type
Decodedfrom a byte slice. It returns a result containing the decoded value or an error if decoding fails. - Default
Codec - A trait for types that have a default codec associated with them. This allows for easy retrieval of the default codec for a type without needing to specify the codec explicitly each time.
- Encode
- A trait for types that can encode themselves into a byte slice.
- Encode
Default - Encode
ToVec - Encoder
- A codec that can encode values of type
Decodedinto a byte slice. It returns a result indicating success or failure but never panics. - Encoder
ToVec - Endian
Coded - A trait for types that can encode and decode themselves with specified endianness. The implementor must ensure that the length of the byte representation is constant.
- Fixed
Measurer - A codec that can measure the size in bytes required to encode a value of type
Decodedwhere the size is fixed and does not depend on the actual value. It provides a method to get the fixed size directly without needing a value. - Measure
- A trait for types that can measure the size of their encoded representation.
- Measure
Fixed - A trait for types that have a fixed size when encoded.
- Measurer
- A codec that can measure the size in bytes required to encode a value of type
Decoded. It returns a result containing the size in bytes or an error if measurement fails.