From f2e28dae33feda7eef26ea88070c2796a2f85281 Mon Sep 17 00:00:00 2001 From: TheVeryDarkness <3266343194@qq.com> Date: Mon, 14 Oct 2024 11:24:42 +0800 Subject: [PATCH] Fix an issue from clippy and add short line mode. --- benches/read.rs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/benches/read.rs b/benches/read.rs index 035938b..93030bf 100644 --- a/benches/read.rs +++ b/benches/read.rs @@ -5,10 +5,9 @@ use std::{ io::{self, BufRead, BufReader, Cursor, Read, Write}, }; -#[derive(Clone)] -struct LazyWriter(std::ops::Range, Vec); +struct LazyWriter(std::ops::Range, Vec); -impl Read for LazyWriter { +impl Read for LazyWriter { fn read(&mut self, mut buf: &mut [u8]) -> io::Result { let mut count = 0usize; if !self.1.is_empty() { @@ -16,8 +15,12 @@ impl Read for LazyWriter { 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); @@ -107,6 +110,11 @@ fn template( buf.push(byte); } } + if !buf.is_empty() { + let s = unwrap!(std::str::from_utf8(&buf)); + results.push(unwrap!(s.parse::())); + // buf.clear(); + } assert_eq!(results.len(), count); }) }); @@ -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::(0..COUNT as i32, Vec::new())) + }))(c); + (template("lazy - long", COUNT, || { + BufReader::new(LazyWriter::(0..COUNT as i32, Vec::new())) + }))(c); } criterion_group!(benches, cursor, file, lazy);