Remove unneccisary multi-`stdout` locking. Added internal buffering.

Fortune for rematch's current commit: Blessing − 吉
old-interface-extra-help-info
Avril 2 weeks ago
parent 4539f87528
commit 765270feaf
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -90,8 +90,8 @@ impl<'a> GroupConfig<'a>
} }
#[inline] #[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<()> fn print_groups<'c, 'a, S: ?Sized, G, T: 'a, I>(to: &mut S, g: G, groups: I, how: impl std::borrow::Borrow<GroupConfig<'c>>) -> std::io::Result<()>
where S: std::io::Write, 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<Item = &'a Option<T>> + Clone + Copy, // NOTE: Copy bound to ensure we're not accidentally doing deep clones of `g`. G: IntoIterator<Item = &'a Option<T>> + Clone + Copy, // NOTE: Copy bound to ensure we're not accidentally doing deep clones of `g`.
//G: std::ops::Index<usize>, G::Output: std::borrow::Borrow<Option<T>>, //G: std::ops::Index<usize>, G::Output: std::borrow::Borrow<Option<T>>,
T: std::borrow::Borrow<str>, T: std::borrow::Borrow<str>,
@ -101,6 +101,7 @@ where S: std::io::Write,
borrow::Borrow, borrow::Borrow,
io::Write, io::Write,
}; };
let how = how.borrow(); //std::borrow::ToOwned::clone_into(&self, target);
let mut first = true; let mut first = true;
for group in groups.into_iter() { for group in groups.into_iter() {
let group = group.borrow(); let group = group.borrow();
@ -128,7 +129,7 @@ where S: std::io::Write,
first = false; first = false;
} }
// If `first == true`, no groups were printed, so we do not print the new-line. // 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()) to.write_all(how.line_delimiter.as_ref())
} else { } else {
Ok(()) Ok(())
@ -181,11 +182,11 @@ fn main() -> eyre::Result<()>
let mut stdout = std::io::stdout(); let mut stdout = std::io::stdout();
if &text[..] == "-" { if &text[..] == "-" {
text::stdin_lines(|text| -> eyre::Result<bool> { let mut stdout = std::io::BufWriter::new(stdout.lock());
let mut stdout = stdout.lock(); text::stdin_lines(move |text| -> eyre::Result<bool> {
match re.exec(&text)? { match re.exec(&text)? {
Some(g) /*if g.len() > group*/ => // NOTE: This check branch has now been moved into `print_groups()` 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) Ok(true)

Loading…
Cancel
Save