From 8b447ec4d6276c3110285e663417487ff18dafc1 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Sun, 26 May 2024 19:44:46 +0200 Subject: [PATCH] perf(rect)!: `Rect::inner` takes `Margin` directly instead of reference (#1008) BREAKING CHANGE: Margin needs to be passed without reference now. ```diff -let area = area.inner(&Margin { +let area = area.inner(Margin { vertical: 0, horizontal: 2, }); ``` --- BREAKING-CHANGES.md | 27 ++++++++++++++++++++------- examples/demo2/tabs/about.rs | 22 +++++++++------------- examples/demo2/tabs/email.rs | 2 +- examples/demo2/tabs/recipe.rs | 4 ++-- examples/demo2/tabs/traceroute.rs | 2 +- examples/demo2/tabs/weather.rs | 4 ++-- examples/scrollbar.rs | 6 +++--- examples/table.rs | 2 +- src/layout/layout.rs | 2 +- src/layout/rect.rs | 5 ++--- src/widgets/canvas/rectangle.rs | 10 +++++----- src/widgets/scrollbar.rs | 2 +- 12 files changed, 48 insertions(+), 40 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index bb96e597..bcedbeaa 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -11,8 +11,8 @@ GitHub with a [breaking change] label. This is a quick summary of the sections below: - [Unreleased](#unreleased) + - `Rect::inner` takes `Margin` directly instead of reference - `Stylize::bg()` now accepts `Into` -- [v0.27.0 (unreleased)](#v0270-unreleased) - Removed deprecated `List::start_corner` - [v0.26.0](#v0260) - `Flex::Start` is the new default flex mode for `Layout` @@ -53,17 +53,30 @@ This is a quick summary of the sections below: ## Unreleased +### `Rect::inner` takes `Margin` directly instead of reference ([#1008]) + +[#1008]: https://github.com/ratatui-org/ratatui/pull/1008 + +`Margin` needs to be passed without reference now. + +```diff +-let area = area.inner(&Margin { ++let area = area.inner(Margin { + vertical: 0, + horizontal: 2, + }); +``` + ### `Stylize::bg()` now accepts `Into` ([#1103]) -[#1099]: https://github.com/ratatui-org/ratatui/pull/1103 +[#1103]: https://github.com/ratatui-org/ratatui/pull/1103 -Previously, `Stylize::bg()` accepted `Color` but now accepts `Into`. This allows more flexible types from calling scopes, though it can break some type inference in the calling scope. +Previously, `Stylize::bg()` accepted `Color` but now accepts `Into`. This allows more +flexible types from calling scopes, though it can break some type inference in the calling scope. -## v0.27.0 (unreleased) +### Remove deprecated `List::start_corner` and `layout::Corner` ([#757]) -### Remove deprecated `List::start_corner` and `layout::Corner` ([#758]) - -[#758]: https://github.com/ratatui-org/ratatui/pull/757 +[#757]: https://github.com/ratatui-org/ratatui/pull/757 `List::start_corner` was deprecated in v0.25. Use `List::direction` and `ListDirection` instead. diff --git a/examples/demo2/tabs/about.rs b/examples/demo2/tabs/about.rs index cf669bf6..802bff51 100644 --- a/examples/demo2/tabs/about.rs +++ b/examples/demo2/tabs/about.rs @@ -64,20 +64,16 @@ impl Widget for AboutTab { } fn render_crate_description(area: Rect, buf: &mut Buffer) { - let area = area.inner( - &(Margin { - vertical: 4, - horizontal: 2, - }), - ); + let area = area.inner(Margin { + vertical: 4, + horizontal: 2, + }); Clear.render(area, buf); // clear out the color swatches Block::new().style(THEME.content).render(area, buf); - let area = area.inner( - &(Margin { - vertical: 1, - horizontal: 2, - }), - ); + let area = area.inner(Margin { + vertical: 1, + horizontal: 2, + }); let text = "- cooking up terminal user interfaces - Ratatui is a Rust crate that provides widgets (e.g. Paragraph, Table) and draws them to the \ @@ -108,7 +104,7 @@ pub fn render_logo(selected_row: usize, area: Rect, buf: &mut Buffer) { } else { THEME.logo.rat_eye_alt }; - let area = area.inner(&Margin { + let area = area.inner(Margin { vertical: 0, horizontal: 2, }); diff --git a/examples/demo2/tabs/email.rs b/examples/demo2/tabs/email.rs index 4ec8e812..2a5e2a0d 100644 --- a/examples/demo2/tabs/email.rs +++ b/examples/demo2/tabs/email.rs @@ -59,7 +59,7 @@ impl EmailTab { impl Widget for EmailTab { fn render(self, area: Rect, buf: &mut Buffer) { RgbSwatch.render(area, buf); - let area = area.inner(&Margin { + let area = area.inner(Margin { vertical: 1, horizontal: 2, }); diff --git a/examples/demo2/tabs/recipe.rs b/examples/demo2/tabs/recipe.rs index 8ac6cdc4..c38945ec 100644 --- a/examples/demo2/tabs/recipe.rs +++ b/examples/demo2/tabs/recipe.rs @@ -105,7 +105,7 @@ impl RecipeTab { impl Widget for RecipeTab { fn render(self, area: Rect, buf: &mut Buffer) { RgbSwatch.render(area, buf); - let area = area.inner(&Margin { + let area = area.inner(Margin { vertical: 1, horizontal: 2, }); @@ -124,7 +124,7 @@ impl Widget for RecipeTab { }; render_scrollbar(self.row_index, scrollbar_area, buf); - let area = area.inner(&Margin { + let area = area.inner(Margin { horizontal: 2, vertical: 1, }); diff --git a/examples/demo2/tabs/traceroute.rs b/examples/demo2/tabs/traceroute.rs index edaeb6cd..cd5bc5c0 100644 --- a/examples/demo2/tabs/traceroute.rs +++ b/examples/demo2/tabs/traceroute.rs @@ -26,7 +26,7 @@ impl TracerouteTab { impl Widget for TracerouteTab { fn render(self, area: Rect, buf: &mut Buffer) { RgbSwatch.render(area, buf); - let area = area.inner(&Margin { + let area = area.inner(Margin { vertical: 1, horizontal: 2, }); diff --git a/examples/demo2/tabs/weather.rs b/examples/demo2/tabs/weather.rs index 9878a656..439c9ed3 100644 --- a/examples/demo2/tabs/weather.rs +++ b/examples/demo2/tabs/weather.rs @@ -28,14 +28,14 @@ impl WeatherTab { impl Widget for WeatherTab { fn render(self, area: Rect, buf: &mut Buffer) { RgbSwatch.render(area, buf); - let area = area.inner(&Margin { + let area = area.inner(Margin { vertical: 1, horizontal: 2, }); Clear.render(area, buf); Block::new().style(THEME.content).render(area, buf); - let area = area.inner(&Margin { + let area = area.inner(Margin { horizontal: 2, vertical: 1, }); diff --git a/examples/scrollbar.rs b/examples/scrollbar.rs index 5ed7c0fd..4777ab7f 100644 --- a/examples/scrollbar.rs +++ b/examples/scrollbar.rs @@ -186,7 +186,7 @@ fn ui(f: &mut Frame, app: &mut App) { .begin_symbol(None) .track_symbol(None) .end_symbol(None), - chunks[2].inner(&Margin { + chunks[2].inner(Margin { vertical: 1, horizontal: 0, }), @@ -204,7 +204,7 @@ fn ui(f: &mut Frame, app: &mut App) { Scrollbar::new(ScrollbarOrientation::HorizontalBottom) .thumb_symbol("🬋") .end_symbol(None), - chunks[3].inner(&Margin { + chunks[3].inner(Margin { vertical: 0, horizontal: 1, }), @@ -222,7 +222,7 @@ fn ui(f: &mut Frame, app: &mut App) { Scrollbar::new(ScrollbarOrientation::HorizontalBottom) .thumb_symbol("░") .track_symbol(Some("─")), - chunks[4].inner(&Margin { + chunks[4].inner(Margin { vertical: 0, horizontal: 1, }), diff --git a/examples/table.rs b/examples/table.rs index b74bd3c6..b2c0fcd1 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -318,7 +318,7 @@ fn render_scrollbar(f: &mut Frame, app: &mut App, area: Rect) { .orientation(ScrollbarOrientation::VerticalRight) .begin_symbol(None) .end_symbol(None), - area.inner(&Margin { + area.inner(Margin { vertical: 1, horizontal: 1, }), diff --git a/src/layout/layout.rs b/src/layout/layout.rs index 55734750..e6799fd7 100644 --- a/src/layout/layout.rs +++ b/src/layout/layout.rs @@ -608,7 +608,7 @@ impl Layout { // This is equivalent to storing the solver in `Layout` and calling `solver.reset()` here. let mut solver = Solver::new(); - let inner_area = area.inner(&self.margin); + let inner_area = area.inner(self.margin); let (area_start, area_end) = match self.direction { Direction::Horizontal => ( f64::from(inner_area.x) * FLOAT_PRECISION_MULTIPLIER, diff --git a/src/layout/rect.rs b/src/layout/rect.rs index 3e4a3e94..17b0a08d 100644 --- a/src/layout/rect.rs +++ b/src/layout/rect.rs @@ -119,9 +119,8 @@ impl Rect { /// Returns a new `Rect` inside the current one, with the given margin on each side. /// /// If the margin is larger than the `Rect`, the returned `Rect` will have no area. - #[allow(clippy::trivially_copy_pass_by_ref)] // See PR #1008 #[must_use = "method returns the modified value"] - pub const fn inner(self, margin: &Margin) -> Self { + pub const fn inner(self, margin: Margin) -> Self { let doubled_margin_horizontal = margin.horizontal.saturating_mul(2); let doubled_margin_vertical = margin.vertical.saturating_mul(2); @@ -406,7 +405,7 @@ mod tests { #[test] fn inner() { assert_eq!( - Rect::new(1, 2, 3, 4).inner(&Margin::new(1, 2)), + Rect::new(1, 2, 3, 4).inner(Margin::new(1, 2)), Rect::new(2, 4, 1, 0) ); } diff --git a/src/widgets/canvas/rectangle.rs b/src/widgets/canvas/rectangle.rs index cdfc32a9..93496f03 100644 --- a/src/widgets/canvas/rectangle.rs +++ b/src/widgets/canvas/rectangle.rs @@ -98,7 +98,7 @@ mod tests { "██████████", ]); expected.set_style(buffer.area, Style::new().red()); - expected.set_style(buffer.area.inner(&Margin::new(1, 1)), Style::reset()); + expected.set_style(buffer.area.inner(Margin::new(1, 1)), Style::reset()); assert_eq!(buffer, expected); } @@ -132,8 +132,8 @@ mod tests { "█▄▄▄▄▄▄▄▄█", ]); expected.set_style(buffer.area, Style::new().red().on_red()); - expected.set_style(buffer.area.inner(&Margin::new(1, 0)), Style::reset().red()); - expected.set_style(buffer.area.inner(&Margin::new(1, 1)), Style::reset()); + expected.set_style(buffer.area.inner(Margin::new(1, 0)), Style::reset().red()); + expected.set_style(buffer.area.inner(Margin::new(1, 1)), Style::reset()); assert_eq!(buffer, expected); } @@ -176,8 +176,8 @@ mod tests { "⣇⣀⣀⣀⣀⣀⣀⣀⣀⣸", ]); expected.set_style(buffer.area, Style::new().red()); - expected.set_style(buffer.area.inner(&Margin::new(1, 1)), Style::new().green()); - expected.set_style(buffer.area.inner(&Margin::new(2, 2)), Style::reset()); + expected.set_style(buffer.area.inner(Margin::new(1, 1)), Style::new().green()); + expected.set_style(buffer.area.inner(Margin::new(2, 2)), Style::reset()); assert_eq!(buffer, expected); } } diff --git a/src/widgets/scrollbar.rs b/src/widgets/scrollbar.rs index cec37a27..e679f3b0 100644 --- a/src/widgets/scrollbar.rs +++ b/src/widgets/scrollbar.rs @@ -63,7 +63,7 @@ use crate::{prelude::*, symbols::scrollbar::*}; /// // and the scrollbar, those are separate widgets /// frame.render_stateful_widget( /// scrollbar, -/// area.inner(&Margin { +/// area.inner(Margin { /// // using an inner vertical margin of 1 unit makes the scrollbar inside the block /// vertical: 1, /// horizontal: 0,