Struct bytelines::ByteLines

source ·
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§

Constructs a new ByteLines from an input BufRead.

Retrieves a reference to the next line of bytes in the reader (if any).

Trait Implementations§

IntoIterator conversion for ByteLines to provide Iterator APIs.

Constructs an ByteLinesIter to provide an Iterator API.

The type of the elements being iterated over.
Which kind of iterator are we turning this into?

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.