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
impl Protobuf
Sourcepub fn from_input(buf: Vec<u8>) -> Protobuf
pub fn from_input(buf: Vec<u8>) -> Protobuf
Create a Protobuf instance from a byte buffer.
Sourcepub fn decode_varint(&mut self) -> u64
pub fn decode_varint(&mut self) -> u64
Decode a varint from the buffer at the current position.
Sourcepub fn skip(&mut self, t: Type)
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.
Sourcepub fn read_field(&mut self) -> Field
pub fn read_field(&mut self) -> Field
Read a field from the buffer.
Sourcepub fn read_bytes(&mut self) -> Vec<u8>
pub fn read_bytes(&mut self) -> Vec<u8>
Read in bytes from the buffer.
Sourcepub fn read_string(&mut self) -> String
pub fn read_string(&mut self) -> String
Read in a string from the buffer.
Sourcepub fn read_fixed<T>(&mut self) -> Twhere
T: BitCast,
pub fn read_fixed<T>(&mut self) -> Twhere
T: BitCast,
Read in a fixed size value from the buffer.
Sourcepub fn read_varint<T>(&mut self) -> Twhere
T: BitCast,
pub fn read_varint<T>(&mut self) -> Twhere
T: BitCast,
Read in a variable size value from the buffer.
Sourcepub fn read_s_varint<T>(&mut self) -> T
pub fn read_s_varint<T>(&mut self) -> T
Read in a signed variable size value from the buffer.
§Panics
Panics if the conversion from i64
to T
fails.
Sourcepub fn read_packed<T>(&mut self) -> Vec<T>where
T: BitCast,
pub fn read_packed<T>(&mut self) -> Vec<T>where
T: BitCast,
Read in a packed value from the buffer.
Sourcepub fn read_s_packed<T>(&mut self) -> Vec<T>
pub fn read_s_packed<T>(&mut self) -> Vec<T>
Read in a signed packed value from the buffer.
Sourcepub fn read_fields<T: ProtoRead>(&mut self, t: &mut T, end: Option<usize>)
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.
Sourcepub fn read_message<T: ProtoRead>(&mut self, t: &mut T)
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.
Sourcepub fn write_varint<T: BitCast>(&mut self, val: T)
pub fn write_varint<T: BitCast>(&mut self, val: T)
Write a u64 to the buffer.
Sourcepub fn write_s_varint(&mut self, val: i64)
pub fn write_s_varint(&mut self, val: i64)
Write an i64 to the buffer.
Sourcepub fn write_fixed<T>(&mut self, val: T)where
T: BitCast,
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.
Sourcepub fn write_field(&mut self, tag: u64, type: Type)
pub fn write_field(&mut self, tag: u64, type: Type)
write a field of “tag” and “type” to the buffer.
Sourcepub fn write_length_varint(&mut self, tag: u64, val: usize)
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.
Sourcepub fn write_varint_field<T>(&mut self, tag: u64, val: T)where
T: BitCast,
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.
Sourcepub fn write_s_varint_field<T>(&mut self, tag: u64, val: T)
pub fn write_s_varint_field<T>(&mut self, tag: u64, val: T)
write a signed variable sized number into to the buffer.
Sourcepub fn write_packed_varint<T>(&mut self, tag: u64, val: &[T])
pub fn write_packed_varint<T>(&mut self, tag: u64, val: &[T])
write a vector packed variable sized number, bool, or enum into to the buffer.
Sourcepub fn write_packed_s_varint<T>(&mut self, tag: u64, val: &[T])
pub fn write_packed_s_varint<T>(&mut self, tag: u64, val: &[T])
write a vector packed signed variable sized number into to the buffer.
Sourcepub fn write_fixed_field<T>(&mut self, tag: u64, val: T)
pub fn write_fixed_field<T>(&mut self, tag: u64, val: T)
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.
Sourcepub fn write_string(&mut self, val: &str)
pub fn write_string(&mut self, val: &str)
write only the string to the buffer
Sourcepub fn write_string_field(&mut self, tag: u64, val: &str)
pub fn write_string_field(&mut self, tag: u64, val: &str)
write a string into to the buffer.
Sourcepub fn write_bytes_field(&mut self, tag: u64, val: &[u8])
pub fn write_bytes_field(&mut self, tag: u64, val: &[u8])
write a byte array into to the buffer.
Sourcepub fn write_message<T: ProtoWrite>(&mut self, tag: u64, t: &T)
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.
Sourcepub fn write_fields<T: ProtoWrite>(&mut self, t: &T)
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.