diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index bcedbeaa..8c335749 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -12,6 +12,7 @@ This is a quick summary of the sections below: - [Unreleased](#unreleased) - `Rect::inner` takes `Margin` directly instead of reference + - `Buffer::filled` takes `Cell` directly instead of reference - `Stylize::bg()` now accepts `Into` - Removed deprecated `List::start_corner` - [v0.26.0](#v0260) @@ -67,6 +68,17 @@ This is a quick summary of the sections below: }); ``` +### `Buffer::filled` takes `Cell` directly instead of reference ([#1148]) + +[#1148]: https://github.com/ratatui-org/ratatui/pull/1148 + +`Buffer::filled` moves the `Cell` instead of taking a reference. + +```diff +-Buffer::filled(area, &Cell::new("X")); ++Buffer::filled(area, Cell::new("X")); +``` + ### `Stylize::bg()` now accepts `Into` ([#1103]) [#1103]: https://github.com/ratatui-org/ratatui/pull/1103 diff --git a/src/buffer/buffer.rs b/src/buffer/buffer.rs index e460c79d..2af0e981 100644 --- a/src/buffer/buffer.rs +++ b/src/buffer/buffer.rs @@ -54,14 +54,14 @@ impl Buffer { /// Returns a Buffer with all cells set to the default one #[must_use] pub fn empty(area: Rect) -> Self { - Self::filled(area, &Cell::EMPTY) + Self::filled(area, Cell::EMPTY) } /// Returns a Buffer with all cells initialized with the attributes of the given Cell #[must_use] - pub fn filled(area: Rect, cell: &Cell) -> Self { + pub fn filled(area: Rect, cell: Cell) -> Self { let size = area.area() as usize; - let content = vec![cell.clone(); size]; + let content = vec![cell; size]; Self { area, content } } @@ -750,7 +750,7 @@ mod tests { fn diff_empty_filled() { let area = Rect::new(0, 0, 40, 40); let prev = Buffer::empty(area); - let next = Buffer::filled(area, &Cell::new("a")); + let next = Buffer::filled(area, Cell::new("a")); let diff = prev.diff(&next); assert_eq!(diff.len(), 40 * 40); } @@ -758,8 +758,8 @@ mod tests { #[test] fn diff_filled_filled() { let area = Rect::new(0, 0, 40, 40); - let prev = Buffer::filled(area, &Cell::new("a")); - let next = Buffer::filled(area, &Cell::new("a")); + let prev = Buffer::filled(area, Cell::new("a")); + let next = Buffer::filled(area, Cell::new("a")); let diff = prev.diff(&next); assert_eq!(diff, []); } @@ -853,8 +853,8 @@ mod tests { Lines: IntoIterator, Lines::Item: Into>, { - let mut one = Buffer::filled(one, &Cell::new("1")); - let two = Buffer::filled(two, &Cell::new("2")); + let mut one = Buffer::filled(one, Cell::new("1")); + let two = Buffer::filled(two, Cell::new("2")); one.merge(&two); assert_eq!(one, Buffer::with_lines(expected)); } @@ -868,7 +868,7 @@ mod tests { width: 2, height: 2, }, - &Cell::new("1"), + Cell::new("1"), ); let two = Buffer::filled( Rect { @@ -877,7 +877,7 @@ mod tests { width: 3, height: 4, }, - &Cell::new("2"), + Cell::new("2"), ); one.merge(&two); let mut expected = Buffer::with_lines(["222 ", "222 ", "2221", "2221"]); @@ -893,25 +893,29 @@ mod tests { #[rstest] #[case(false, true, [false, false, true, true, true, true])] #[case(true, false, [true, true, false, false, false, false])] - fn merge_skip(#[case] one: bool, #[case] two: bool, #[case] expected: [bool; 6]) { - let mut one = Buffer::filled( - Rect { + fn merge_skip(#[case] skip_one: bool, #[case] skip_two: bool, #[case] expected: [bool; 6]) { + let mut one = { + let area = Rect { x: 0, y: 0, width: 2, height: 2, - }, - Cell::new("1").set_skip(one), - ); - let two = Buffer::filled( - Rect { + }; + let mut cell = Cell::new("1"); + cell.skip = skip_one; + Buffer::filled(area, cell) + }; + let two = { + let area = Rect { x: 0, y: 1, width: 2, height: 2, - }, - Cell::new("2").set_skip(two), - ); + }; + let mut cell = Cell::new("2"); + cell.skip = skip_two; + Buffer::filled(area, cell) + }; one.merge(&two); let skipped = one.content().iter().map(|c| c.skip).collect::>(); assert_eq!(skipped, expected); diff --git a/src/text/line.rs b/src/text/line.rs index 5e45d6d4..be6b13f4 100644 --- a/src/text/line.rs +++ b/src/text/line.rs @@ -1169,7 +1169,7 @@ mod tests { fn render_truncates_away_from_0x0(#[case] alignment: Alignment, #[case] expected: &str) { let line = Line::from(vec![Span::raw("a🦀b"), Span::raw("c🦀d")]).alignment(alignment); // Fill buffer with stuff to ensure the output is indeed padded - let mut buf = Buffer::filled(Rect::new(0, 0, 10, 1), &Cell::new("X")); + let mut buf = Buffer::filled(Rect::new(0, 0, 10, 1), Cell::new("X")); let area = Rect::new(2, 0, 6, 1); line.render_ref(area, &mut buf); assert_eq!(buf, Buffer::with_lines([expected])); @@ -1188,7 +1188,7 @@ mod tests { let line = Line::from(vec![Span::raw("a🦀b"), Span::raw("c🦀d")]).right_aligned(); let area = Rect::new(0, 0, buf_width, 1); // Fill buffer with stuff to ensure the output is indeed padded - let mut buf = Buffer::filled(area, &Cell::new("X")); + let mut buf = Buffer::filled(area, Cell::new("X")); line.render_ref(buf.area, &mut buf); assert_eq!(buf, Buffer::with_lines([expected])); } diff --git a/src/widgets/canvas.rs b/src/widgets/canvas.rs index c2991af8..d51dc3f3 100644 --- a/src/widgets/canvas.rs +++ b/src/widgets/canvas.rs @@ -817,7 +817,7 @@ mod tests { // results in the expected output fn test_marker(marker: Marker, expected: &str) { let area = Rect::new(0, 0, 5, 5); - let mut buf = Buffer::filled(area, &Cell::new("x")); + let mut buf = Buffer::filled(area, Cell::new("x")); let horizontal_line = Line { x1: 0.0, y1: 0.0, diff --git a/src/widgets/sparkline.rs b/src/widgets/sparkline.rs index 363d84dc..f690286e 100644 --- a/src/widgets/sparkline.rs +++ b/src/widgets/sparkline.rs @@ -239,7 +239,7 @@ mod tests { // filled with x symbols to make it easier to assert on the result fn render(widget: Sparkline, width: u16) -> Buffer { let area = Rect::new(0, 0, width, 1); - let mut buffer = Buffer::filled(area, &Cell::new("x")); + let mut buffer = Buffer::filled(area, Cell::new("x")); widget.render(area, &mut buffer); buffer }