Struct Protobuf

Source
pub struct Protobuf { /* private fields */ }
Expand description

The Protobuf struct is used to read and write protobuf messages.

§Example

Create a new Protobuf instance:

use pbf_core::Protobuf;

let mut pbf = Protobuf::new();

Create a Protobuf instance from a byte buffer:

use pbf_core::Protobuf;

let mut buf = vec![0x0A, 0x03, 0x74, 0x65, 0x73, 0x74];
let mut pbf = Protobuf::from_input(buf);
// OR use the From trait
// let mut pbf: Protobuf = buf.into();

Implementations§

Source§

impl Protobuf

Source

pub fn new() -> Protobuf

Create a new Protobuf instance.

Source

pub fn from_input(buf: Vec<u8>) -> Protobuf

Create a Protobuf instance from a byte buffer.

Source

pub fn set_pos(&mut self, pos: usize)

Set the position to read from the buffer next.

Source

pub fn get_pos(&self) -> usize

get the current position

Source

pub fn len(&self) -> usize

get the length of the bufer

Source

pub fn is_empty(&self) -> bool

check if the buffer is empty

Source

pub fn decode_varint(&mut self) -> u64

Decode a varint from the buffer at the current position.

Source

pub fn skip(&mut self, t: Type)

After reading a field, you can choose to skip it’s value in the buffer if it is not needed.

Source

pub fn read_field(&mut self) -> Field

Read a field from the buffer.

Source

pub fn read_bytes(&mut self) -> Vec<u8>

Read in bytes from the buffer.

Source

pub fn read_string(&mut self) -> String

Read in a string from the buffer.

Source

pub fn read_fixed<T>(&mut self) -> T
where T: BitCast,

Read in a fixed size value from the buffer.

Source

pub fn read_varint<T>(&mut self) -> T
where T: BitCast,

Read in a variable size value from the buffer.

Source

pub fn read_s_varint<T>(&mut self) -> T
where T: TryFrom<i64>,

Read in a signed variable size value from the buffer.

§Panics

Panics if the conversion from i64 to T fails.

Source

pub fn read_packed<T>(&mut self) -> Vec<T>
where T: BitCast,

Read in a packed value from the buffer.

Source

pub fn read_s_packed<T>(&mut self) -> Vec<T>
where T: TryFrom<i64>,

Read in a signed packed value from the buffer.

Source

pub fn read_fields<T: ProtoRead>(&mut self, t: &mut T, end: Option<usize>)

Read a message from the buffer. This is the alternative to read_message which does the same thing but you may already know the size of the message. The other case is top level data may have fields but no message length.

Source

pub fn read_message<T: ProtoRead>(&mut self, t: &mut T)

Read in an entire message from the buffer. This is usually used to read in a struct or enum.

Source

pub fn write_varint<T: BitCast>(&mut self, val: T)

Write a u64 to the buffer.

Source

pub fn write_s_varint(&mut self, val: i64)

Write an i64 to the buffer.

Source

pub fn write_fixed<T>(&mut self, val: T)
where T: BitCast,

Write a fixed size value to the buffer. This will not compress the value.

Source

pub fn write_field(&mut self, tag: u64, type: Type)

write a field of “tag” and “type” to the buffer.

Source

pub fn write_length_varint(&mut self, tag: u64, val: usize)

write a tag with the size of the buffer to be appended to the internal buffer.

Source

pub fn write_varint_field<T>(&mut self, tag: u64, val: T)
where T: BitCast,

write a variable sized number, bool, or enum into to the buffer.

Source

pub fn write_s_varint_field<T>(&mut self, tag: u64, val: T)
where T: Into<i64>,

write a signed variable sized number into to the buffer.

Source

pub fn write_packed_varint<T>(&mut self, tag: u64, val: &[T])
where T: BitCast + Copy,

write a vector packed variable sized number, bool, or enum into to the buffer.

Source

pub fn write_packed_s_varint<T>(&mut self, tag: u64, val: &[T])
where T: Into<i64> + Copy,

write a vector packed signed variable sized number into to the buffer.

Source

pub fn write_fixed_field<T>(&mut self, tag: u64, val: T)
where T: BitCast + Copy,

write a fixed sized number into to the buffer. No compression is done. Supports 32 and 64 bit numbers.

§Panics

Panics if the size of the type is not 32 or 64 bits.

Source

pub fn write_string(&mut self, val: &str)

write only the string to the buffer

Source

pub fn write_string_field(&mut self, tag: u64, val: &str)

write a string into to the buffer.

Source

pub fn write_bytes_field(&mut self, tag: u64, val: &[u8])

write a byte array into to the buffer.

Source

pub fn write_message<T: ProtoWrite>(&mut self, tag: u64, t: &T)

write a message into to the buffer. The message must implement the ProtoWrite trait. This is usually reserved for structs and enums.

Source

pub fn write_fields<T: ProtoWrite>(&mut self, t: &T)

write a collection of fields into to the buffer. The collection must implement the ProtoWrite trait. This is usually reserved for top level structs and enums.

Source

pub fn take(&mut self) -> Vec<u8>

When done writing to the buffer, call this function to take ownership

Trait Implementations§

Source§

impl Clone for Protobuf

Source§

fn clone(&self) -> Protobuf

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 Protobuf

Source§

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

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

impl Default for Protobuf

Source§

fn default() -> Protobuf

Returns the “default value” for a type. Read more
Source§

impl From<Vec<u8>> for Protobuf

Source§

fn from(buf: Vec<u8>) -> Protobuf

Converts to this type from the input type.

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