Function read_varint

Source
pub fn read_varint(data: &[u8]) -> Result<(u32, usize)>
Expand description

Read a variable-length integer from a byte slice

Variable-length integers use 7 bits per byte with a continuation bit. This is compatible with protobuf/varint encoding.

§Arguments

  • data - Byte slice to read from

§Returns

  • Tuple of (value, bytes_consumed)

§Errors

  • Returns error if varint is malformed or exceeds 5 bytes

§Example

use tact_parser::utils::read_varint;

let data = [0x08]; // Value 8
let (value, consumed) = read_varint(&data).unwrap();
assert_eq!(value, 8);
assert_eq!(consumed, 1);

let data = [0x96, 0x01]; // Value 150
let (value, consumed) = read_varint(&data).unwrap();
assert_eq!(value, 150);
assert_eq!(consumed, 2);