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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// SPDX-License-Identifier: Apache-2.0
use crateError;
use decode;
/// Trait for fallibly decoding values from byte slices.
///
/// This trait provides zero-copy parsing of varint-encoded values from byte slices,
/// returning both the decoded value and the remaining unconsumed bytes. This design
/// enables efficient sequential parsing of multiple values from a single buffer.
///
/// # Lifetime Parameter
///
/// The `'a` lifetime parameter ties the returned slice reference to the input buffer,
/// preventing dangling references and enabling zero-copy parsing.
///
/// # Error Handling
///
/// Decoding can fail for several reasons:
/// - Insufficient bytes in the input
/// - Invalid varint encoding
/// - Truncated data
///
/// Errors are returned as structured [`Error`] types with proper
/// source chains for debugging.
///
/// # Performance
///
/// Decoding is highly optimized:
/// - Zero allocations (returns slice references)
/// - No data copying
/// - Constant-time validation
///
/// # Thread Safety
///
/// This trait is `Send + Sync` safe. Implementations are stateless and can be
/// called concurrently from multiple threads.
///
/// # Examples
///
/// ## Basic Decoding
///
/// ```rust
/// use multi_trait::TryDecodeFrom;
///
/// // Decode a single value
/// let bytes = vec![42];
/// let (value, remaining) = u8::try_decode_from(&bytes).unwrap();
/// assert_eq!(value, 42);
/// assert!(remaining.is_empty());
/// ```
///
/// ## Sequential Decoding
///
/// ```rust
/// use multi_trait::TryDecodeFrom;
///
/// // Decode multiple values from one buffer
/// let bytes = vec![0x01, 0x02, 0x03];
/// let (first, rest) = u8::try_decode_from(&bytes).unwrap();
/// let (second, rest) = u8::try_decode_from(rest).unwrap();
/// let (third, rest) = u8::try_decode_from(rest).unwrap();
///
/// assert_eq!(first, 1);
/// assert_eq!(second, 2);
/// assert_eq!(third, 3);
/// assert!(rest.is_empty());
/// ```
///
/// ## Error Handling
///
/// ```rust
/// use multi_trait::{TryDecodeFrom, Error};
///
/// // Handle decode errors
/// let empty: &[u8] = &[];
/// match u32::try_decode_from(empty) {
/// Ok((value, _)) => println!("Decoded: {}", value),
/// Err(Error::UnsignedVarintDecode { .. }) => {
/// // Expected for empty input
/// }
/// Err(e) => panic!("Unexpected error: {}", e),
/// }
/// ```
///
/// # Implemented For
///
/// - `bool`: Decodes 0 as false, non-zero as true
/// - `u8`, `u16`, `u32`, `u64`, `u128`: Variable-length decoding
/// - `usize`: Platform-dependent (32-bit or 64-bit)
///
/// # Length Bounds
///
/// Integer decoders delegate to the `unsigned-varint` crate, which enforces
/// type-specific maximum byte counts (10 bytes for `u64`, 19 for `u128`).
/// These bounds prevent unbounded varint expansion but do **not** cap the
/// size of higher-level structures built on top of varints. Callers that
/// decode length-prefixed payloads (e.g. `Varbytes` in `multi-util`) should
/// enforce their own upper bound on the claimed length before allocating.
/// Macro to implement `TryDecodeFrom` for unsigned integer types using varint decoding.
///
/// This macro eliminates code duplication by generating identical implementations
/// for different numeric types. Each implementation:
/// 1. Calls the appropriate decode function from `unsigned_varint`
/// 2. Maps any decode error to a properly structured Error with source chain
/// 3. Returns the decoded value and remaining bytes
///
/// # Usage
///
/// ```text
/// impl_try_decode_from! {
/// u8 => u8;
/// u16 => u16;
/// }
/// ```
///
/// # Error Handling
///
/// The macro properly constructs errors with source chains for debugging,
/// following Rust error handling best practices.
///
/// # Hygiene
///
/// This macro uses fully qualified paths to ensure proper hygiene and avoid
/// namespace collisions with user code.
/// Try to decode a varuint encoded bool
// Implement TryDecodeFrom for all unsigned integer types using the macro
impl_try_decode_from!
/// Decode a fixed-length byte array (reads N bytes; used for BLS share identifiers).