tiny_varint/
lib.rs

1#![no_std]
2
3//! # tiny-varint
4//!
5//! A high-performance variable-length integer (VarInt) encoding/decoding library, 
6//! fully compatible with no_std environments.
7//!
8//! VarInt is an efficient way to encode integers into byte sequences, using fewer 
9//! bytes for smaller values. It's widely used in protocol buffers, data compression, 
10//! and network transmission scenarios.
11//!
12//! ## Features
13//!
14//! * **Generic Integer Support**: Works with all integer types (u8-u128, i8-i128)
15//! * **Batch Processing API**: Efficiently handle multiple values with state management
16//! * **Iterator-based API**: Memory-efficient processing using iterator methods
17//! * **Basic Encoding Functions**: Low-level functions for direct use
18//! * **ZigZag Support**: Efficient encoding of signed integers
19//! * **Unified Value Type**: VarintValue enum for type-aware encoding/decoding
20//! * **No-std Compatible**: Works in embedded environments
21//!
22//! ## Usage Examples
23//!
24//! ### Generic API with different integer types
25//! 
26//! ```rust
27//! use tiny_varint::{encode, decode};
28//! 
29//! // Works with any integer type
30//! let mut buf = [0u8; 10];
31//! 
32//! // Use with u32
33//! let bytes_written = encode(42u32, &mut buf).unwrap();
34//! let (value, bytes_read) = decode::<u32>(&buf).unwrap();
35//! assert_eq!(value, 42u32);
36//! 
37//! // Use with i16
38//! let bytes_written = encode(-42i16, &mut buf).unwrap();
39//! let (value, bytes_read) = decode::<i16>(&buf).unwrap();
40//! assert_eq!(value, -42i16);
41//! ```
42//!
43//! ### Batch API
44//! 
45//! ```rust
46//! use tiny_varint::{VarIntEncoder, VarIntDecoder};
47//! 
48//! // Encode multiple values
49//! let values = [1u64, 127, 128, 16383, 16384];
50//! let mut buffer = [0u8; 100];
51//! 
52//! let mut encoder = VarIntEncoder::new(&mut buffer);
53//! let bytes_written = encoder.write_batch(&values).unwrap();
54//! 
55//! // Decode multiple values
56//! let mut decoded = [0u64; 5];
57//! let mut decoder = VarIntDecoder::new(&buffer[..bytes_written]);
58//! decoder.read_batch(&mut decoded).unwrap();
59//! ```
60//! 
61//! ### Iterator-based API
62//! 
63//! ```rust
64//! use tiny_varint::{bytes_of, values_from};
65//! 
66//! // Encode a value into bytes using iterator methods
67//! let value = 16384u64;
68//! for byte in bytes_of(value) {
69//!     // Process each byte individually
70//!     println!("{:02X}", byte);
71//! }
72//! 
73//! // Decode values from a byte buffer using iterator methods
74//! let buffer = [0x80, 0x80, 0x01]; // Encoded value 16384
75//! for result in values_from::<u64>(&buffer) {
76//!     match result {
77//!         Ok(value) => println!("Decoded: {}", value),
78//!         Err(e) => println!("Error: {:?}", e),
79//!     }
80//! }
81//! ```
82//!
83//! ### VarintValue for mixed type data
84//!
85//! ```rust
86//! use tiny_varint::{VarintValue, varint};
87//!
88//! // Create values of different types
89//! let values = [
90//!     varint!(u32: 42),
91//!     varint!(i16: -100),
92//!     varint!(u64: 1000000)
93//! ];
94//!
95//! // Serialize each value
96//! let mut buffer = [0u8; 100];
97//! let mut pos = 0;
98//!
99//! for value in &values {
100//!     let bytes_written = value.to_bytes(&mut buffer[pos..]).unwrap();
101//!     pos += bytes_written;
102//! }
103//!
104//! // Deserialize values
105//! let mut read_pos = 0;
106//! let mut results = Vec::new();
107//!
108//! while read_pos < pos {
109//!     let (value, bytes_read) = VarintValue::from_bytes(&buffer[read_pos..pos]).unwrap();
110//!     results.push(value);
111//!     read_pos += bytes_read;
112//! }
113//!
114//! // Values are preserved with their original types
115//! assert_eq!(results[0], varint!(u32: 42));
116//! assert_eq!(results[1], varint!(i16: -100));
117//! assert_eq!(results[2], varint!(u64: 1000000));
118//! ```
119
120// Import zigzag-rs for ZigZag encoding/decoding
121extern crate zigzag_rs;
122
123// Define modules
124mod error;
125mod traits;
126mod encoding;
127mod batch;
128mod iter;
129mod zigzag;
130mod value;
131#[cfg(test)]
132mod tests;
133
134// Re-export all public items
135pub use error::Error;
136pub use traits::VarInt;
137pub use encoding::{encode, decode, varint_size};
138pub use zigzag::{ZigZag, encode_zigzag, decode_zigzag};
139pub use batch::{VarIntEncoder, VarIntDecoder, encode_batch, decode_batch};
140pub use iter::{VarIntBytesIter, VarIntValuesIter, bytes_of, values_from};
141pub use value::VarintValue;
142// varint! macro is re-exported via #[macro_export]