Struct 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§

Source§

impl<R: Read> LineReader<R>

Source

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")?);
Source

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")?);
Source

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")?);
Source

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")?);
Source

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");
Source

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]
}
Source

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]
}
Source

pub fn reset(&mut self)

Reset the internal state of the buffer. Next lines are read from wherever the reader happens to be.

Source

pub fn get_ref(&self) -> &R

Get a reference to the reader.

Source

pub fn get_mut(&mut self) -> &mut R

Get a mutable reference to the reader.

Source

pub fn into_inner(self) -> R

Unwrap this LineReader, returning the underlying reader and discarding any unread buffered lines.

Trait Implementations§

Source§

impl<R: Read> Debug for LineReader<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for LineReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for LineReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for LineReader<R>
where R: Send,

§

impl<R> Sync for LineReader<R>
where R: Sync,

§

impl<R> Unpin for LineReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for LineReader<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.