Crate byten

Crate byten 

Source
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 for no_std environments.
  • alloc: Enables types that require allocation (Vec, Box, String). Enabled by default via std, but can be used independently in no_std environments with an allocator.
  • anyhow: Integration with the anyhow error handling crate (enabled by default, requires std).
  • derive: Enables procedural macros for deriving self-coded trait implementations for structs and enums (enabled by default).

§Built-in Codecs

Codec TypeDefault for#[byten(…)]Codes
U8Codecu8u8u8 type
I8Codeci8i8i8 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
BoolCodecboolboolbool type
BoxCodecBox<T>Box<T> or ... boxBox<T> where t_codec is the codec for T
ArrayCodec... []fixed-size arrays, [Item; N]
EndianCodec... $be or ... $leprimitive types with little/big endian byte orders
SelfCoded{SelfCoded::<T>::new()}derived type T
UTF8Codec$utf8&str type using the provided bytes codec
CStrCodecCStrCStr or &CStrC-style strings (CStr and &CStr)
OwnedCodec... $ownowned 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... $uvarbeunsigned variable-length big-endian integers
OptionCodecOption<T>... ?Option<T> using the provided codec for T
BytesCodec$bytes[len]byte slices with length prefixed by the provided codec
PhantomCodec= exprphantom 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§

byten

Structs§

ArrayCodec
A codec for fixed-size arrays of fixed/dynamic sized elements.
BoolCodec
A codec for the bool type.
BoxCodec
A codec that boxes the decoded value of another codec.
BytesCodec
A codec for byte slices with a length prefix. The length of the byte slice is encoded/decoded using the provided length codec.
CStrCodec
A codec for C-style null-terminated strings.
EndianCodec
A codec that encodes and decodes values using specified endianness.
HeaplessCStringCodec
A codec for heapless C-style null-terminated strings.
I8Codec
A codec for the i8 type.
OptionCodec
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).
OwnedCodec
A codec that decodes to owned data by first decoding to borrowed data and then cloning it.
PhantomCodec
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.
PrefixedCodec
RemainingCodec
A codec that draws all remaining bytes from the input during decoding, and writes all bytes during encoding.
SelfCoded
A wrapper codec that re-uses the Decode, Encode, Measure, and MeasureFixed traits of the decoded type.
Tuple1Codec
Tuple2Codec
Tuple3Codec
Tuple4Codec
Tuple5Codec
Tuple6Codec
Tuple7Codec
U8ArrayCodec
A codec for fixed-size arrays of u8.
U8ArrayRefCodec
A codec for references to fixed-size arrays of u8.
U8Codec
A codec for the u8 type.
UTF8Codec
A codec for UTF-8 encoded strings.
UVarBECodec
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.
UnitCodec
Codec for the unit type (). The unit codec encodes and decodes the unit type without consuming any bytes.

Enums§

DecodeError
EncodeError
Endianness

Traits§

BitStream
ConstantCoder
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.
DecodeDefault
DecodeOwned
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 Decoded from a byte slice. It returns a result containing the decoded value or an error if decoding fails.
DefaultCodec
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.
EncodeDefault
EncodeToVec
Encoder
A codec that can encode values of type Decoded into a byte slice. It returns a result indicating success or failure but never panics.
EncoderToVec
EndianCoded
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.
FixedMeasurer
A codec that can measure the size in bytes required to encode a value of type Decoded where 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.
MeasureFixed
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.

Type Aliases§

BitIndex
Tuple0Codec

Derive Macros§

Decode
DecodeOwned
DefaultCodec
Encode
Measure
MeasureFixed