ReadExtVarint

Trait ReadExtVarint 

Source
pub trait ReadExtVarint {
    // Required methods
    fn read_varint(&mut self) -> Result<Option<Varint>>;
    fn read_varints(&mut self) -> VarintIterator<Bytes<&mut Self>>
       where Self: Read;
}
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::ReadExtVarint;

let file = std::fs::File::open("data.bin")?;
let mut reader = BufReader::new(file);
let varint = reader.read_varint()?;

§Example

use ::std::io::Cursor;
use ::protobuf_core::{ReadExtVarint, Varint};

let data = vec![0x96, 0x01]; // 150 in varint encoding
let mut reader = Cursor::new(data);
let varint = reader.read_varint().unwrap().unwrap();
assert_eq!(varint.to_uint64(), 150);

Required Methods§

Source

fn read_varint(&mut self) -> Result<Option<Varint>>

Read a varint from this reader.

Returns the Varint Ok(Some(varint)) if successfully read. Returns Ok(None) if no input is available (EOF). Returns Err(ProtobufError::VarintTooLong) if the varint exceeds MAX_VARINT_BYTES. Returns Err(ProtobufError::IoError) if an I/O error occurs.

Source

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

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.

Implementors§

Source§

impl<R> ReadExtVarint for R
where R: Read,