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
//! # tiny-varint
//!
//! A high-performance variable-length integer (VarInt) encoding/decoding library,
//! fully compatible with no_std environments.
//!
//! VarInt is an efficient way to encode integers into byte sequences, using fewer
//! bytes for smaller values. It's widely used in protocol buffers, data compression,
//! and network transmission scenarios.
//!
//! ## Features
//!
//! * **Generic Integer Support**: Works with all integer types (u8-u128, i8-i128)
//! * **Batch Processing API**: Efficiently handle multiple values with state management
//! * **Iterator-based API**: Memory-efficient processing using iterator methods
//! * **Basic Encoding Functions**: Low-level functions for direct use
//! * **ZigZag Support**: Efficient encoding of signed integers
//! * **Unified Value Type**: VarintValue enum for type-aware encoding/decoding
//! * **No-std Compatible**: Works in embedded environments
//!
//! ## Usage Examples
//!
//! ### Generic API with different integer types
//!
//! ```rust
//! use tiny_varint::{encode, decode};
//!
//! // Works with any integer type
//! let mut buf = [0u8; 10];
//!
//! // Use with u32
//! let bytes_written = encode(42u32, &mut buf).unwrap();
//! let (value, bytes_read) = decode::<u32>(&buf).unwrap();
//! assert_eq!(value, 42u32);
//!
//! // Use with i16
//! let bytes_written = encode(-42i16, &mut buf).unwrap();
//! let (value, bytes_read) = decode::<i16>(&buf).unwrap();
//! assert_eq!(value, -42i16);
//! ```
//!
//! ### Batch API
//!
//! ```rust
//! use tiny_varint::{VarIntEncoder, VarIntDecoder};
//!
//! // Encode multiple values
//! let values = [1u64, 127, 128, 16383, 16384];
//! let mut buffer = [0u8; 100];
//!
//! let mut encoder = VarIntEncoder::new(&mut buffer);
//! let bytes_written = encoder.write_batch(&values).unwrap();
//!
//! // Decode multiple values
//! let mut decoded = [0u64; 5];
//! let mut decoder = VarIntDecoder::new(&buffer[..bytes_written]);
//! decoder.read_batch(&mut decoded).unwrap();
//! ```
//!
//! ### Iterator-based API
//!
//! ```rust
//! use tiny_varint::{bytes_of, values_from};
//!
//! // Encode a value into bytes using iterator methods
//! let value = 16384u64;
//! for byte in bytes_of(value) {
//! // Process each byte individually
//! println!("{:02X}", byte);
//! }
//!
//! // Decode values from a byte buffer using iterator methods
//! let buffer = [0x80, 0x80, 0x01]; // Encoded value 16384
//! for result in values_from::<u64>(&buffer) {
//! match result {
//! Ok(value) => println!("Decoded: {}", value),
//! Err(e) => println!("Error: {:?}", e),
//! }
//! }
//! ```
//!
//! ### VarintValue for mixed type data
//!
//! ```rust
//! use tiny_varint::{VarintValue, varint};
//!
//! // Create values of different types
//! let values = [
//! varint!(u32: 42),
//! varint!(i16: -100),
//! varint!(u64: 1000000)
//! ];
//!
//! // Serialize each value
//! let mut buffer = [0u8; 100];
//! let mut pos = 0;
//!
//! for value in &values {
//! let bytes_written = value.to_bytes(&mut buffer[pos..]).unwrap();
//! pos += bytes_written;
//! }
//!
//! // Deserialize values
//! let mut read_pos = 0;
//! let mut results = Vec::new();
//!
//! while read_pos < pos {
//! let (value, bytes_read) = VarintValue::from_bytes(&buffer[read_pos..pos]).unwrap();
//! results.push(value);
//! read_pos += bytes_read;
//! }
//!
//! // Values are preserved with their original types
//! assert_eq!(results[0], varint!(u32: 42));
//! assert_eq!(results[1], varint!(i16: -100));
//! assert_eq!(results[2], varint!(u64: 1000000));
//! ```
// Import zigzag-rs for ZigZag encoding/decoding
extern crate zigzag_rs;
// Define modules
// Re-export all public items
pub use Error;
pub use VarInt;
pub use ;
pub use ;
pub use ;
pub use ;
pub use VarintValue;
// varint! macro is re-exported via #[macro_export]