perf(buffer)!: filled moves the cell to be filled (#1148)

This commit is contained in:
EdJoPaTo 2024-05-27 20:07:27 +02:00 committed by GitHub
parent df4b706674
commit 4ce67fc84e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 41 additions and 25 deletions

View file

@ -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<Color>`
- 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<Color>` ([#1103])
[#1103]: https://github.com/ratatui-org/ratatui/pull/1103

View file

@ -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<Line<'line>>,
{
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::<Vec<_>>();
assert_eq!(skipped, expected);

View file

@ -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]));
}

View file

@ -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,

View file

@ -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
}