VarintReader

Trait VarintReader 

Source
pub trait VarintReader {
    type Error;

Show 13 methods // Required method fn read(&mut self) -> Result<u8, Self::Error>; // Provided methods fn read_i8_varint(&mut self) -> Result<i8, Self::Error> { ... } fn read_u8_varint(&mut self) -> Result<u8, Self::Error> { ... } fn read_i16_varint(&mut self) -> Result<i16, Self::Error> { ... } fn read_u16_varint(&mut self) -> Result<u16, Self::Error> { ... } fn read_i32_varint(&mut self) -> Result<i32, Self::Error> { ... } fn read_u32_varint(&mut self) -> Result<u32, Self::Error> { ... } fn read_i64_varint(&mut self) -> Result<i64, Self::Error> { ... } fn read_u64_varint(&mut self) -> Result<u64, Self::Error> { ... } fn read_i128_varint(&mut self) -> Result<i128, Self::Error> { ... } fn read_u128_varint(&mut self) -> Result<u128, Self::Error> { ... } fn read_isize_varint(&mut self) -> Result<isize, Self::Error> { ... } fn read_usize_varint(&mut self) -> Result<usize, Self::Error> { ... }
}
Expand description

The VarintReader trait enables reading of varints.

This is pre-implemented on structures which implement std::io::Read.

§Example

If you would like to implement VarintReader for a type, you’ll have to specify a VarintReader::Error and create the VarintReader::read method.

As an example, this is how std::io::Read is implemented:

use varint_rs::VarintReader;
use std::io;
 
// we are implementing `VarintReader` for all `std::io::Read` implementors
impl<R: io::Read> VarintReader for R {
  // reading can cause an error so we give it the appropriate error value
  type Error = io::Error;
 
  // now we can implement the read function which will read the next u8 value
  // for the varint
  fn read(&mut self) -> Result<u8, Self::Error> {
    // i won't explain this as the implementation will be specific to the
    // type you're implementing on
    let mut buf: [u8; 1] = [0];
 
    match io::Read::read(self, &mut buf) {
      Ok(count) => {
        if count == 1 {
          Ok(buf[0])
        } else {
          Err(io::Error::new(io::ErrorKind::UnexpectedEof, "could not read byte"))
        }
      },
      Err(error) => Err(error)
    }
  }
}

Required Associated Types§

Required Methods§

Source

fn read(&mut self) -> Result<u8, Self::Error>

Reads the next u8 for the varint.

Provided Methods§

Source

fn read_i8_varint(&mut self) -> Result<i8, Self::Error>

Reads an i8 from a signed 8-bit varint.

Source

fn read_u8_varint(&mut self) -> Result<u8, Self::Error>

Reads a u8 from an unsigned 8-bit varint.

Source

fn read_i16_varint(&mut self) -> Result<i16, Self::Error>

Reads an i16 from a signed 16-bit varint.

Source

fn read_u16_varint(&mut self) -> Result<u16, Self::Error>

Reads a u16 from an unsigned 16-bit varint.

Source

fn read_i32_varint(&mut self) -> Result<i32, Self::Error>

Reads an i32 from a signed 32-bit varint.

Source

fn read_u32_varint(&mut self) -> Result<u32, Self::Error>

Reads a u32 from an unsigned 32-bit varint.

Source

fn read_i64_varint(&mut self) -> Result<i64, Self::Error>

Reads an i64 from a signed 64-bit varint.

Source

fn read_u64_varint(&mut self) -> Result<u64, Self::Error>

Reads a u64 from an unsigned 64-bit varint.

Source

fn read_i128_varint(&mut self) -> Result<i128, Self::Error>

Reads an i128 from a signed 128-bit varint.

Source

fn read_u128_varint(&mut self) -> Result<u128, Self::Error>

Reads a u128 from an unsigned 128-bit varint.

Source

fn read_isize_varint(&mut self) -> Result<isize, Self::Error>

Reads an isize from a signed size-bit varint.

Source

fn read_usize_varint(&mut self) -> Result<usize, Self::Error>

Reads a usize from an unsigned size-bit varint.

Implementors§