Struct linereader::LineReader
source · pub struct LineReader<R> { /* private fields */ }Expand description
The LineReader struct adds buffered, byte-delimited (default: \n)
reading to any io::Reader.
Implementations
sourceimpl<R: Read> LineReader<R>
impl<R: Read> LineReader<R>
sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Create a new LineReader around the reader with a default capacity of
64 KiB and delimiter of \n.
let reader = LineReader::new(File::open("myfile.txt")?);sourcepub fn with_capacity(capacity: usize, inner: R) -> Self
pub fn with_capacity(capacity: usize, inner: R) -> Self
Create a new LineReader around the reader with a given capacity and
delimiter of \n.
let mut reader = LineReader::with_capacity(1024*512, File::open("myfile.txt")?);sourcepub fn with_delimiter(delimiter: u8, inner: R) -> Self
pub fn with_delimiter(delimiter: u8, inner: R) -> Self
Create a new LineReader around the reader with a default capacity of
1 MiB and the given delimiter.
let mut reader = LineReader::with_delimiter(b'\t', File::open("myfile.txt")?);sourcepub fn with_delimiter_and_capacity(
delimiter: u8,
capacity: usize,
inner: R
) -> Self
pub fn with_delimiter_and_capacity(
delimiter: u8,
capacity: usize,
inner: R
) -> Self
Create a new LineReader around the reader with a given capacity and
delimiter.
let mut reader = LineReader::with_delimiter_and_capacity(b'\t', 1024*512, File::open("myfile.txt")?);sourcepub fn next_line(&mut self) -> Option<Result<&[u8]>>
pub fn next_line(&mut self) -> Option<Result<&[u8]>>
Get the next line from the reader, an IO error, or None on EOF. The delimiter
is included in any returned slice, unless the file ends without one or a line was
truncated to the buffer size due to length.
while let Some(line) = reader.next_line() {
let line = line?; // unwrap io::Result to &[u8]
}sourcepub fn next_batch(&mut self) -> Option<Result<&[u8]>>
pub fn next_batch(&mut self) -> Option<Result<&[u8]>>
Return a slice of complete lines, up to the size of the internal buffer.
This is functionally identical to next_line, only instead of getting up to the first instance of the delimiter, you get up to the last.
while let Some(lines) = reader.next_batch() {
let lines = lines?; // unwrap io::Result to &[u8]
}sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the internal state of the buffer. Next lines are read from wherever the reader happens to be.
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Unwrap this LineReader, returning the underlying reader and discarding any
unread buffered lines.