From 765270feafc1e51854e65088d93eb07c8bf3cb33 Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 6 Apr 2025 18:26:13 +0100 Subject: [PATCH] Remove unneccisary multi-`stdout` locking. Added internal buffering. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for rematch's current commit: Blessing − 吉 --- src/main.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 91daf9e..893153d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,8 +90,8 @@ impl<'a> GroupConfig<'a> } #[inline] -fn print_groups<'c, 'a, S: ?Sized, G, T: 'a, I>(to: &mut S, g: G, groups: I, how: GroupConfig<'c>) -> std::io::Result<()> -where S: std::io::Write, +fn print_groups<'c, 'a, S: ?Sized, G, T: 'a, I>(to: &mut S, g: G, groups: I, how: impl std::borrow::Borrow>) -> std::io::Result<()> +where S: std::io::Write + 'c, // NOTE: This lifetime bound is not yet used, as it is just `Write`, but if we change this to a context wrapper, then we can copy the `how`'s `'c` references into the context object without direct write/format/cloning. G: IntoIterator> + Clone + Copy, // NOTE: Copy bound to ensure we're not accidentally doing deep clones of `g`. //G: std::ops::Index, G::Output: std::borrow::Borrow>, T: std::borrow::Borrow, @@ -101,6 +101,7 @@ where S: std::io::Write, borrow::Borrow, io::Write, }; + let how = how.borrow(); //std::borrow::ToOwned::clone_into(&self, target); let mut first = true; for group in groups.into_iter() { let group = group.borrow(); @@ -128,7 +129,7 @@ where S: std::io::Write, first = false; } // If `first == true`, no groups were printed, so we do not print the new-line. - if !first && !how.line_delimiter.is_empty() { + if !first && how.has_line_delimiter() { to.write_all(how.line_delimiter.as_ref()) } else { Ok(()) @@ -181,11 +182,11 @@ fn main() -> eyre::Result<()> let mut stdout = std::io::stdout(); if &text[..] == "-" { - text::stdin_lines(|text| -> eyre::Result { - let mut stdout = stdout.lock(); + let mut stdout = std::io::BufWriter::new(stdout.lock()); + text::stdin_lines(move |text| -> eyre::Result { match re.exec(&text)? { Some(g) /*if g.len() > group*/ => // NOTE: This check branch has now been moved into `print_groups()` - print_groups(&mut stdout, &g, &groups, print_cfg.clone())?, //println!("{}", &g[group]), + print_groups(&mut stdout, &g, &groups, &print_cfg)?, //println!("{}", &g[group]), _ => (), } Ok(true)