Struct TokenReader

Source
pub struct TokenReader<R> { /* private fields */ }
Expand description

This struct wraps a BufReader to allow easy parsing of whitespace delimited files.

Implementations§

Source§

impl<R: BufRead> TokenReader<R>

Source

pub fn new(buf_read: R) -> Self

Creates a TokenReader from a type that implements BufRead, such as Stdin.

Source

pub fn line<T>(&mut self) -> Result<T, ReadTokensError<T::Error>>
where T: FromTokens,

Reads and parses a single line of whitespace delimited tokens.

§Examples
let mut input = TokenReader::new("James 158000 0.58".as_bytes());
let (name, points, win_chance): (String, u64, f64) = input.line()?;

assert_eq!(name, "James");
assert_eq!(points, 158000);
assert_eq!(win_chance, 0.58);
let mut input = TokenReader::new("13 8 17".as_bytes());
let numbers: Vec<i64> = input.line()?;

assert_eq!(numbers, vec![13, 8, 17]);
Source

pub fn line_raw(&mut self) -> Result<String, ReadLineError>

Reads a single line, unmodified.

§Example
let mut input = TokenReader::new("1. Write a parsing library in Rust.\n2. ???\n3. Profit!".as_bytes());
let line = input.line_raw()?;

assert_eq!(line, "1. Write a parsing library in Rust.");
Source

pub fn take<T>(&mut self, count: usize) -> Take<'_, T, R, usize>
where T: FromTokens,

Creates an iterator that reads and parses a specific number of lines.

The line count must be an usize. You can also use TokenReader::take_count, which allows other types.

§Example
let mut input = TokenReader::new("1 a\n2 b\n3 c".as_bytes());
let lines: Vec<(u64, char)> = input.take(3).collect::<Result<_, _>>()?;

assert_eq!(lines, vec![(1, 'a'), (2, 'b'), (3, 'c')]);
Source

pub fn take_count<T, S>(&mut self, count: S) -> Take<'_, T, R, S>
where T: FromTokens, S: LineCount,

Like TokenReader::take, but can use non-usize counts.

This method can use any type implementing LineCount as the element count, like u32. It can be used to process more than 2^32 lines on 32-bit systems.

§Example
let mut input = TokenReader::new("1 a\n2 b\n3 c".as_bytes());
let lines: Vec<(u64, char)> = input.take_count(3u64).collect::<Result<_, _>>()?;

assert_eq!(lines, vec![(1, 'a'), (2, 'b'), (3, 'c')]);
Source§

impl<R: Read> TokenReader<BufReader<R>>

Source

pub fn from_read(read: R) -> Self

Creates a TokenReader from a type that implements Read.

This is a convenience method for wrapping the reader with BufReader.

Trait Implementations§

Source§

impl<R: Debug> Debug for TokenReader<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R> From<R> for TokenReader<R>
where R: BufRead,

Source§

fn from(value: R) -> Self

Wraps an implementation of BufRead.

Identical to TokenReader::new.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<R> UnwindSafe for TokenReader<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.