Varint

Struct Varint 

Source
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

Source

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.

Source

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.

Source

pub fn from_uint64(value: u64) -> Self

Create a Varint from u64, assuming UInt64 protobuf type.

Source

pub fn from_uint32(value: u32) -> Self

Create a Varint from u32, assuming UInt32 protobuf type.

Source

pub fn from_sint64(value: i64) -> Self

Create a Varint from i64, assuming SInt64 protobuf type.

Source

pub fn from_sint32(value: i32) -> Self

Create a Varint from i32, assuming SInt32 protobuf type.

Source

pub fn from_int64(value: i64) -> Self

Create a Varint from i64, assuming Int64 protobuf type.

Source

pub fn from_int32(value: i32) -> Self

Create a Varint from i32, assuming Int32 protobuf type.

Source

pub fn from_bool(value: bool) -> Self

Create a Varint from bool, assuming Bool protobuf type.

Source

pub fn to_uint64(&self) -> u64

Convert to u64, assuming UInt64 protobuf type.

Source

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.

Source

pub fn to_sint64(&self) -> i64

Convert to i64, assuming SInt64 protobuf type.

Source

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.

Source

pub fn to_int64(&self) -> i64

Convert to i64, assuming Int64 protobuf type.

Source

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.

Source

pub fn to_bool(&self) -> bool

Convert to bool, assuming Bool protobuf type.

Source

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.

Source

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]);

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 From<Tag> for Varint

Source§

fn from(tag: Tag) -> 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 TryFrom<Varint> for Tag

Source§

type Error = ProtobufError

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

fn try_from(encoded: Varint) -> Result<Self>

Performs the conversion.
Source§

impl Copy for Varint

Source§

impl Eq for Varint

Source§

impl StructuralPartialEq for Varint

Auto Trait Implementations§

§

impl Freeze for Varint

§

impl RefUnwindSafe for Varint

§

impl Send for Varint

§

impl Sync for Varint

§

impl Unpin for Varint

§

impl UnwindSafe for Varint

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<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.