pub trait BufReaderExt: BufRead + Seek {
// Provided methods
fn read_fixed_string(&mut self, length: usize) -> Result<String> { ... }
fn read_null_terminated_string(&mut self) -> Result<String> { ... }
fn read_enumerable<T: Default + Enumerable>(
&mut self,
count: u64,
) -> Result<Vec<T>>
where Self: Sized,
Vec<T>: FromIterator<T> { ... }
}
Expand description
Extension trait for BufRead
to add custom reading methods.
Provided Methods§
Sourcefn read_fixed_string(&mut self, length: usize) -> Result<String>
fn read_fixed_string(&mut self, length: usize) -> Result<String>
Reads a fixed-length UTF-8 encoded string from the reader.
This function reads exactly length
bytes and converts them to a String.
If the bytes read are all 0xFF, an empty string is returned.
§Arguments
length
- The exact number of bytes to read
§Errors
- If the reader fails to read the exact number of bytes
ReadError
- If the bytes read are not valid UTF-8
Utf8ReadingError
§Examples
use std::io::Cursor;
use std::io::BufReader;
use infinite_rs::common::extensions::BufReaderExt;
let data = b"I love cats!";
let mut reader = BufReader::new(Cursor::new(data));
let string = reader.read_fixed_string(data.len()).unwrap();
assert_eq!(string, "I love cats!");
Sourcefn read_null_terminated_string(&mut self) -> Result<String>
fn read_null_terminated_string(&mut self) -> Result<String>
Reads a null-terminated string from the reader.
This function reads bytes in a reader until it hits 0x00
and converts them to a String.
The null terminator is removed from the final output.
§Errors
- If the reader fails to read the exact number of bytes
ReadError
- If the bytes read are not valid UTF-8
Utf8ReadingError
§Examples
use std::io::Cursor;
use std::io::BufReader;
use infinite_rs::common::extensions::BufReaderExt;
let data = [0x49, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x20, 0x63, 0x61, 0x74, 0x73, 0x21, 0x00];
let mut reader = BufReader::new(Cursor::new(data));
let string = reader.read_null_terminated_string().unwrap();
assert_eq!(string, "I love cats!");
Sourcefn read_enumerable<T: Default + Enumerable>(
&mut self,
count: u64,
) -> Result<Vec<T>>
fn read_enumerable<T: Default + Enumerable>( &mut self, count: u64, ) -> Result<Vec<T>>
Reads multiple instances of an enumerable type into a vector.
Creates a vector of type T by reading the type count
times from the buffer.
Type T must implement both Default
and Enumerable
traits.
§Type Parameters
T
- The type to read, must implementDefault + Enumerable
§Arguments
count
- Number of instances to read
§Errors
- If the reader fails to read the exact number of bytes
ReadError
§Examples
use std::io::{Cursor, BufReader,};
use infinite_rs::common::extensions::{BufReaderExt, Enumerable};
use infinite_rs::common::errors::Error;
use byteorder::{ReadBytesExt, LE};
#[derive(Default)]
struct TestType {
value: u32,
}
impl Enumerable for TestType {
fn read<R: BufReaderExt>(&mut self, reader: &mut R) -> Result<(), Error> {
self.value = reader.read_u32::<LE>()?;
Ok(())
}
}
let data = b"\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00";
let mut reader = BufReader::new(Cursor::new(data));
let enumerables = reader.read_enumerable::<TestType>(3).unwrap();
assert_eq!(enumerables.len(), 3);
assert_eq!(enumerables[0].value, 1);
assert_eq!(enumerables[1].value, 2);
assert_eq!(enumerables[2].value, 3);