From 6c69160d6b297a6963230fcc4d41a34f39898cc5 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Fri, 7 Sep 2018 22:24:52 +0200 Subject: [PATCH] feat: remove unecessary borrows of Style --- examples/custom_widget.rs | 2 +- examples/demo.rs | 12 ++++----- examples/list.rs | 8 +++--- examples/table.rs | 4 +-- src/buffer.rs | 18 +++++++++----- src/widgets/barchart.rs | 4 +-- src/widgets/block.rs | 2 +- src/widgets/canvas/mod.rs | 2 +- src/widgets/chart.rs | 10 ++++---- src/widgets/gauge.rs | 2 +- src/widgets/list.rs | 48 ++++++++++++++++++------------------ src/widgets/table.rs | 52 +++++++++++++++++++-------------------- src/widgets/tabs.rs | 4 +-- 13 files changed, 87 insertions(+), 81 deletions(-) diff --git a/examples/custom_widget.rs b/examples/custom_widget.rs index 54b13575..e88760e8 100644 --- a/examples/custom_widget.rs +++ b/examples/custom_widget.rs @@ -19,7 +19,7 @@ impl<'a> Default for Label<'a> { impl<'a> Widget for Label<'a> { fn draw(&mut self, area: Rect, buf: &mut Buffer) { - buf.set_string(area.left(), area.top(), self.text, &Style::default()); + buf.set_string(area.left(), area.top(), self.text, Style::default()); } } diff --git a/examples/demo.rs b/examples/demo.rs index ca7565b4..c8059562 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -367,10 +367,10 @@ fn draw_charts(f: &mut Frame, app: &App, area: Rect) { Item::StyledData( format!("{}: {}", level, evt), match level { - "ERROR" => &error_style, - "CRITICAL" => &critical_style, - "WARNING" => &warning_style, - _ => &info_style, + "ERROR" => error_style, + "CRITICAL" => critical_style, + "WARNING" => warning_style, + _ => info_style, }, ) }); @@ -477,9 +477,9 @@ fn draw_second_tab(f: &mut Frame, app: &App, area: Rect) { ["Server", "Location", "Status"].into_iter(), app.servers.iter().map(|s| { let style = if s.status == "Up" { - &up_style + up_style } else { - &failure_style + failure_style }; Row::StyledData(vec![s.name, s.location, s.status].into_iter(), style) }), diff --git a/examples/list.rs b/examples/list.rs index db461dce..f26e8e78 100644 --- a/examples/list.rs +++ b/examples/list.rs @@ -191,10 +191,10 @@ fn draw(t: &mut Terminal, app: &App) -> Result<(), io::Error> { Item::StyledData( format!("{}: {}", level, evt), match level { - "ERROR" => &app.error_style, - "CRITICAL" => &app.critical_style, - "WARNING" => &app.warning_style, - _ => &app.info_style, + "ERROR" => app.error_style, + "CRITICAL" => app.critical_style, + "WARNING" => app.warning_style, + _ => app.info_style, }, ) }); diff --git a/examples/table.rs b/examples/table.rs index fd3de1f8..2aaed375 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -90,9 +90,9 @@ fn draw(t: &mut Terminal, app: &App) -> Result<(), io::Error> { let header = ["Header1", "Header2", "Header3"]; let rows = app.items.iter().enumerate().map(|(i, item)| { if i == app.selected { - Row::StyledData(item.into_iter(), &selected_style) + Row::StyledData(item.into_iter(), selected_style) } else { - Row::StyledData(item.into_iter(), &normal_style) + Row::StyledData(item.into_iter(), normal_style) } }); diff --git a/src/buffer.rs b/src/buffer.rs index d592d901..015969a7 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -81,7 +81,7 @@ impl Default for Cell { /// let mut buf = Buffer::empty(Rect{x: 0, y: 0, width: 10, height: 5}); /// buf.get_mut(0, 2).set_symbol("x"); /// assert_eq!(buf.get(0, 2).symbol, "x"); -/// buf.set_string(3, 0, "string", &Style::default().fg(Color::Red).bg(Color::White)); +/// buf.set_string(3, 0, "string", Style::default().fg(Color::Red).bg(Color::White)); /// assert_eq!(buf.get(5, 0), &Cell{ /// symbol: String::from("r"), /// style: Style { @@ -233,20 +233,26 @@ impl Buffer { } /// Print a string, starting at the position (x, y) - pub fn set_string(&mut self, x: u16, y: u16, string: &str, style: &Style) { + pub fn set_string(&mut self, x: u16, y: u16, string: S, style: Style) + where + S: AsRef, + { self.set_stringn(x, y, string, usize::MAX, style); } /// Print at most the first n characters of a string if enough space is available /// until the end of the line - pub fn set_stringn(&mut self, x: u16, y: u16, string: &str, limit: usize, style: &Style) { + pub fn set_stringn(&mut self, x: u16, y: u16, string: S, limit: usize, style: Style) + where + S: AsRef, + { let mut index = self.index_of(x, y); - let graphemes = UnicodeSegmentation::graphemes(string, true).collect::>(); + let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true); let max_index = min((self.area.right() - x) as usize, limit); - for s in graphemes.into_iter().take(max_index) { + for s in graphemes.take(max_index) { self.content[index].symbol.clear(); self.content[index].symbol.push_str(s); - self.content[index].style = *style; + self.content[index].style = style; index += 1; } } diff --git a/src/widgets/barchart.rs b/src/widgets/barchart.rs index 28c67877..057a9645 100644 --- a/src/widgets/barchart.rs +++ b/src/widgets/barchart.rs @@ -177,7 +177,7 @@ impl<'a> Widget for BarChart<'a> { + (self.bar_width - width) / 2, chart_area.bottom() - 2, value_label, - &self.value_style, + self.value_style, ); } } @@ -186,7 +186,7 @@ impl<'a> Widget for BarChart<'a> { chart_area.bottom() - 1, label, self.bar_width as usize, - &self.label_style, + self.label_style, ); } } diff --git a/src/widgets/block.rs b/src/widgets/block.rs index d74ea1b6..29e062fe 100644 --- a/src/widgets/block.rs +++ b/src/widgets/block.rs @@ -178,7 +178,7 @@ impl<'a> Widget for Block<'a> { area.top(), title, width as usize, - &self.title_style, + self.title_style, ); } } diff --git a/src/widgets/canvas/mod.rs b/src/widgets/canvas/mod.rs index 082ee0d0..a45dcb37 100644 --- a/src/widgets/canvas/mod.rs +++ b/src/widgets/canvas/mod.rs @@ -285,7 +285,7 @@ where dx + canvas_area.left(), dy + canvas_area.top(), label.text, - &style.fg(label.color), + style.fg(label.color), ); } } diff --git a/src/widgets/chart.rs b/src/widgets/chart.rs index b1ae09de..26004f2c 100644 --- a/src/widgets/chart.rs +++ b/src/widgets/chart.rs @@ -361,12 +361,12 @@ where if let Some((x, y)) = layout.title_x { let title = self.x_axis.title.unwrap(); - buf.set_string(x, y, title, &self.x_axis.style); + buf.set_string(x, y, title, self.x_axis.style); } if let Some((x, y)) = layout.title_y { let title = self.y_axis.title.unwrap(); - buf.set_string(x, y, title, &self.y_axis.style); + buf.set_string(x, y, title, self.y_axis.style); } if let Some(y) = layout.label_x { @@ -380,7 +380,7 @@ where - label.as_ref().width() as u16, y, label.as_ref(), - &self.x_axis.labels_style, + self.x_axis.labels_style, ); } } @@ -396,7 +396,7 @@ where x, graph_area.bottom() - 1 - dy, label.as_ref(), - &self.y_axis.labels_style, + self.y_axis.labels_style, ); } } @@ -471,7 +471,7 @@ where legend_area.x + 1, legend_area.y + 1 + i as u16, dataset.name, - &dataset.style, + dataset.style, ); } } diff --git a/src/widgets/gauge.rs b/src/widgets/gauge.rs index 503e7153..27396e6d 100644 --- a/src/widgets/gauge.rs +++ b/src/widgets/gauge.rs @@ -92,7 +92,7 @@ impl<'a> Widget for Gauge<'a> { let label = self.label.unwrap_or(&precent_label); let label_width = label.width() as u16; let middle = (gauge_area.width - label_width) / 2 + gauge_area.left(); - buf.set_string(middle, y, label, &self.style); + buf.set_string(middle, y, label, self.style); } // Fix colors diff --git a/src/widgets/list.rs b/src/widgets/list.rs index 27575273..f79ffde9 100644 --- a/src/widgets/list.rs +++ b/src/widgets/list.rs @@ -9,14 +9,14 @@ use layout::{Corner, Rect}; use style::Style; use widgets::{Block, Widget}; -pub enum Item<'i, D: 'i> { +pub enum Item { Data(D), - StyledData(D, &'i Style), + StyledData(D, Style), } -pub struct List<'b, 'i, L, D: 'i> +pub struct List<'b, L, D> where - L: Iterator>, + L: Iterator>, { block: Option>, items: L, @@ -24,11 +24,11 @@ where start_corner: Corner, } -impl<'b, 'i, L, D> Default for List<'b, 'i, L, D> +impl<'b, L, D> Default for List<'b, L, D> where - L: Iterator> + Default, + L: Iterator> + Default, { - fn default() -> List<'b, 'i, L, D> { + fn default() -> List<'b, L, D> { List { block: None, items: L::default(), @@ -38,11 +38,11 @@ where } } -impl<'b, 'i, L, D> List<'b, 'i, L, D> +impl<'b, L, D> List<'b, L, D> where - L: Iterator>, + L: Iterator>, { - pub fn new(items: L) -> List<'b, 'i, L, D> { + pub fn new(items: L) -> List<'b, L, D> { List { block: None, items, @@ -51,33 +51,33 @@ where } } - pub fn block(mut self, block: Block<'b>) -> List<'b, 'i, L, D> { + pub fn block(mut self, block: Block<'b>) -> List<'b, L, D> { self.block = Some(block); self } - pub fn items(mut self, items: I) -> List<'b, 'i, L, D> + pub fn items(mut self, items: I) -> List<'b, L, D> where - I: IntoIterator, IntoIter = L>, + I: IntoIterator, IntoIter = L>, { self.items = items.into_iter(); self } - pub fn style(mut self, style: Style) -> List<'b, 'i, L, D> { + pub fn style(mut self, style: Style) -> List<'b, L, D> { self.style = style; self } - pub fn start_corner(mut self, corner: Corner) -> List<'b, 'i, L, D> { + pub fn start_corner(mut self, corner: Corner) -> List<'b, L, D> { self.start_corner = corner; self } } -impl<'b, 'i, L, D> Widget for List<'b, 'i, L, D> +impl<'b, L, D> Widget for List<'b, L, D> where - L: Iterator>, + L: Iterator>, D: Display, { fn draw(&mut self, area: Rect, buf: &mut Buffer) { @@ -112,13 +112,13 @@ where buf.set_stringn( x, y, - &format!("{}", v), + format!("{}", v), list_area.width as usize, - &Style::default(), + Style::default(), ); } Item::StyledData(ref v, s) => { - buf.set_stringn(x, y, &format!("{}", v), list_area.width as usize, s); + buf.set_stringn(x, y, format!("{}", v), list_area.width as usize, s); } }; } @@ -216,8 +216,8 @@ impl<'b> Widget for SelectableList<'b> { // Use highlight_style only if something is selected let (selected, highlight_style) = match self.selected { - Some(i) => (Some(i), &self.highlight_style), - None => (None, &self.style), + Some(i) => (Some(i), self.highlight_style), + None => (None, self.style), }; let highlight_symbol = self.highlight_symbol.unwrap_or(""); let blank_symbol = iter::repeat(" ") @@ -244,10 +244,10 @@ impl<'b> Widget for SelectableList<'b> { if i == s { Item::StyledData(format!("{} {}", highlight_symbol, item), highlight_style) } else { - Item::StyledData(format!("{} {}", blank_symbol, item), &self.style) + Item::StyledData(format!("{} {}", blank_symbol, item), self.style) } } else { - Item::StyledData(item.to_string(), &self.style) + Item::StyledData(item.to_string(), self.style) } }) .skip(offset as usize); diff --git a/src/widgets/table.rs b/src/widgets/table.rs index af4e0a5a..83fc7008 100644 --- a/src/widgets/table.rs +++ b/src/widgets/table.rs @@ -7,13 +7,13 @@ use style::Style; use widgets::{Block, Widget}; /// Holds data to be displayed in a Table widget -pub enum Row<'i, D, I> +pub enum Row where D: Iterator, I: Display, { Data(D), - StyledData(D, &'i Style), + StyledData(D, Style), } /// A widget to display data in formatted columns @@ -28,9 +28,9 @@ where /// Table::new( /// ["Col1", "Col2", "Col3"].into_iter(), /// vec![ -/// Row::StyledData(["Row11", "Row12", "Row13"].into_iter(), &row_style), -/// Row::StyledData(["Row21", "Row22", "Row23"].into_iter(), &row_style), -/// Row::StyledData(["Row31", "Row32", "Row33"].into_iter(), &row_style), +/// Row::StyledData(["Row11", "Row12", "Row13"].into_iter(), row_style), +/// Row::StyledData(["Row21", "Row22", "Row23"].into_iter(), row_style), +/// Row::StyledData(["Row31", "Row32", "Row33"].into_iter(), row_style), /// Row::Data(["Row41", "Row42", "Row43"].into_iter()) /// ].into_iter() /// ) @@ -41,13 +41,13 @@ where /// .column_spacing(1); /// # } /// ``` -pub struct Table<'a, 'i, T, H, I, D, R> +pub struct Table<'a, T, H, I, D, R> where T: Display, H: Iterator, I: Display, D: Iterator, - R: Iterator>, + R: Iterator>, { /// A block to wrap the widget in block: Option>, @@ -66,15 +66,15 @@ where rows: R, } -impl<'a, 'i, T, H, I, D, R> Default for Table<'a, 'i, T, H, I, D, R> +impl<'a, T, H, I, D, R> Default for Table<'a, T, H, I, D, R> where T: Display, H: Iterator + Default, I: Display, D: Iterator, - R: Iterator> + Default, + R: Iterator> + Default, { - fn default() -> Table<'a, 'i, T, H, I, D, R> { + fn default() -> Table<'a, T, H, I, D, R> { Table { block: None, style: Style::default(), @@ -87,15 +87,15 @@ where } } -impl<'a, 'i, T, H, I, D, R> Table<'a, 'i, T, H, I, D, R> +impl<'a, T, H, I, D, R> Table<'a, T, H, I, D, R> where T: Display, H: Iterator, I: Display, D: Iterator, - R: Iterator>, + R: Iterator>, { - pub fn new(header: H, rows: R) -> Table<'a, 'i, T, H, I, D, R> { + pub fn new(header: H, rows: R) -> Table<'a, T, H, I, D, R> { Table { block: None, style: Style::default(), @@ -106,12 +106,12 @@ where column_spacing: 1, } } - pub fn block(mut self, block: Block<'a>) -> Table<'a, 'i, T, H, I, D, R> { + pub fn block(mut self, block: Block<'a>) -> Table<'a, T, H, I, D, R> { self.block = Some(block); self } - pub fn header(mut self, header: II) -> Table<'a, 'i, T, H, I, D, R> + pub fn header(mut self, header: II) -> Table<'a, T, H, I, D, R> where II: IntoIterator, { @@ -119,42 +119,42 @@ where self } - pub fn header_style(mut self, style: Style) -> Table<'a, 'i, T, H, I, D, R> { + pub fn header_style(mut self, style: Style) -> Table<'a, T, H, I, D, R> { self.header_style = style; self } - pub fn widths(mut self, widths: &'a [u16]) -> Table<'a, 'i, T, H, I, D, R> { + pub fn widths(mut self, widths: &'a [u16]) -> Table<'a, T, H, I, D, R> { self.widths = widths; self } - pub fn rows(mut self, rows: II) -> Table<'a, 'i, T, H, I, D, R> + pub fn rows(mut self, rows: II) -> Table<'a, T, H, I, D, R> where - II: IntoIterator, IntoIter = R>, + II: IntoIterator, IntoIter = R>, { self.rows = rows.into_iter(); self } - pub fn style(mut self, style: Style) -> Table<'a, 'i, T, H, I, D, R> { + pub fn style(mut self, style: Style) -> Table<'a, T, H, I, D, R> { self.style = style; self } - pub fn column_spacing(mut self, spacing: u16) -> Table<'a, 'i, T, H, I, D, R> { + pub fn column_spacing(mut self, spacing: u16) -> Table<'a, T, H, I, D, R> { self.column_spacing = spacing; self } } -impl<'a, 'i, T, H, I, D, R> Widget for Table<'a, 'i, T, H, I, D, R> +impl<'a, T, H, I, D, R> Widget for Table<'a, T, H, I, D, R> where T: Display, H: Iterator, I: Display, D: Iterator, - R: Iterator>, + R: Iterator>, { fn draw(&mut self, area: Rect, buf: &mut Buffer) { // Render block if necessary and get the drawing area @@ -185,7 +185,7 @@ where if y < table_area.bottom() { x = table_area.left(); for (w, t) in widths.iter().zip(self.header.by_ref()) { - buf.set_string(x, y, &format!("{}", t), &self.header_style); + buf.set_string(x, y, format!("{}", t), self.header_style); x += *w + self.column_spacing; } } @@ -197,12 +197,12 @@ where let remaining = (table_area.bottom() - y) as usize; for (i, row) in self.rows.by_ref().take(remaining).enumerate() { let (data, style) = match row { - Row::Data(d) => (d, &default_style), + Row::Data(d) => (d, default_style), Row::StyledData(d, s) => (d, s), }; x = table_area.left(); for (w, elt) in widths.iter().zip(data) { - buf.set_stringn(x, y + i as u16, &format!("{}", elt), *w as usize, style); + buf.set_stringn(x, y + i as u16, format!("{}", elt), *w as usize, style); x += *w + self.column_spacing; } } diff --git a/src/widgets/tabs.rs b/src/widgets/tabs.rs index 4c1b086c..548c9802 100644 --- a/src/widgets/tabs.rs +++ b/src/widgets/tabs.rs @@ -105,9 +105,9 @@ where let mut x = tabs_area.left(); for (title, style) in self.titles.iter().enumerate().map(|(i, t)| { if i == self.selected { - (t, &self.highlight_style) + (t, self.highlight_style) } else { - (t, &self.style) + (t, self.style) } }) { x += 1;