pub struct Varint(/* private fields */);Expand description
A deserialized varint value.
This type represents the decoded 8-byte value from serialized bytes to protobuf integer types.
Implementations§
Source§impl Varint
impl Varint
Sourcepub fn new(bytes: [u8; 8]) -> Self
pub fn new(bytes: [u8; 8]) -> Self
Create a new Varint from raw bytes.
The bytes given are, essentially, a little-endian encoded u64. Note that this is NOT the “protobuf encoded” varint bytes.
Sourcepub fn as_bytes(&self) -> &[u8; 8]
pub fn as_bytes(&self) -> &[u8; 8]
Get the underlying byte array, the little-endian encoded u64. Note that this is NOT the “protobuf encoded” varint bytes.
Sourcepub fn from_uint64(value: u64) -> Self
pub fn from_uint64(value: u64) -> Self
Create a Varint from u64, assuming UInt64 protobuf type.
Sourcepub fn from_uint32(value: u32) -> Self
pub fn from_uint32(value: u32) -> Self
Create a Varint from u32, assuming UInt32 protobuf type.
Sourcepub fn from_sint64(value: i64) -> Self
pub fn from_sint64(value: i64) -> Self
Create a Varint from i64, assuming SInt64 protobuf type.
Sourcepub fn from_sint32(value: i32) -> Self
pub fn from_sint32(value: i32) -> Self
Create a Varint from i32, assuming SInt32 protobuf type.
Sourcepub fn from_int64(value: i64) -> Self
pub fn from_int64(value: i64) -> Self
Create a Varint from i64, assuming Int64 protobuf type.
Sourcepub fn from_int32(value: i32) -> Self
pub fn from_int32(value: i32) -> Self
Create a Varint from i32, assuming Int32 protobuf type.
Sourcepub fn try_to_uint32(&self) -> Result<u32>
pub fn try_to_uint32(&self) -> Result<u32>
Convert to u32, assuming UInt32 protobuf type.
Returns an error if the value is out of range for u32.
Sourcepub fn try_to_sint32(&self) -> Result<i32>
pub fn try_to_sint32(&self) -> Result<i32>
Convert to i32, assuming SInt32 protobuf type.
Returns an error if the value is out of range for i32.
Sourcepub fn try_to_int32(&self) -> Result<i32>
pub fn try_to_int32(&self) -> Result<i32>
Convert to i32, assuming Int32 protobuf type.
Returns an error if the value is out of range for i32.
Sourcepub fn varint_size(&self) -> usize
pub fn varint_size(&self) -> usize
Get the size of this varint when encoded as a varint.
This method calculates the exact number of bytes needed to encode the underlying value as a protobuf varint.
Sourcepub fn encode(&self) -> ([u8; 10], usize)
pub fn encode(&self) -> ([u8; 10], usize)
Encode this varint as a varint and return the bytes with count.
Returns a tuple of (bytes, count) where:
- bytes: fixed-size array containing the encoded varint
- count: actual number of bytes used (1-MAX_VARINT_BYTES)
§Example
use ::protobuf_core::Varint;
let varint = Varint::from_uint64(150);
let (bytes, count) = varint.encode();
assert_eq!(count, 2);
assert_eq!(&bytes[..count], &[0x96, 0x01]);