Skip to content

Commit add1e55

Browse files
committed
edition 2018 + clippy
1 parent 3e0ce24 commit add1e55

File tree

5 files changed

+41
-53
lines changed

5 files changed

+41
-53
lines changed

Cargo.toml

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
[package]
22
name = "easy_reader"
3-
version = "0.3.1"
3+
version = "0.4.0"
44
authors = ["Michele Federici (@ps1dr3x) <[email protected]>"]
55
description = "Move forward, backward or randomly through the lines of huge files. Easily and fastly."
66
repository = "https://github.com/ps1dr3x/easy_reader"
77
license = "Apache-2.0"
88
keywords = ["lines", "reader", "reverse", "backwards", "random"]
9-
readme="README.md"
9+
readme = "README.md"
10+
edition = "2018"
1011

1112
[dependencies]
12-
rand = "0.4.2"
13-
fnv = "1.0.3"
13+
rand = "~0.6"
14+
fnv = "~1.0"
1415

1516
[dev-dependencies]
16-
criterion = "0.2"
17+
criterion = "~0.2"
1718

1819
[[bench]]
1920
name = "benchmarks"

README.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ By the way, it's not advisable to generate the index for very large files, as an
1818
### Example: basic usage
1919

2020
```rust
21-
extern crate easy_reader;
22-
2321
use easy_reader::EasyReader;
2422
use std::{
2523
fs::File,
@@ -43,16 +41,16 @@ fn easy() -> Result<(), Error> {
4341
println!("Random line: {}", reader.random_line()?.unwrap());
4442

4543
// Iteration through the entire file (reverse)
46-
reader.from_eof();
44+
reader.eof();
4745
while let Some(line) = reader.prev_line()? {
4846
println!("{}", line);
4947
}
5048

5149
// You can always start/restart reading from the end of file (EOF)
52-
reader.from_eof();
50+
reader.eof();
5351
println!("Last line: {}", reader.prev_line()?.unwrap());
5452
// Or the begin of file (BOF)
55-
reader.from_bof();
53+
reader.bof();
5654
println!("First line: {}", reader.next_line()?.unwrap());
5755

5856
Ok(())
@@ -62,8 +60,6 @@ fn easy() -> Result<(), Error> {
6260
### Example: read random lines endlessly
6361

6462
```rust
65-
extern crate easy_reader;
66-
6763
use easy_reader::EasyReader;
6864
use std::{
6965
fs::File,

benches/benchmarks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn criterion_benchmark(c: &mut Criterion) {
4242
c.bench_function(
4343
"Read backward [1000][index]",
4444
move |b| b.iter(|| {
45-
reader.from_eof();
45+
reader.eof();
4646
while let Ok(Some(_line)) = reader.prev_line() {}
4747
}),
4848
);
@@ -63,7 +63,7 @@ fn criterion_benchmark(c: &mut Criterion) {
6363
"Read forward [EasyReader][index]",
6464
move |b| b.iter(|| {
6565
while let Ok(Some(_line)) = reader.next_line() {}
66-
reader.from_bof();
66+
reader.bof();
6767
}),
6868
);
6969

src/lib.rs

+18-27
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
//! ### Example: basic usage
2222
//!
2323
//! ```rust
24-
//! extern crate easy_reader;
25-
//!
2624
//! use easy_reader::EasyReader;
2725
//! use std::{
2826
//! fs::File,
@@ -46,16 +44,16 @@
4644
//! println!("Random line: {}", reader.random_line()?.unwrap());
4745
//!
4846
//! // Iteration through the entire file (reverse)
49-
//! reader.from_eof();
47+
//! reader.eof();
5048
//! while let Some(line) = reader.prev_line()? {
5149
//! println!("{}", line);
5250
//! }
5351
//!
5452
//! // You can always start/restart reading from the end of file (EOF)
55-
//! reader.from_eof();
53+
//! reader.eof();
5654
//! println!("Last line: {}", reader.prev_line()?.unwrap());
5755
//! // Or the begin of file (BOF)
58-
//! reader.from_bof();
56+
//! reader.bof();
5957
//! println!("First line: {}", reader.next_line()?.unwrap());
6058
//!
6159
//! Ok(())
@@ -65,8 +63,6 @@
6563
//! ### Example: read random lines endlessly
6664
//!
6765
//! ```no_run
68-
//! extern crate easy_reader;
69-
//!
7066
//! use easy_reader::EasyReader;
7167
//! use std::{
7268
//! fs::File,
@@ -89,9 +85,6 @@
8985
//! }
9086
//! ```
9187
92-
extern crate rand;
93-
extern crate fnv;
94-
9588
use std::io::{
9689
self,
9790
prelude::*,
@@ -102,8 +95,8 @@ use std::io::{
10295
use rand::Rng;
10396
use fnv::FnvHashMap;
10497

105-
const CR_BYTE: u8 = '\r' as u8;
106-
const LF_BYTE: u8 = '\n' as u8;
98+
const CR_BYTE: u8 = b'\r';
99+
const LF_BYTE: u8 = b'\n';
107100

108101
#[derive(Clone, PartialEq)]
109102
enum ReadMode {
@@ -130,7 +123,7 @@ impl<R: Read + Seek> EasyReader<R> {
130123
if file_size == 0 { return Err(Error::new(ErrorKind::UnexpectedEof, "Empty file")) }
131124

132125
Ok(EasyReader {
133-
file: file,
126+
file,
134127
file_size,
135128
chunk_size: 200,
136129
current_start_line_offset: 0,
@@ -146,13 +139,13 @@ impl<R: Read + Seek> EasyReader<R> {
146139
self
147140
}
148141

149-
pub fn from_bof(&mut self) -> &mut Self {
142+
pub fn bof(&mut self) -> &mut Self {
150143
self.current_start_line_offset = 0;
151144
self.current_end_line_offset = 0;
152145
self
153146
}
154147

155-
pub fn from_eof(&mut self) -> &mut Self {
148+
pub fn eof(&mut self) -> &mut Self {
156149
self.current_start_line_offset = self.file_size;
157150
self.current_end_line_offset = self.file_size;
158151
self
@@ -287,7 +280,7 @@ impl<R: Read + Seek> EasyReader<R> {
287280
let mut chunk = self.read_chunk(from)?;
288281
chunk.reverse();
289282

290-
for i in 0..self.chunk_size {
283+
for (i, chunk_el) in chunk.iter().enumerate().take(self.chunk_size) {
291284
if i < margin { continue; }
292285
if new_start_line_offset == 0 {
293286
found = true;
@@ -301,7 +294,7 @@ impl<R: Read + Seek> EasyReader<R> {
301294
continue;
302295
}
303296

304-
if chunk[i] == LF_BYTE {
297+
if *chunk_el == LF_BYTE {
305298
found = true;
306299
}
307300
}
@@ -312,10 +305,10 @@ impl<R: Read + Seek> EasyReader<R> {
312305
},
313306
ReadMode::Current => (),
314307
ReadMode::Next => {
315-
let mut chunk = self.read_chunk(new_start_line_offset)?;
308+
let chunk = self.read_chunk(new_start_line_offset)?;
316309

317-
for i in 0..self.chunk_size {
318-
if chunk[i] == LF_BYTE {
310+
for chunk_el in chunk.iter().take(self.chunk_size) {
311+
if *chunk_el == LF_BYTE {
319312
found = true;
320313
}
321314

@@ -351,12 +344,10 @@ impl<R: Read + Seek> EasyReader<R> {
351344
if chunk[i - 1] == CR_BYTE {
352345
new_end_line_offset -= 1;
353346
}
354-
} else {
355-
if new_end_line_offset < self.file_size {
356-
let next_byte = self.read_bytes(new_end_line_offset - 1, 1)?[0];
357-
if next_byte == CR_BYTE {
358-
new_end_line_offset -= 1;
359-
}
347+
} else if new_end_line_offset < self.file_size {
348+
let next_byte = self.read_bytes(new_end_line_offset - 1, 1)?[0];
349+
if next_byte == CR_BYTE {
350+
new_end_line_offset -= 1;
360351
}
361352
}
362353
found = true;
@@ -379,7 +370,7 @@ impl<R: Read + Seek> EasyReader<R> {
379370
fn read_bytes(&mut self, offset: u64, bytes: usize) -> io::Result<Vec<u8>> {
380371
let mut buffer = vec![0; bytes];
381372
self.file.seek(SeekFrom::Start(offset as u64))?;
382-
self.file.read(&mut buffer[..])?;
373+
self.file.read(&mut buffer)?;
383374
Ok(buffer)
384375
}
385376
}

src/tests.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ fn test_one_line_file() {
1919
assert!(reader.prev_line().unwrap().is_none(), "There is no other lines in one-line-file, this should be None");
2020
assert!(reader.current_line().unwrap().unwrap().eq("A"), "The single line of one-line-file should be: A");
2121

22-
reader.from_bof();
22+
reader.bof();
2323
assert!(reader.next_line().unwrap().unwrap().eq("A"), "The single line of one-line-file from the bof should be: A");
2424

25-
reader.from_eof();
25+
reader.eof();
2626
assert!(reader.prev_line().unwrap().unwrap().eq("A"), "The single line of one-line-file from the eof should be: A");
2727

2828
for _i in 1..10 {
@@ -35,14 +35,14 @@ fn test_move_through_lines() {
3535
let file = File::open("resources/test-file-lf").unwrap();
3636
let mut reader = EasyReader::new(file).unwrap();
3737

38-
reader.from_eof();
38+
reader.eof();
3939
assert!(reader.prev_line().unwrap().unwrap().eq("EEEE EEEEE EEEE EEEEE"), "[test-file-lf] The first line from the EOF should be: EEEE EEEEE EEEE EEEEE");
4040
assert!(reader.prev_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "[test-file-lf] The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
4141
assert!(reader.prev_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-lf] The third line from the EOF should be: CCCC CCCCC");
4242
assert!(reader.current_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-lf] The third line from the EOF should be: CCCC CCCCC");
4343
assert!(reader.next_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "[test-file-lf] The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
4444

45-
reader.from_bof();
45+
reader.bof();
4646
assert!(reader.next_line().unwrap().unwrap().eq("AAAA AAAA"), "[test-file-lf] The first line from the BOF should be: AAAA AAAA");
4747
assert!(reader.next_line().unwrap().unwrap().eq("B B BB BBB"), "[test-file-lf] The second line from the BOF should be: B B BB BBB");
4848
assert!(reader.next_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-lf] The third line from the BOF should be: CCCC CCCCC");
@@ -52,14 +52,14 @@ fn test_move_through_lines() {
5252
let file = File::open("resources/test-file-crlf").unwrap();
5353
let mut reader = EasyReader::new(file).unwrap();
5454

55-
reader.from_eof();
55+
reader.eof();
5656
assert!(reader.prev_line().unwrap().unwrap().eq("EEEE EEEEE EEEE EEEEE"), "[test-file-crlf] The first line from the EOF should be: EEEE EEEEE EEEE EEEEE");
5757
assert!(reader.prev_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "[test-file-crlf] The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
5858
assert!(reader.prev_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-crlf] The third line from the EOF should be: CCCC CCCCC");
5959
assert!(reader.current_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-crlf] The third line from the EOF should be: CCCC CCCCC");
6060
assert!(reader.next_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "[test-file-crlf] The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
6161

62-
reader.from_bof();
62+
reader.bof();
6363
assert!(reader.next_line().unwrap().unwrap().eq("AAAA AAAA"), "[test-file-crlf] The first line from the BOF should be: AAAA AAAA");
6464
assert!(reader.next_line().unwrap().unwrap().eq("B B BB BBB"), "[test-file-crlf] The second line from the BOF should be: B B BB BBB");
6565
assert!(reader.next_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-crlf] The third line from the BOF should be: CCCC CCCCC");
@@ -98,7 +98,7 @@ fn test_iterations() {
9898
assert!(reader.current_line().unwrap().unwrap().eq("EEEE EEEEE EEEE EEEEE"), "The first line from the EOF should be: EEEE EEEEE EEEE EEEEE");
9999
assert!(reader.prev_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
100100

101-
reader.from_eof();
101+
reader.eof();
102102
while let Ok(Some(line)) = reader.prev_line() {
103103
assert!(!line.is_empty(), "Empty line, but test-file-lf does not contain empty lines");
104104
}
@@ -113,14 +113,14 @@ fn test_indexed() {
113113
let mut reader = EasyReader::new(file).unwrap();
114114
reader.build_index().unwrap();
115115

116-
reader.from_eof();
116+
reader.eof();
117117
assert!(reader.prev_line().unwrap().unwrap().eq("EEEE EEEEE EEEE EEEEE"), "[test-file-lf] The first line from the EOF should be: EEEE EEEEE EEEE EEEEE");
118118
assert!(reader.prev_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "[test-file-lf] The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
119119
assert!(reader.prev_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-lf] The third line from the EOF should be: CCCC CCCCC");
120120
assert!(reader.current_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-lf] The third line from the EOF should be: CCCC CCCCC");
121121
assert!(reader.next_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "[test-file-lf] The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
122122

123-
reader.from_bof();
123+
reader.bof();
124124
assert!(reader.next_line().unwrap().unwrap().eq("AAAA AAAA"), "[test-file-lf] The first line from the BOF should be: AAAA AAAA");
125125
assert!(reader.next_line().unwrap().unwrap().eq("B B BB BBB"), "[test-file-lf] The second line from the BOF should be: B B BB BBB");
126126
assert!(reader.next_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-lf] The third line from the BOF should be: CCCC CCCCC");
@@ -131,14 +131,14 @@ fn test_indexed() {
131131
let mut reader = EasyReader::new(file).unwrap();
132132
reader.build_index().unwrap();
133133

134-
reader.from_eof();
134+
reader.eof();
135135
assert!(reader.prev_line().unwrap().unwrap().eq("EEEE EEEEE EEEE EEEEE"), "[test-file-crlf] The first line from the EOF should be: EEEE EEEEE EEEE EEEEE");
136136
assert!(reader.prev_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "[test-file-crlf] The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
137137
assert!(reader.prev_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-crlf] The third line from the EOF should be: CCCC CCCCC");
138138
assert!(reader.current_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-crlf] The third line from the EOF should be: CCCC CCCCC");
139139
assert!(reader.next_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "[test-file-crlf] The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
140140

141-
reader.from_bof();
141+
reader.bof();
142142
assert!(reader.next_line().unwrap().unwrap().eq("AAAA AAAA"), "[test-file-crlf] The first line from the BOF should be: AAAA AAAA");
143143
assert!(reader.next_line().unwrap().unwrap().eq("B B BB BBB"), "[test-file-crlf] The second line from the BOF should be: B B BB BBB");
144144
assert!(reader.next_line().unwrap().unwrap().eq("CCCC CCCCC"), "[test-file-crlf] The third line from the BOF should be: CCCC CCCCC");
@@ -156,7 +156,7 @@ fn test_indexed() {
156156
assert!(reader.current_line().unwrap().unwrap().eq("EEEE EEEEE EEEE EEEEE"), "The first line from the EOF should be: EEEE EEEEE EEEE EEEEE");
157157
assert!(reader.prev_line().unwrap().unwrap().eq("DDDD DDDDD DD DDD DDD DD"), "The second line from the EOF should be: DDDD DDDDD DD DDD DDD DD");
158158

159-
reader.from_eof();
159+
reader.eof();
160160
while let Ok(Some(line)) = reader.prev_line() {
161161
assert!(!line.is_empty(), "Empty line, but test-file-lf does not contain empty lines");
162162
}

0 commit comments

Comments
 (0)