[−][src]Trait omnom::prelude::BufReadExt
Extend BufRead
with methods for streaming parsing.
Provided methods
fn read_while<P>(&mut self, buf: &mut Vec<u8>, predicate: P) -> Result<usize> where
P: FnMut(u8) -> bool,
P: FnMut(u8) -> bool,
Read bytes based on a predicate.
read_while
takes a predicate as an argument.
It will call this on each byte, and copy it to the slice if the
predicate evaluates to true
. Returns the amount of bytes read.
Errors
If this function encounters an error of the kind
ErrorKind::Interrupted
then the error is ignored and the operation
will continue.
If any other read error is encountered then this function immediately
returns. Any bytes which have already been read will be appended to
buf
.
Examples
[std::io::Cursor
][Cursor
] is a type that implements BufRead
. In
this example, we use [Cursor
] to read bytes in a byte slice until
we encounter a hyphen:
use std::io::{self, BufRead}; use omnom::prelude::*; let mut cursor = io::Cursor::new(b"lorem-ipsum"); let mut buf = vec![]; let num_bytes = cursor.read_while(&mut buf, |b| b != b'-') .expect("reading from cursor won't fail"); assert_eq!(buf, b"lorem");
fn fill_while<P>(&mut self, buf: &mut Vec<u8>, predicate: P) -> Result<usize> where
Self: Read,
P: FnMut(u8) -> bool,
Self: Read,
P: FnMut(u8) -> bool,
Try reading based on a predicate.
read_while
takes a predicate as an argument.
It will call this on each byte, and copy it to the slice if the
predicate evaluates to true
. Returns the amount of bytes read.
Unlike read_while
after consuming bytes through this method you'll
have to manually call BufRead::consume
.
Errors
If this function encounters an error of the kind
ErrorKind::Interrupted
then the error is ignored and the operation
will continue.
If any other read error is encountered then this function immediately
returns. Any bytes which have already been read will be appended to
buf
.
Examples
std::io::Cursor
is a type that implements BufRead
. In
this example, we use Cursor
to read bytes in a byte slice until
we encounter a hyphen:
use std::io::{self, BufRead}; use omnom::prelude::*; let mut cursor = io::Cursor::new(b"lorem-ipsum"); let mut buf = vec![]; let num_bytes = cursor.fill_while(&mut buf, |b| b != b'-') .expect("reading from cursor won't fail"); assert_eq!(buf, b"lorem"); cursor.consume(num_bytes);
fn fill_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize>
Read all bytes into buf
until the delimiter byte
or EOF is reached.
This function will read bytes from the underlying stream until the
delimiter or EOF is found. Once found, all bytes up to, and including,
the delimiter (if found) will be appended to buf
.
Unlike read_until
after consuming bytes through this method you'll
have to manually call BufRead::consume
.
If successful, this function will return the total number of bytes read.
Errors
This function will ignore all instances of ErrorKind::Interrupted
and
will otherwise return any errors returned by BufRead::fill_buf
.
If an I/O error is encountered then all bytes read so far will be
present in buf
and its length will have been adjusted appropriately.
Examples
std::io::Cursor
is a type that implements BufRead
. In
this example, we use Cursor
to read all the bytes in a byte slice
in hyphen delimited segments:
use std::io::{self, BufRead}; use omnom::prelude::*; let mut cursor = io::Cursor::new(b"lorem-ipsum"); let mut buf = vec![]; // cursor is at 'l' let num_bytes = cursor.fill_until(b'-', &mut buf) .expect("reading from cursor won't fail"); assert_eq!(buf, b"lorem-"); assert_eq!(num_bytes, 6); cursor.consume(num_bytes); buf.clear(); // cursor is at 'i' let num_bytes = cursor.fill_until(b'-', &mut buf) .expect("reading from cursor won't fail"); assert_eq!(buf, b"ipsum"); assert_eq!(num_bytes, 5); cursor.consume(num_bytes); buf.clear(); // cursor is at EOF let num_bytes = cursor.fill_until(b'-', &mut buf) .expect("reading from cursor won't fail"); assert_eq!(num_bytes, 0); assert_eq!(buf, b"");
fn fill_exact(&mut self, buf: &mut [u8]) -> Result<()>
Read the exact number of bytes required to fill buf
.
This function reads as many bytes as necessary to completely fill the
specified buffer buf
.
Unlike read_exact
, after reading bytes through this method you'll
have to manually call BufRead::consume
.
No guarantees are provided about the contents of buf
when this
function is called, implementations cannot rely on any property of the
contents of buf
being true. It is recommended that implementations
only write data to buf
instead of reading its contents.
Errors
If this function encounters an error of the kind
ErrorKind::Interrupted
then the error is ignored and the operation
will continue.
If this function encounters an "end of file" before completely filling
the buffer, it returns an error of the kind ErrorKind::UnexpectedEof
.
The contents of buf
are unspecified in this case.
If any other read error is encountered then this function immediately
returns. The contents of buf
are unspecified in this case.
If this function returns an error, it is unspecified how many bytes it has read, but it will never read more than would be necessary to completely fill the buffer.
Examples
File
s implement Read
:
use std::io::{self, BufReader}; use std::io::prelude::*; use std::fs::File; use omnom::prelude::*; let mut cursor = io::Cursor::new(b"lorem-ipsum"); let mut buf = vec![0; 5]; // read exactly 5 bytes cursor.fill_exact(&mut buf).unwrap(); assert_eq!(buf, b"lorem"); buf.clear(); // the same bytes can be read again cursor.fill_exact(&mut buf).unwrap(); assert_eq!(buf, b"lorem"); buf.clear(); cursor.consume(5); // after consuming bytes we read new bytes cursor.fill_exact(&mut buf).unwrap(); assert_eq!(buf, b"-ipsu");
fn skip(&mut self, n: usize) -> Result<()>
Skip the first n
bytes.
fn skip_while<P>(&mut self, predicate: P) -> Result<usize> where
P: FnMut(u8) -> bool,
P: FnMut(u8) -> bool,
Skip bytes while the predicate is true.
fn fill_be<B: ReadBytes>(&mut self) -> Result<B> where
Self: Sized,
Self: Sized,
Fill bytes as big endian.
fn fill_le<B: ReadBytes>(&mut self) -> Result<B> where
Self: Sized,
Self: Sized,
Fill bytes as little endian.
fn fill_ne<B: ReadBytes>(&mut self) -> Result<B> where
Self: Sized,
Self: Sized,
Fill bytes using native endianness.