[][src]Struct whiteread::reader::Reader

pub struct Reader<B: BufRead> { /* fields omitted */ }

Wrapper for BufRead allowing easy parsing values from a Reader.

This struct contains line-buffer, which enables scanf-like behavior (newline-agnostic parsing). Newline-aware parsing is also supported.

Reader also provides almost zero-allocation parsing (an allocation is needed to store the line-buffer).

Examples

This code

let i = std::io::stdin();
let mut i = Reader::new(i.lock());
let (n, k): (i32, f64) = i.p();

will accept both of following inputs:

1

2
1 2

Overview of how various methods handle newlines:

let data = std::io::Cursor::new(
br"1 2

3 4 5
6 7
8
" as &[u8]);
let mut r = Reader::new(data);
assert_eq!(r.next_line().unwrap().trim(), "1 2");
assert_eq!(r.continue_().ok(), Some(1));
assert_eq!(r.continue_().ok(), Some( (2, 3) ));   // continue_line would return `TooShort` here
assert_eq!(r.continue_line().ok(), Some(4)); // finish_line would return `Leftovers` here
assert_eq!(r.start_line().ok(), Some(6));   // line would return `Leftovers` here
assert_eq!(r.line().ok(), Some(8));
// from now, everything will return TooShort

Methods

impl<B: BufRead> Reader<B>[src]

pub fn new(buf: B) -> Reader<B>[src]

Wraps a BufRead.

Note that you don't have to pass an owned buffered reader, it could be also &mut.

impl Reader<BufReader<Stdin>>[src]

pub fn from_stdin_naive() -> Reader<BufReader<Stdin>>[src]

Wraps stdin in the reader making some assumptions

This is a helper that allows constructing a Reader from io::stdin in one function. The usual way to do it needs a separate binding for stdin, because of how StdinLock works:

let stdin = std::io::stdin();
let mut reader = Reader::new(stdin.lock());

Examples

This struct allows you to just call

let mut reader = Reader::from_stdin_naive();

This is almost equivalent to previous snippet, but:

Drawbacks

This will construct stdin object with a separate buffer, in addition to the buffer that stdin already uses. This may cause:

  • unexpected behaviour when mixing Reader usage with
  • double-buffering, which may affect performance. But on the normal usage, StdinLock should just pass the read through and not buffer.

So if you're not trying to get last percent of performance and you operate on stdin solely using the Reader, you're free to use this constructor.

See also

impl Reader<BufReader<File>>[src]

pub fn open<P: AsRef<Path>>(path: P) -> Result<Reader<BufReader<File>>>[src]

Opens a file and wraps in Reader

Shortcut for opening a file, wrapping it in a BufReader and then in a Reader.

Examples

Read an integer from the beginning of file.

let mut reader = Reader::open("number.txt").unwrap();
let x: u32 = Reader::open("number.txt").unwrap().parse().unwrap();

See also

impl<B: BufRead> Reader<B>[src]

Line-agnostic parsing

Following methods parse some part of input into a FromStream value.

Errors

These methods may return TooShort, ParseError or IoError error variant. If they return other variants too, it is stated explicitely.

pub fn continue_<T: FromStream>(&mut self) -> Result<T>[src]

Parses a FromStream value without specialy treating newlines (just like scanf or cin>>)

pub fn parse<T: FromStream>(&mut self) -> Result<T>[src]

Same as continue_.

Using continue_ over parse is preferred, as it conveys better which part of input will be parsed.

pub fn p<T: FromStream>(&mut self) -> T[src]

Just .continue_().unwrap().

Use it if you really value your time. ;)

pub fn finish<T: FromStream>(&mut self) -> Result<T>[src]

Parses remaining part of reader into FromStream value in a line-agnostic way.

It could be used with T=(), to just check if we're at the EOF.

Errors

Additionaly to usual parse errors, this method may also return Leftovers.

impl<B: BufRead> Reader<B>[src]

Line-aware parsing

Following methods parse some part of input into a FromStream value.

Errors

These methods may return TooShort, ParseError or IoError error variant. If they return other variants too, it is stated explicitely.

pub fn line<T: FromStream>(&mut self) -> Result<T>[src]

Reads a new line from input and parses it into FromStream value as a whole.

The function is called just line for brevity and also to make it look different than global read_line to avoid mistakes.

Errors

Additionaly to usual parse errors, this method may also return Leftovers.

pub fn start_line<T: FromStream>(&mut self) -> Result<T>[src]

Reads a new line from input and parses some part of it into FromStream value.

pub fn continue_line<T: FromStream>(&mut self) -> Result<T>[src]

Parses some part of current line into FromStream value.

pub fn finish_line<T: FromStream>(&mut self) -> Result<T>[src]

Parses remaining part of current line into FromStream value.

It could be used with T=(), to just check if we're on the end of line.

Errors

Additionaly to usual parse errors, this method may also return Leftovers.

impl<B: BufRead> Reader<B>[src]

pub fn next_line(&mut self) -> Result<&str>[src]

Reads a new line and returns it.

This function should be used when FromStream-returning functions are insufficient or just to get a preview of a line. Note that line's content will not be considered consumed and will be available for continue_ and continue_line.

pub fn into_inner(self) -> B[src]

Gets underlying buffer back.

Trait Implementations

impl<B: BufRead> StrStream for Reader<B>[src]

Auto Trait Implementations

impl<B> Send for Reader<B> where
    B: Send

impl<B> Sync for Reader<B> where
    B: Sync

impl<B> Unpin for Reader<B> where
    B: Unpin

impl<B> UnwindSafe for Reader<B> where
    B: UnwindSafe

impl<B> RefUnwindSafe for Reader<B> where
    B: RefUnwindSafe

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]