pub struct LineReader<R> { /* private fields */ }
Expand description
The LineReader
struct adds buffered, byte-delimited (default: \n
)
reading to any io::Reader.
Implementations§
Source§impl<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
64 KiB 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 for_each<F: FnMut(&[u8]) -> Result<bool>>(&mut self, f: F) -> Result<()>
pub fn for_each<F: FnMut(&[u8]) -> Result<bool>>(&mut self, f: F) -> Result<()>
Run the given closure for each line while while the closure returns Ok(true)
.
If either the reader or the closure return an error, iteration ends and the error is returned.
let buf: &[u8] = b"foo\nbar\nbaz";
let mut reader = LineReader::new(buf);
let mut lines = vec![];
reader.for_each(|line| {
lines.push(line.to_vec());
Ok(true)
})?;
assert_eq!(lines.len(), 3);
assert_eq!(lines[0], b"foo\n");
assert_eq!(lines[1], b"bar\n");
assert_eq!(lines[2], b"baz");
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.