Skip to main content

VarInt

Struct VarInt 

Source
pub struct VarInt(pub i32);
Expand description

A variable-length encoded 32-bit signed integer, occupying 1 to 5 bytes.

VarInt is the most common type in the Minecraft protocol. It is used for packet IDs, packet lengths, string length prefixes, array lengths, enum discriminants, and many field values. The encoding uses the MSB of each byte as a continuation bit (1 = more bytes follow, 0 = last byte), with the lower 7 bits carrying the value in little-endian order (least significant group first). Negative values use two’s complement and always require 5 bytes.

Tuple Fields§

§0: i32

Implementations§

Source§

impl VarInt

Source

pub const MAX_BYTES: usize = 5

Maximum number of bytes a VarInt can occupy on the wire.

Trait Implementations§

Source§

impl Clone for VarInt

Source§

fn clone(&self) -> VarInt

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for VarInt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decode for VarInt

Decodes a VarInt by reading 1-5 bytes from the buffer.

Reads bytes one at a time, accumulating 7-bit groups until a byte without the continuation bit is found. If more than 5 bytes have the continuation bit set, the value would exceed 32 bits and the decoder returns Error::VarIntTooLarge. If the buffer runs out mid-VarInt, returns Error::BufferUnderflow.

Source§

fn decode(buf: &mut &[u8]) -> Result<Self>

Reads and decodes a VarInt, advancing the cursor past all consumed bytes.

Source§

impl Encode for VarInt

Encodes the VarInt as 1-5 bytes using MSB continuation bit encoding.

Each byte carries 7 bits of the value (least significant first). The MSB is set to 1 if more bytes follow, 0 on the final byte. Negative values are encoded as their unsigned two’s complement representation and always produce 5 bytes.

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the variable-length encoded bytes to the buffer.

Source§

impl EncodedSize for VarInt

Computes the number of bytes this VarInt will occupy when encoded.

Returns 1-5 based on the unsigned magnitude of the value. Small positive values (0-127) take 1 byte, while negative values always take 5 bytes due to two’s complement sign extension.

Source§

impl From<VarInt> for i32

Extracts the inner i32 value from a VarInt.

Source§

fn from(value: VarInt) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for VarInt

Wraps a raw i32 into a VarInt for protocol encoding.

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl Hash for VarInt

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for VarInt

Source§

fn eq(&self, other: &VarInt) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for VarInt

Source§

impl Eq for VarInt

Source§

impl StructuralPartialEq for VarInt

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.