encode/combinators/mod.rs
1//! Combinators for composing encodables.
2//!
3//! This module contains a number of combinators that can be used to compose
4//! encodables. The objective is to provide a set of building blocks that can be
5//! combined to create more complex encodables that can be encoded into byte
6//! sequences.
7//!
8//! # Available encodables
9//!
10//! ## Basic encodables
11//!
12//! | Type | Description |
13//! |------------|-------------|
14//! | [`(...)`](tuple) | Encodes all encodables in sequence |
15//! | [i8], [u8] | Encodes a single byte |
16//! | [`char`] | Encodes the character as a UTF-8 byte sequence |
17//! | [`&str`](str) | Encodes a string as a UTF-8 byte sequence |
18//! | [`&CStr`](core::ffi::CStr) | Encodes a string as a byte sequence with a null terminator (`\0`) |
19//! | [`&[u8]`](slice) | Encodes a slice of bytes |
20//! | [`Arguments`](core::fmt::Arguments)([`format_args!`])| Runs [`core::fmt`](`core::fmt`) machinery and encodes the result, without allocations |
21//!
22//! ## Encodable combinators
23//!
24//! | Type | Description |
25//! |------------|-------------|
26//! | [`Option`] | Encodes `T` if `Some`, or does nothing on `None` |
27//! | [`Result`] | Encodes `T` if [`Ok`], or bubbles up `E` on [`Err`] |
28//! | [`Cond`] | Conditionally encodes an encodable if the given predicate is true |
29//! | [`Flags`] | Encodes a set of bit flags as a byte |
30//! | [`LE`] | Encodes a number in little-endian order. |
31//! | [`BE`] | Encodes a number in big-endian order. |
32//! | [`Separated`] | Encodes a sequence of encodables separated by a delimiter. |
33//! | [`LengthPrefix`] | Encodes a value after its size. |
34//! | [`Iter`] | Encodes an iterator of encodables as a sequence. |
35#![cfg_attr(
36 feature = "alloc",
37 doc = r#"## alloc encodables (requires `alloc` feature)
38
39| Type| Description |
40|-------------|-------------------------------------------------------------------|
41| [`Vec<u8>`] | Encodes a vector of bytes |
42| [`String`] | Encodes a string as a UTF-8 byte sequence |
43| [`CString`](std::ffi::CString) | Encodes a string as a byte sequence with a null terminator (`\0`) |
44| [`Box`] | Encodes a boxed value |
45"#
46)]
47
48mod be;
49mod cond;
50mod flags;
51mod from_error;
52mod iter;
53mod le;
54mod length_prefix;
55mod separated;
56
57pub use be::BE;
58pub use cond::Cond;
59pub use flags::Flags;
60pub use from_error::FromError;
61pub use iter::Iter;
62pub use le::LE;
63pub use length_prefix::LengthPrefix;
64pub use separated::Separated;