Skip to content

Commit

Permalink
perf(buffer)!: filled moves the cell to be filled (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed May 27, 2024
1 parent df4b706 commit 4ce67fc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
12 changes: 12 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
46 changes: 25 additions & 21 deletions src/buffer/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down Expand Up @@ -750,16 +750,16 @@ 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);
}

#[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, []);
}
Expand Down Expand Up @@ -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));
}
Expand All @@ -868,7 +868,7 @@ mod tests {
width: 2,
height: 2,
},
&Cell::new("1"),
Cell::new("1"),
);
let two = Buffer::filled(
Rect {
Expand All @@ -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"]);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand All @@ -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]));
}
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/sparkline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 4ce67fc

Please sign in to comment.