#buffer #iterator #reader #reverse #line

reverse-lines

Rust Iterator for reading files line by line with a buffer in reverse

2 releases

0.1.1 Jun 10, 2023
0.1.0 Jun 10, 2023

#2215 in Algorithms

MIT license

11KB
164 lines

reverse_lines

This library provides a small Rust Iterator for reading files line by line with a buffer in reverse.

It is a rework of rev_lines.

Documentation

Documentation is available on Docs.rs.

Example

extern crate reverse_lines;

use reverse_lines::ReverseLines;

let file = File::open("/path/to/file").unwrap();
let mut reverse_lines = ReverseLines::new(file).unwrap();

for line in reverse_lines {
    println!("{}", line.unwrap());
}

lib.rs:

ReverseLines

This library provides a small Rust Iterator for reading files or anything that implements std::io::Seek and std::io::Read in reverse.

It is a rework of rev_lines with improved error handling and allowance for more types.

Example

 extern crate reverse_lines;

 use reverse_lines::ReverseLines;
 use std::io::BufReader;
 use std::fs::File;

 fn main() {
     let file = File::open("tests/multi_line_file").unwrap();
     let reverse_lines = ReverseLines::new(BufReader::new(file)).unwrap();

     for line in reverse_lines {
         println!("{}", line.unwrap());
     }
 }

If a line with invalid UTF-8 is encountered, or if there is an I/O error, the iterator will yield an std::io::Error.

This method uses logic borrowed from uutils/coreutils tail and code borrowed from rev_lines.

No runtime deps