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
// SPDX-License-Identifier: Apache-2.0
use ;
use encode;
/// Trait for encoding values into compact varint byte representation.
///
/// This trait provides efficient encoding of numeric values into unsigned varint
/// format, which uses fewer bytes for smaller values. The encoding is deterministic
/// and reversible via [`TryDecodeFrom`](crate::TryDecodeFrom).
///
/// # Varint Format
///
/// The varint (variable-length integer) format uses the most significant bit (MSB)
/// of each byte as a continuation bit:
/// - If MSB is 1, more bytes follow
/// - If MSB is 0, this is the last byte
///
/// This allows small values to use fewer bytes:
/// - Values 0-127: 1 byte
/// - Values 128-16,383: 2 bytes
/// - And so on...
///
/// # Performance
///
/// Encoding is optimized for performance:
/// - Single heap allocation per call
/// - O(1) length calculation
/// - No byte-by-byte copying
///
/// # Thread Safety
///
/// This trait is `Send + Sync` safe. All implementations are stateless and can
/// be called concurrently from multiple threads.
///
/// # Examples
///
/// ```rust
/// use multi_trait::EncodeInto;
///
/// // Small values use minimal space
/// let small = 42u8;
/// let encoded = small.encode_into();
/// assert_eq!(encoded.len(), 1);
///
/// // Larger values use more bytes as needed
/// let large = 256u16;
/// let encoded = large.encode_into();
/// assert!(encoded.len() > 1);
///
/// // Boolean encoding
/// assert_eq!(true.encode_into(), vec![1]);
/// assert_eq!(false.encode_into(), vec![0]);
/// ```
///
/// # Implemented For
///
/// - `bool`: Encoded as 0 (false) or 1 (true)
/// - `u8`, `u16`, `u32`, `u64`, `u128`: Variable-length encoding
/// - `usize`: Platform-dependent (32-bit or 64-bit)
/// Macro to implement `EncodeInto` for unsigned integer types using varint encoding.
///
/// This macro eliminates code duplication by generating identical implementations
/// for different numeric types. Each implementation:
/// 1. Creates an appropriate buffer for the type
/// 2. Encodes the value into the buffer
/// 3. Finds the encoded length by locating the last byte marker
/// 4. Returns a Vec containing only the encoded bytes
///
/// # Usage
///
/// ```text
/// impl_encode_into! {
/// u8 => u8_buffer, u8;
/// u16 => u16_buffer, u16;
/// }
/// ```
///
/// # Hygiene
///
/// This macro uses fully qualified paths to ensure proper hygiene and avoid
/// namespace collisions with user code.
/// Encode a bool into a compact varuint `Vec<u8>`
// Implement EncodeInto for all unsigned integer types using the macro
impl_encode_into!
/// Encode a fixed-length byte array as raw bytes (used for BLS share identifiers).