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 reader = BufReader::new(file);
let mut lines = ByteLines::new(reader);
// walk our lines using `while` syntax
while let Some(line) = lines.next() {
// do something with the line, which is Result<&[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 reader = BufReader::new(file);
let mut lines = ByteLines::new(reader);
// walk our lines using `for` syntax
for line in lines.into_iter() {
// do something with the line, which is Result<Vec<u8>, _>
}Implementations
Trait Implementations
sourceimpl<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.
sourcefn into_iter(self) -> ByteLinesIter<B>ⓘNotable traits for ByteLinesIter<B>impl<B> Iterator for ByteLinesIter<B> where
B: BufRead, type Item = Result<Vec<u8>, Error>;
fn into_iter(self) -> ByteLinesIter<B>ⓘNotable traits for ByteLinesIter<B>impl<B> Iterator for ByteLinesIter<B> where
B: BufRead, type Item = Result<Vec<u8>, Error>;
B: BufRead, type Item = Result<Vec<u8>, Error>;
Constructs a ByteLinesIter to provide an Iterator API.
type IntoIter = ByteLinesIter<B>
type IntoIter = ByteLinesIter<B>
Which kind of iterator are we turning this into?
Auto Trait Implementations
impl<B> RefUnwindSafe for ByteLines<B> where
B: RefUnwindSafe,
impl<B> Send for ByteLines<B> where
B: Send,
impl<B> Sync for ByteLines<B> where
B: Sync,
impl<B> Unpin for ByteLines<B> where
B: Unpin,
impl<B> UnwindSafe for ByteLines<B> where
B: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more