gbf_core::graal_io

Struct GraalReader

Source
pub struct GraalReader<R: Read> { /* private fields */ }
Expand description

A reader that reads Graal-encoded data.

Implementations§

Source§

impl<R: Read> GraalReader<R>

Source

pub fn new(inner: R) -> Self

Creates a new GraalReader

§Arguments
  • inner: The reader to wrap.
§Returns
  • A new GraalReader with the given bytes.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let reader = GraalReader::new(Cursor::new(vec![1, 2, 3, 4]));
Source

pub fn decode_bits(slice: &[u8]) -> u64

Decodes a sequence of bytes using the Graal encoding.

§Arguments
  • slice: The slice of bytes to decode.
§Returns
  • The decoded integer.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let value = GraalReader::<Cursor<Vec<u8>>>::decode_bits(&[32, 32, 32, 33]);
assert_eq!(value, 1);
Source

pub fn read_string(&mut self) -> Result<String, GraalIoError>

Reads a null-terminated string from the reader.

§Returns
  • The string read from the reader.
§Errors
  • GraalIoError::NoNullTerminator: If the null terminator (0x00) is not found.
  • GraalIoError::Utf8ConversionFailed: If the bytes cannot be converted to a UTF-8 string.
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![104, 101, 108, 108, 111, 0, 119, 111, 114, 108, 100, 0]));
assert_eq!(reader.read_string().unwrap(), "hello");
Source

pub fn read_gstring(&mut self) -> Result<String, GraalIoError>

Reads a string with a graal-encoded integer at the beginning.

§Returns
  • The string read from the reader.
§Errors
  • GraalIoError::Utf8ConversionFailed: If the bytes cannot be converted to a UTF-8 string.
  • GraalIoError::Io: If there is an underlying I/O error.
  • GraalIoError::ValueExceedsMaximum: If the value exceeds the maximum for a Graal-encoded integer.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![32 + 5, 104, 101, 108, 108, 111, 32 + 5, 119, 111, 114, 108, 100]));
assert_eq!(reader.read_gstring().unwrap(), "hello");
assert_eq!(reader.read_gstring().unwrap(), "world");
Source

pub fn read_u8(&mut self) -> Result<u8, GraalIoError>

Reads an unsigned char from the reader.

§Returns
  • The unsigned char read from the reader.
§Errors
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![1, 2]));
assert_eq!(reader.read_u8().unwrap(), 1);
assert_eq!(reader.read_u8().unwrap(), 2);
Source

pub fn read_u16(&mut self) -> Result<u16, GraalIoError>

Read an unsigned short from the reader.

§Returns
  • The unsigned short read from the reader.
§Errors
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![0, 1, 0, 2]));
assert_eq!(reader.read_u16().unwrap(), 1);
assert_eq!(reader.read_u16().unwrap(), 2);
Source

pub fn read_u32(&mut self) -> Result<u32, GraalIoError>

Read an unsigned int from the reader.

§Returns
  • The unsigned int read from the reader.
§Errors
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![0, 0, 0, 1, 0, 0, 0, 2]));
assert_eq!(reader.read_u32().unwrap(), 1);
assert_eq!(reader.read_u32().unwrap(), 2);
Source

pub fn read_gu8(&mut self) -> Result<u64, GraalIoError>

Reads a Graal encoded unsigned 8-bit integer from the reader.

§Returns
  • The decoded unsigned char.
§Errors
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![32 + 1]));
assert_eq!(reader.read_gu8().unwrap(), 1);
Source

pub fn read_gu16(&mut self) -> Result<u64, GraalIoError>

Reads a Graal encoded unsigned 16-bit integer from the reader.

§Returns
  • The decoded unsigned short.
§Errors
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![32 + 1, 32 + 1]));
assert_eq!(reader.read_gu16().unwrap(), 129);
Source

pub fn read_gu24(&mut self) -> Result<u64, GraalIoError>

Reads a Graal encoded unsigned 24-bit integer from the reader.

§Returns
  • The decoded unsigned int.
§Errors
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![32 + 1, 32 + 1, 32 + 1]));
assert_eq!(reader.read_gu24().unwrap(), 16513);
Source

pub fn read_gu32(&mut self) -> Result<u64, GraalIoError>

Reads a Graal encoded unsigned 32-bit integer from the reader.

§Returns
  • The decoded unsigned int.
§Errors
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![32 + 1, 32 + 1, 32 + 1, 32 + 1]));
assert_eq!(reader.read_gu32().unwrap(), 2113665);
Source

pub fn read_gu40(&mut self) -> Result<u64, GraalIoError>

Reads a Graal encoded unsigned 40-bit integer from the reader.

§Returns
  • The decoded unsigned int.
§Errors
  • GraalIoError::Io: If there is an underlying I/O error.
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![32 + 1, 32 + 1, 32 + 1, 32 + 1, 32 + 1]));
assert_eq!(reader.read_gu40().unwrap(), 270549121);
Source

pub fn read_gu(&mut self, n: usize) -> Result<u64, GraalIoError>

Reads n bytes from the reader and decodes them as a Graal unsigned integer.

§Arguments
  • n: The number of bytes to read.
§Returns
  • The decoded Graal unsigned integer.
§Errors
  • GraalIoError::Io: If an I/O error occurs
§Examples
use gbf_core::graal_io::GraalReader;
use std::io::Cursor;

let mut reader = GraalReader::new(Cursor::new(vec![32 + 1, 32 + 1, 32 + 1, 32 + 1, 32 + 1]));
assert_eq!(reader.read_gu(5).unwrap(), 270549121);

Auto Trait Implementations§

§

impl<R> Freeze for GraalReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for GraalReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for GraalReader<R>
where R: Send,

§

impl<R> Sync for GraalReader<R>
where R: Sync,

§

impl<R> Unpin for GraalReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for GraalReader<R>
where R: UnwindSafe,

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