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

source§

impl<B> ByteLines<B>
where B: BufRead,

source

pub fn new(buf: B) -> Self

Constructs a new ByteLines from an input BufRead.

source

pub fn next(&mut self) -> Option<Result<&[u8], Error>>

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

Trait Implementations§

source§

impl<B> IntoIterator for ByteLines<B>
where B: BufRead,

IntoIterator conversion for ByteLines to provide Iterator APIs.

source§

fn into_iter(self) -> ByteLinesIter<B>

Constructs a ByteLinesIter to provide an Iterator API.

§

type Item = Result<Vec<u8>, Error>

The type of the elements being iterated over.
§

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§

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>,

§

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>,

§

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.