pub trait ReadExtVarint {
// Required methods
fn read_varint_partial(&mut self) -> Result<DecodeOutcome>;
fn read_varint_resume(
&mut self,
state: DecodeState,
) -> Result<DecodeOutcome>;
fn read_varints(&mut self) -> VarintIterator<Bytes<&mut Self>>
where Self: Read;
// Provided method
fn read_varint(&mut self) -> Result<Option<Varint>> { ... }
}Expand description
Extension trait for reading varints from Read instances.
This trait provides a convenient method to read varints directly from
any type that implements std::io::Read.
§Performance Note
When reading from file handles or network streams, consider wrapping the reader
with BufReader to avoid inefficient byte-by-byte system calls:
use std::io::{BufReader, Read};
use protobuf_core::{DecodeOutcome, ReadExtVarint};
let file = std::fs::File::open("data.bin")?;
let mut reader = BufReader::new(file);
let outcome = reader.read_varint_partial()?;
if let DecodeOutcome::Complete(varint) = outcome {
// use varint
}§Example
use ::std::io::Cursor;
use ::protobuf_core::{DecodeOutcome, ReadExtVarint, Varint};
let data = vec![0x96, 0x01]; // 150 in varint encoding
let mut reader = Cursor::new(data);
let outcome = reader.read_varint_partial().unwrap();
let varint = match outcome {
DecodeOutcome::Complete(v) => v,
_ => panic!("expected complete"),
};
assert_eq!(varint.to_uint64(), 150);Required Methods§
Sourcefn read_varint_partial(&mut self) -> Result<DecodeOutcome>
fn read_varint_partial(&mut self) -> Result<DecodeOutcome>
Read a varint from this reader, supporting incomplete input.
Sourcefn read_varint_resume(&mut self, state: DecodeState) -> Result<DecodeOutcome>
fn read_varint_resume(&mut self, state: DecodeState) -> Result<DecodeOutcome>
Resume varint decoding with additional bytes from this reader.
Sourcefn read_varints(&mut self) -> VarintIterator<Bytes<&mut Self>>where
Self: Read,
fn read_varints(&mut self) -> VarintIterator<Bytes<&mut Self>>where
Self: Read,
Create an iterator that reads multiple varints from this reader.
Returns an iterator that yields Result<Varint> for each varint read.
The iterator stops when there are no more bytes available (EOF) or an error occurs.
§Example
use ::std::io::Cursor;
use ::protobuf_core::{ReadExtVarint, Varint};
let data = vec![0x96, 0x01, 0x7F]; // 150 and 127 in varint encoding
let mut reader = Cursor::new(data);
let varints: Vec<Varint> = reader.read_varints().collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(varints.len(), 2);
assert_eq!(varints[0].to_uint64(), 150);
assert_eq!(varints[1].to_uint64(), 127);Provided Methods§
Sourcefn read_varint(&mut self) -> Result<Option<Varint>>
fn read_varint(&mut self) -> Result<Option<Varint>>
Read a varint from this reader.
Returns Ok(Some(varint)) when a complete varint was read, Ok(None) when the input
is empty (EOF), and Err when the input is incomplete or invalid.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.