pub struct VarIntDecoder<'a, T: VarInt = u64> { /* private fields */ }
Expand description
Batch decoder for VarInt values with state management
Implementations§
Source§impl<'a, T: VarInt> VarIntDecoder<'a, T>
impl<'a, T: VarInt> VarIntDecoder<'a, T>
Sourcepub fn new(buf: &'a [u8]) -> Self
pub fn new(buf: &'a [u8]) -> Self
Creates a new decoder with the provided buffer
Examples found in repository?
examples/protocol_serialization.rs (line 81)
80 fn deserialize(bytes: &[u8]) -> Result<(Self, usize), Error> {
81 let mut decoder = VarIntDecoder::<u32>::new(bytes);
82
83 // Decode message ID
84 let message_id = decoder.read()? as u64;
85
86 // Decode temperature
87 let temperature = decoder.read_zigzag::<i16>()?;
88
89 // Decode humidity
90 let humidity = decoder.read_zigzag::<i8>()?;
91
92 // Decode payload length
93 let payload_len = decoder.read()? as usize;
94 if payload_len != 16 {
95 return Err(Error::InvalidEncoding);
96 }
97
98 // Decode payload
99 let current_offset = decoder.position();
100 if current_offset + payload_len > bytes.len() {
101 return Err(Error::InputTooShort);
102 }
103
104 let mut payload = [0u8; 16];
105 payload.copy_from_slice(&bytes[current_offset..current_offset + payload_len]);
106
107 let final_offset = current_offset + payload_len;
108
109 let msg = SimpleMessage {
110 message_id,
111 temperature,
112 humidity,
113 payload,
114 };
115
116 Ok((msg, final_offset))
117 }
More examples
examples/basic_usage.rs (line 75)
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}
Sourcepub fn position(&self) -> usize
pub fn position(&self) -> usize
Gets the current position in the buffer
Examples found in repository?
examples/protocol_serialization.rs (line 99)
80 fn deserialize(bytes: &[u8]) -> Result<(Self, usize), Error> {
81 let mut decoder = VarIntDecoder::<u32>::new(bytes);
82
83 // Decode message ID
84 let message_id = decoder.read()? as u64;
85
86 // Decode temperature
87 let temperature = decoder.read_zigzag::<i16>()?;
88
89 // Decode humidity
90 let humidity = decoder.read_zigzag::<i8>()?;
91
92 // Decode payload length
93 let payload_len = decoder.read()? as usize;
94 if payload_len != 16 {
95 return Err(Error::InvalidEncoding);
96 }
97
98 // Decode payload
99 let current_offset = decoder.position();
100 if current_offset + payload_len > bytes.len() {
101 return Err(Error::InputTooShort);
102 }
103
104 let mut payload = [0u8; 16];
105 payload.copy_from_slice(&bytes[current_offset..current_offset + payload_len]);
106
107 let final_offset = current_offset + payload_len;
108
109 let msg = SimpleMessage {
110 message_id,
111 temperature,
112 humidity,
113 payload,
114 };
115
116 Ok((msg, final_offset))
117 }
More examples
examples/basic_usage.rs (line 84)
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}
Sourcepub fn read(&mut self) -> Result<T, Error>
pub fn read(&mut self) -> Result<T, Error>
Reads a VarInt value from the buffer
Returns the decoded value
Examples found in repository?
examples/protocol_serialization.rs (line 84)
80 fn deserialize(bytes: &[u8]) -> Result<(Self, usize), Error> {
81 let mut decoder = VarIntDecoder::<u32>::new(bytes);
82
83 // Decode message ID
84 let message_id = decoder.read()? as u64;
85
86 // Decode temperature
87 let temperature = decoder.read_zigzag::<i16>()?;
88
89 // Decode humidity
90 let humidity = decoder.read_zigzag::<i8>()?;
91
92 // Decode payload length
93 let payload_len = decoder.read()? as usize;
94 if payload_len != 16 {
95 return Err(Error::InvalidEncoding);
96 }
97
98 // Decode payload
99 let current_offset = decoder.position();
100 if current_offset + payload_len > bytes.len() {
101 return Err(Error::InputTooShort);
102 }
103
104 let mut payload = [0u8; 16];
105 payload.copy_from_slice(&bytes[current_offset..current_offset + payload_len]);
106
107 let final_offset = current_offset + payload_len;
108
109 let msg = SimpleMessage {
110 message_id,
111 temperature,
112 humidity,
113 payload,
114 };
115
116 Ok((msg, final_offset))
117 }
Sourcepub fn read_batch(&mut self, values: &mut [T]) -> Result<usize, Error>
pub fn read_batch(&mut self, values: &mut [T]) -> Result<usize, Error>
Reads a batch of VarInt values into the provided buffer
Returns the number of values read
Examples found in repository?
examples/basic_usage.rs (line 77)
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}
Sourcepub fn read_u64(&mut self) -> Result<u64, Error>
pub fn read_u64(&mut self) -> Result<u64, Error>
Reads a u64 value from the buffer (convenience method)
Returns the decoded value
Sourcepub fn read_u128(&mut self) -> Result<u128, Error>
pub fn read_u128(&mut self) -> Result<u128, Error>
Reads a u128 value from the buffer (convenience method)
Returns the decoded value
Sourcepub fn read_zigzag<S>(&mut self) -> Result<S, Error>
pub fn read_zigzag<S>(&mut self) -> Result<S, Error>
Reads a signed value that was encoded using zigzag encoding
Returns the decoded value
Examples found in repository?
examples/protocol_serialization.rs (line 87)
80 fn deserialize(bytes: &[u8]) -> Result<(Self, usize), Error> {
81 let mut decoder = VarIntDecoder::<u32>::new(bytes);
82
83 // Decode message ID
84 let message_id = decoder.read()? as u64;
85
86 // Decode temperature
87 let temperature = decoder.read_zigzag::<i16>()?;
88
89 // Decode humidity
90 let humidity = decoder.read_zigzag::<i8>()?;
91
92 // Decode payload length
93 let payload_len = decoder.read()? as usize;
94 if payload_len != 16 {
95 return Err(Error::InvalidEncoding);
96 }
97
98 // Decode payload
99 let current_offset = decoder.position();
100 if current_offset + payload_len > bytes.len() {
101 return Err(Error::InputTooShort);
102 }
103
104 let mut payload = [0u8; 16];
105 payload.copy_from_slice(&bytes[current_offset..current_offset + payload_len]);
106
107 let final_offset = current_offset + payload_len;
108
109 let msg = SimpleMessage {
110 message_id,
111 temperature,
112 humidity,
113 payload,
114 };
115
116 Ok((msg, final_offset))
117 }
Auto Trait Implementations§
impl<'a, T> Freeze for VarIntDecoder<'a, T>
impl<'a, T> RefUnwindSafe for VarIntDecoder<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for VarIntDecoder<'a, T>where
T: Send,
impl<'a, T> Sync for VarIntDecoder<'a, T>where
T: Sync,
impl<'a, T> Unpin for VarIntDecoder<'a, T>where
T: Unpin,
impl<'a, T> UnwindSafe for VarIntDecoder<'a, T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more