Skip to content

Commit

Permalink
Fix an issue from clippy and add short line mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVeryDarkness committed Oct 14, 2024
1 parent e65edc4 commit f2e28da
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions benches/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ use std::{
io::{self, BufRead, BufReader, Cursor, Read, Write},
};

#[derive(Clone)]
struct LazyWriter(std::ops::Range<i32>, Vec<u8>);
struct LazyWriter<const LONG: bool>(std::ops::Range<i32>, Vec<u8>);

impl Read for LazyWriter {
impl<const LONG: bool> Read for LazyWriter<LONG> {
fn read(&mut self, mut buf: &mut [u8]) -> io::Result<usize> {
let mut count = 0usize;
if !self.1.is_empty() {
let len = buf.write(&self.1)?;
self.1.drain(..len);
count += len;
}
while let Some(num) = self.0.next() {
let mut s = format!(" {}", num);
for num in &mut self.0 {
let mut s = if LONG {
format!("{} ", num)
} else {
format!("{}\n", num)
};
let len = buf.write(s.as_bytes())?;
count += len;
s.drain(..len);
Expand Down Expand Up @@ -107,6 +110,11 @@ fn template<R: BufRead>(
buf.push(byte);
}
}
if !buf.is_empty() {
let s = unwrap!(std::str::from_utf8(&buf));
results.push(unwrap!(s.parse::<Element>()));
// buf.clear();
}
assert_eq!(results.len(), count);
})
});
Expand Down Expand Up @@ -138,10 +146,12 @@ fn file(c: &mut Criterion) {
}

fn lazy(c: &mut Criterion) {
let writer = LazyWriter(0..COUNT as i32, vec![]);
{
(template("lazy", COUNT, || BufReader::new(writer.clone())))(c);
}
(template("lazy - short", COUNT, || {
BufReader::new(LazyWriter::<false>(0..COUNT as i32, Vec::new()))
}))(c);
(template("lazy - long", COUNT, || {
BufReader::new(LazyWriter::<true>(0..COUNT as i32, Vec::new()))
}))(c);
}

criterion_group!(benches, cursor, file, lazy);
Expand Down

0 comments on commit f2e28da

Please sign in to comment.