From 3392e834863ea3d240c421b5f10b36fe502bff72 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 7 Oct 2022 20:37:51 +0900 Subject: [PATCH] cli: do not abstract away underlying output stream at formatter layer Since the concrete Formatter type is hidden behind the Ui, there wouldn't be many reasons to use dyn Write at the formatter layer. This allows us to create a formatter against MutexGuard> without one more Box wrapper. --- src/formatter.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index e7e135fdb..d66391329 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -65,9 +65,9 @@ impl FormatterFactory { FormatterFactory { kind } } - pub fn new_formatter<'output>( + pub fn new_formatter<'output, W: Write + 'output>( &self, - output: Box, + output: W, ) -> Box { match &self.kind { FormatterFactoryKind::PlainText => Box::new(PlainTextFormatter::new(output)), @@ -82,17 +82,17 @@ impl FormatterFactory { } } -pub struct PlainTextFormatter<'output> { - output: Box, +pub struct PlainTextFormatter { + output: W, } -impl<'output> PlainTextFormatter<'output> { - pub fn new(output: Box) -> PlainTextFormatter<'output> { +impl PlainTextFormatter { + pub fn new(output: W) -> PlainTextFormatter { Self { output } } } -impl Write for PlainTextFormatter<'_> { +impl Write for PlainTextFormatter { fn write(&mut self, data: &[u8]) -> Result { self.output.write(data) } @@ -102,7 +102,7 @@ impl Write for PlainTextFormatter<'_> { } } -impl Formatter for PlainTextFormatter<'_> { +impl Formatter for PlainTextFormatter { fn add_label(&mut self, _label: &str) -> io::Result<()> { Ok(()) } @@ -112,8 +112,8 @@ impl Formatter for PlainTextFormatter<'_> { } } -pub struct ColorFormatter<'output> { - output: Box, +pub struct ColorFormatter { + output: W, colors: Arc>, labels: Vec, cached_colors: HashMap, Vec>, @@ -248,11 +248,8 @@ fn config_colors(user_settings: &UserSettings) -> HashMap { result } -impl<'output> ColorFormatter<'output> { - pub fn new( - output: Box, - colors: Arc>, - ) -> ColorFormatter<'output> { +impl ColorFormatter { + pub fn new(output: W, colors: Arc>) -> ColorFormatter { ColorFormatter { output, colors, @@ -315,7 +312,7 @@ impl<'output> ColorFormatter<'output> { } } -impl Write for ColorFormatter<'_> { +impl Write for ColorFormatter { fn write(&mut self, data: &[u8]) -> Result { self.output.write(data) } @@ -325,7 +322,7 @@ impl Write for ColorFormatter<'_> { } } -impl Formatter for ColorFormatter<'_> { +impl Formatter for ColorFormatter { fn add_label(&mut self, label: &str) -> io::Result<()> { self.labels.push(label.to_owned()); let new_color = self.current_color();