pub fn decode_zigzag<T: ZigZag>(buf: &[u8]) -> Result<(T, usize), Error>Expand description
Decode a signed integer from a varint-encoded zigzag value
Returns the decoded value and the number of bytes read
§Arguments
buf- The input buffer containing the zigzag varint encoded value
§Errors
- Returns
Error::InputTooShortif the input buffer is insufficient - Returns
Error::InvalidEncodingif the varint encoding is invalid - Returns
Error::Overflowif an overflow occurs during decoding
Examples found in repository?
examples/basic_usage.rs (line 52)
16fn main() {
17 println!("=== tiny-varint Basic Example (no-alloc version) ===\n");
18
19 // 1. Single unsigned integer encoding/decoding
20 println!("1. Encoding and decoding single unsigned integers:");
21 let values = [0u64, 127, 128, 16383, 16384, 2097151];
22
23 for &value in &values {
24 let mut buf = [0u8; 10];
25 let bytes_written = encode(value, &mut buf).unwrap();
26
27 print!(" Value {} encoded as {} bytes: [ ", value, bytes_written);
28 for i in 0..bytes_written {
29 print!("{:#04x} ", buf[i]);
30 }
31 println!("]");
32
33 let (decoded, bytes_read) = decode::<u64>(&buf).unwrap();
34 println!(" Decoded: {} (read {} bytes)", decoded, bytes_read);
35 println!();
36 }
37
38 // 2. Signed integer zigzag encoding/decoding
39 println!("\n2. ZigZag encoding and decoding of signed integers:");
40 let signed_values = [0i32, 1, -1, 2, -2, 127, -127, 128, -128];
41
42 for &value in &signed_values {
43 let mut buf = [0u8; 10];
44 let bytes_written = encode_zigzag(value, &mut buf).unwrap();
45
46 print!(" Value {} encoded as {} bytes: [ ", value, bytes_written);
47 for i in 0..bytes_written {
48 print!("{:#04x} ", buf[i]);
49 }
50 println!("]");
51
52 let (decoded, bytes_read) = decode_zigzag::<i32>(&buf).unwrap();
53 println!(" Decoded: {} (read {} bytes)", decoded, bytes_read);
54 println!();
55 }
56
57 // 3. Batch processing using VarIntEncoder/VarIntDecoder
58 println!("\n3. Batch processing using VarIntEncoder/VarIntDecoder:");
59 let batch_values = [1u64, 127, 128, 16383, 16384, 2097151];
60 let mut buffer = [0u8; 100];
61
62 // Using encoder
63 let mut encoder = VarIntEncoder::new(&mut buffer);
64 let bytes_written = encoder.write_batch(&batch_values).unwrap();
65
66 println!(" Encoded {} values using {} bytes", batch_values.len(), bytes_written);
67
68 print!(" Encoded bytes: [ ");
69 for i in 0..bytes_written {
70 print!("{:#04x} ", buffer[i]);
71 }
72 println!("]");
73
74 // Using decoder
75 let mut decoder = VarIntDecoder::new(&buffer[..bytes_written]);
76 let mut decoded = [0u64; 6];
77 let count = decoder.read_batch(&mut decoded).unwrap();
78
79 print!(" Decoded values: [ ");
80 for i in 0..count {
81 print!("{} ", decoded[i]);
82 }
83 println!("]");
84 println!(" Read {} bytes in total", decoder.position());
85
86 // 4. Using different integer types
87 println!("\n4. Using different integer types:");
88
89 // Using u64 type
90 let value = 16384u64;
91 let mut buf = [0u8; 10];
92 let size = encode(value, &mut buf).unwrap();
93
94 print!(" u64 value {} encoded as {} bytes: [ ", value, size);
95 for i in 0..size {
96 print!("{:#04x} ", buf[i]);
97 }
98 println!("]");
99
100 let (decoded, bytes_read) = decode::<u64>(&buf).unwrap();
101 println!(" Decoded: {} (read {} bytes)", decoded, bytes_read);
102
103 // Using u32 type
104 let u32_value = 42u32;
105 let mut buf = [0u8; 10];
106 let size = encode(u32_value, &mut buf).unwrap();
107
108 print!(" u32 value {} encoded as {} bytes: [ ", u32_value, size);
109 for i in 0..size {
110 print!("{:#04x} ", buf[i]);
111 }
112 println!("]");
113
114 let (decoded, bytes_read) = decode::<u32>(&buf).unwrap();
115 println!(" Decoded: {} (read {} bytes)", decoded, bytes_read);
116
117 // Using i16 type
118 let i16_value = -256i16;
119 let size = encode(i16_value, &mut buf).unwrap();
120
121 print!(" i16 value {} encoded as {} bytes: [ ", i16_value, size);
122 for i in 0..size {
123 print!("{:#04x} ", buf[i]);
124 }
125 println!("]");
126
127 let (decoded, bytes_read) = decode::<i16>(&buf).unwrap();
128 println!(" Decoded: {} (read {} bytes)", decoded, bytes_read);
129
130 // 5. Error handling
131 println!("\n5. Error handling:");
132 let large_value: u64 = 0xFFFFFFFFFFFFFFFF; // Requires 10 bytes
133 let mut small_buf = [0u8; 5];
134
135 match encode(large_value, &mut small_buf) {
136 Ok(_) => println!(" Encoding successful (should not reach here)"),
137 Err(Error::BufferTooSmall { needed, actual }) => {
138 println!(" Buffer too small: needed {} bytes, but only had {} bytes", needed, actual);
139 }
140 Err(e) => println!(" Error occurred: {:?}", e),
141 }
142
143 // Create an incomplete varint - flag bit is 1 but no following bytes
144 let incomplete_buf = [0x80];
145
146 match decode::<u64>(&incomplete_buf) {
147 Ok(_) => println!(" Decoding successful (should not reach here)"),
148 Err(Error::InputTooShort) => {
149 println!(" Input too short: high bit is 1 indicating more bytes follow, but data ended");
150 }
151 Err(e) => println!(" Error occurred: {:?}", e),
152 }
153
154 println!("\n=== Example Complete ===");
155}