1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Byte-level serialization and deserialization for Rust types.
//!
//! `byteable` provides two complementary paths for working with binary data:
//!
//! - **Fixed-size path** - For types whose wire size is known at compile time. Derive
//! [`Byteable`] and get zero-copy [`ToByteArray::to_byte_array`] /
//! [`TryFromByteArray::try_from_byte_array`] with a compile-time [`ToByteArray::BYTE_SIZE`]
//! constant. The derive macro generates a `#[repr(C, packed)]` raw struct and uses
//! `transmute`, so no heap allocation or per-field iteration is required.
//!
//! - **Dynamic path** - For types that contain variable-length data (strings, vecs, maps). Add
//! `#[byteable(io_only)]` to derive [`io::Readable`] / [`io::Writable`] instead, which stream data
//! through any [`std::io::Read`] / [`std::io::Write`], or their
//! `embedded-io`/`embedded-io-async`/tokio counterparts, depending on which features are
//! enabled.
//!
//! # Quick Start
//!
//! ## Fixed-size struct
//!
//! ```rust
//! use byteable::{Byteable, ToByteArray, TryFromByteArray};
//!
//! #[derive(Byteable)]
//! struct Point3D {
//! x: f32,
//! y: f32,
//! z: f32,
//! }
//!
//! let p = Point3D { x: 1.0, y: 2.0, z: 3.0 };
//! let bytes: [u8; 12] = p.to_byte_array();
//! let p2 = Point3D::try_from_byte_array(bytes).unwrap();
//! assert_eq!(p.x, p2.x);
//! ```
//!
//! ## Dynamic struct (I/O streaming)
//!
//! This example needs the `std` feature (for `byteable::io` and `String`) on top of `derive`;
//! the body is a no-op without it, since a `no_std` build (e.g. `embedded-io` alone) can't
//! compile a `std::io`-based example at all.
//!
//! ```rust
//! # #[cfg(feature = "std")]
//! # fn main() {
//! use byteable::Byteable;
//! use byteable::io::{Writable, Readable, WriteValue, ReadValue};
//!
//! #[derive(Byteable)]
//! #[byteable(io_only)]
//! struct Waypoint {
//! id: u32,
//! label: String,
//! }
//!
//! let wp = Waypoint { id: 1, label: "home".into() };
//! let mut buf = Vec::new();
//! buf.write_value(&wp).unwrap();
//! let wp2 = std::io::Cursor::new(&buf).read_value::<Waypoint>().unwrap();
//! assert_eq!(wp.id, wp2.id);
//! # }
//! # #[cfg(not(feature = "std"))]
//! # fn main() {}
//! ```
//!
//! # Feature Flags
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `derive` | yes | `#[derive(Byteable)]` proc-macro |
//! | `std` | yes | [`io::Readable`] / [`io::Writable`] I/O traits and `std` type impls |
//! | `tokio` | no | Async I/O traits via tokio |
//! | `ordered-float` | no | Impls for `OrderedFloat<T>` and `NotNan<T>` |
//! | `bitflags` | no | `impl_bitflags!`/`impl_bitflags_endian!` opt-in macros for `bitflags::Flags` types |
//! | `heapless` | no | `io_only` support for `heapless`'s fixed-capacity collections, on every enabled I/O pipeline |
//! | `arrayvec` | no | `io_only` support for `arrayvec::ArrayVec`/`ArrayString`, on every enabled I/O pipeline |
//! | `tinyvec` | no | `io_only` support for `tinyvec::ArrayVec`, on every enabled I/O pipeline |
//! | `defmt` | no | `defmt::Format` impls for this crate's own error/wrapper types (`DecodeError`, `LittleEndian<T>`, `BigEndian<T>`, `eio::EioReadableError<E>`, `eio::EioReadExactError<E>`) |
//! | `alloc` | no (implied by `std`) | `alloc`-backed collection types over the `eio` and `eio_async` modules |
//! | `embedded-io` | no | `eio` module: `embedded-io`-based (sync) I/O traits for `no_std` targets |
//! | `embedded-io-async` | no | `eio_async` module: `embedded-io-async`-based (async) I/O traits for `no_std` targets |
//! | `all` | no | All of the above |
extern crate self as byteable; // used to resolve derive macros in examples etc.
pub use *;
pub use Byteable;
/// Hidden re-export of `tokio` so derive-generated code can name its traits without the
/// downstream crate needing `tokio` as a direct dependency of its own.
pub use tokio as __tokio;
extern crate alloc;
/// Hidden re-export of `bitflags` so [`impl_bitflags!`] can name its traits without the
/// downstream crate needing `bitflags` as a direct dependency of its own.
pub use bitflags as __bitflags;