pub struct ByteLines<B>where
B: BufRead,{ /* private fields */ }
Expand description
Provides iteration over bytes of input, split by line.
Unlike the implementation in the standard library, this requires
no allocations and simply references the input lines from the
internal buffer. In order to do this safely, we must sacrifice
the Iterator
API, and operate using while
syntax:
use bytelines::*;
use std::fs::File;
use std::io::BufReader;
// construct our iterator from our file input
let file = File::open("./res/numbers.txt").unwrap();
let mut lines = BufReader::new(file).byte_lines();
// walk our lines using `while` syntax
while let Some(line) = lines.next() {
// do something with the line, which is &[u8]
}
For those who prefer the Iterator
API, this structure implements
the IntoIterator
trait to provide it. This comes at the cost of
an allocation of a Vec
for each line in the Iterator
. This is
negligible in many cases, so often it comes down to which syntax
is preferred:
use bytelines::*;
use std::fs::File;
use std::io::BufReader;
// construct our iterator from our file input
let file = File::open("./res/numbers.txt").unwrap();
let lines = BufReader::new(file).byte_lines();
// walk our lines using `for` syntax
for line in lines.into_iter() {
// do something with the line, which is Vec<u8>
}
Implementations§
Trait Implementations§
source§impl<B> IntoIterator for ByteLines<B>where
B: BufRead,
impl<B> IntoIterator for ByteLines<B>where
B: BufRead,
IntoIterator
conversion for ByteLines
to provide Iterator
APIs.