pub struct VarInt;
Expand description
CompactSize Unsigned Integers
The raw transaction format and several peer-to-peer network messages use a type of variable-length integer to indicate the number of bytes in a following piece of data.
Bitcoin Core code and this document refers to these variable length integers as compactSize. Many other documents refer to them as var_int or varInt, but this risks conflation with other variable-length integer encodings—such as the CVarInt class used in Bitcoin Core for serializing data to disk. Because it’s used in the transaction format, the format of compactSize unsigned integers is part of the consensus rules.
https://developer.bitcoin.org/reference/transactions.html#compactsize-unsigned-integers
https://learnmeabitcoin.com/technical/varint
Implementations§
Source§impl VarInt
impl VarInt
Sourcepub fn encode(size: u64) -> Result<Vec<u8>, Error>
pub fn encode(size: u64) -> Result<Vec<u8>, Error>
For numbers from 0 to 252, compactSize unsigned integers look like regular unsigned integers. For other numbers up to 0xffffffffffffffff, a byte is prefixed to the number to indicate its length—but otherwise the numbers look like regular unsigned integers in little-endian order.
Sourcepub fn decode(bytes: &[u8]) -> Result<u64, Error>
pub fn decode(bytes: &[u8]) -> Result<u64, Error>
For numbers from 0 to 252, compactSize unsigned integers look like regular unsigned integers. For other numbers up to 0xffffffffffffffff, a byte is prefixed to the number to indicate its length—but otherwise the numbers look like regular unsigned integers in little-endian order.