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
136
137
138
139
140
//! [`crate::impl_bitflags!`] — opt-in [`RawRepr`](crate::RawRepr),
//! [`IntoByteArray`](crate::IntoByteArray), and endian-conversion impls for
//! a type implementing [`bitflags::Flags`] (requires the `bitflags` feature).
//!
//! The wire format is identical to the underlying `Flags::Bits` integer (little-endian by
//! default, same as a bare integer field). Decoding uses `Flags::from_bits_retain`, so bits
//! with no matching flag are preserved rather than rejected — matching `bitflags`'s own
//! philosophy, and staying forward-compatible with wire data written by a version of the
//! flags type that defines more bits than the reader knows about. Decoding is therefore
//! infallible.
//!
//! ## Why a macro instead of a blanket impl
//!
//! `impl<T: bitflags::Flags> RawRepr for T` looks natural but does not compile: Rust's
//! coherence checker treats an unconstrained blanket impl over a foreign trait as
//! potentially overlapping with every other impl in the crate (e.g. `Writable for String`),
//! since it cannot prove `bitflags::Flags` will never be implemented for `String` or `Option`
//! upstream. [`crate::impl_bitflags!`] sidesteps this by expanding to impls for the one concrete
//! named type passed to it, exactly like this crate's own internal `impl_byte_array_via_raw!`
//! does for named types such as `Ipv4Addr`.
//!
//! Once invoked for a type, that type gets fixed-size-path support
//! (`IntoByteArray`/`FromByteArray`) and, transitively through the crate's existing blanket
//! impls over `RawRepr`/`TryFromRawRepr`, `io_only` support on every enabled I/O pipeline
//! (`std`, `tokio`, `embedded-io`, `embedded-io-async`) — no per-pipeline code needed.
//!
//! ```
//! # #[cfg(feature = "bitflags")] {
//! bitflags::bitflags! {
//! #[derive(Debug, Clone, Copy, PartialEq, Eq)]
//! struct Perms: u8 {
//! const READ = 0b001;
//! const WRITE = 0b010;
//! }
//! }
//! byteable::impl_bitflags!(Perms);
//!
//! use byteable::{IntoByteArray, FromByteArray};
//! let bytes = Perms::READ.into_byte_array();
//! assert_eq!(Perms::from_byte_array(bytes), Perms::READ);
//! # }
//! ```
//!
//! ## `u8`/`i8`-backed flags and [`crate::impl_bitflags_endian!`]
//!
//! [`crate::impl_bitflags!`] alone is enough for every field usage except one: explicit
//! `#[byteable(little_endian)]` / `#[byteable(big_endian)]` field attributes, which need
//! [`HasEndianRepr`](crate::HasEndianRepr). That trait is only implemented for multi-byte
//! integers in this crate — a bare `u8` field can't use those attributes either, since a
//! single byte has no byte order — so a flags type backed by `u8`/`i8` inherits that same
//! restriction. For a flags type backed by a multi-byte integer (`u16`/`u32`/`u64`/`u128`)
//! where you do want those attributes, additionally invoke [`crate::impl_bitflags_endian!`].
/// Implements [`RawRepr`](crate::RawRepr), [`TryFromRawRepr`](crate::TryFromRawRepr),
/// [`IntoByteArray`](crate::IntoByteArray), and [`FromByteArray`](crate::FromByteArray) for one
/// or more types implementing [`bitflags::Flags`], so they can be used as fixed-size fields
/// and (via this crate's blanket impls) on every enabled I/O pipeline. Works for any `Bits`
/// width, including `u8`/`i8`. See the [module docs](self) for why this is a macro rather
/// than a blanket impl, and [`crate::impl_bitflags_endian!`] for multi-byte-backed flags that also
/// need explicit little/big-endian field attributes.
/// Additionally implements [`HasEndianRepr`](crate::HasEndianRepr) and
/// [`FromEndianRepr`](crate::FromEndianRepr) for one or more types implementing
/// [`bitflags::Flags`], enabling `#[byteable(little_endian)]` / `#[byteable(big_endian)]` on
/// fields of that type. Only compiles for flags backed by a multi-byte integer (`u16` and
/// wider) — see the [module docs](self). Requires [`crate::impl_bitflags!`] to also be invoked for
/// the same type.