pub struct VarInt(/* private fields */);Expand description
An integer less than 2^62
Values of this type are suitable for encoding as QUIC variable-length integer. It would be neat if we could express to Rust that the top two bits are available for use as enum discriminants
Implementations§
Source§impl VarInt
impl VarInt
Sourcepub const fn from_u32(x: u32) -> Self
pub const fn from_u32(x: u32) -> Self
Construct a VarInt infallibly using the largest available type.
Larger values need to use try_from instead.
Sourcepub const fn from_u64(x: u64) -> Option<Self>
pub const fn from_u64(x: u64) -> Option<Self>
Construct from a u64, or None if it exceeds Self::MAX.
Sourcepub const fn from_u128(x: u128) -> Option<Self>
pub const fn from_u128(x: u128) -> Option<Self>
Construct from a u128, or None if it exceeds Self::MAX.
Sourcepub const fn into_inner(self) -> u64
pub const fn into_inner(self) -> u64
Extract the integer value
Sourcepub const fn from_zigzag(signed: i64) -> Result<Self, BoundsExceeded>
pub const fn from_zigzag(signed: i64) -> Result<Self, BoundsExceeded>
Encode a signed i64 as a zigzag-then-unsigned varint: (n << 1) ^ (n >> 63).
Small negative numbers map to small unsigneds (-1 -> 1, 1 -> 2, -2 -> 3, …).
Returns BoundsExceeded if signed is outside [-2^61, 2^61 - 1], since the
zigzag-encoded result must fit in a 62-bit varint.
Source§impl VarInt
impl VarInt
Sourcepub fn decode_quic<R: Buf>(r: &mut R) -> Result<Self, DecodeError>
pub fn decode_quic<R: Buf>(r: &mut R) -> Result<Self, DecodeError>
Decode a QUIC-style varint (2-bit length tag in top bits).
Sourcepub fn encode_quic<W: BufMut>(&self, w: &mut W) -> Result<(), EncodeError>
pub fn encode_quic<W: BufMut>(&self, w: &mut W) -> Result<(), EncodeError>
Encode a QUIC-style varint (2-bit length tag in top bits).