Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cell): add EMPTY and (const) new method #1143

Merged
merged 4 commits into from
May 25, 2024

Conversation

EdJoPaTo
Copy link
Contributor

This simplifies calls to Buffer::filled in tests.

Copy link

codecov bot commented May 25, 2024

Codecov Report

Attention: Patch coverage is 98.07692% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 94.2%. Comparing base (4f77910) to head (def27fa).
Report is 3 commits behind head on main.

Files Patch % Lines
src/buffer/cell.rs 92.8% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1143     +/-   ##
=======================================
- Coverage   94.2%   94.2%   -0.1%     
=======================================
  Files         60      60             
  Lines      14623   14511    -112     
=======================================
- Hits       13780   13672    -108     
+ Misses       843     839      -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EdJoPaTo
Copy link
Contributor Author

EdJoPaTo commented May 25, 2024

A (breaking) follow up to this will be the following performance improvement (removes one needless clone / drop):

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

As this would result in a bunch of conflicts (and is breaking) I'll wait with that until this one is sorted out.

src/buffer/cell.rs Outdated Show resolved Hide resolved
@joshka
Copy link
Member

joshka commented May 25, 2024

A (breaking) follow up to this will be the following performance improvement (removes one needless clone / drop):

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

+1 to this - looks like a good idea. It's breaking but in a function that's only really useful for testing, so that's not too bad.
https://github.com/search?q=lang%3Arust+ratatui+%22Buffer%3A%3Afilled%22+AND+%28NOT+org%3Aratatui-org%29+AND+%28NOT+is%3Afork%29&type=code

Copy link
Member

@joshka joshka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another idea that springs from this - doing a quick search suggests perhaps a Cell::styled() constructor could be useful

// instead of:
buf.get_mut(x,y)
  .set_symbol("x")
  .set_style(style);
// what about:
buf[(x,y)] = Cell::styled("x", style);

I'm not sure what impact this would have on allocations (likely many) / perf (likely little). Doesn't have to be part of this PR at all, but I'm curious to hear your thoughts on this.

@EdJoPaTo
Copy link
Contributor Author

Another idea that springs from this - doing a quick search suggests perhaps a Cell::styled() constructor could be useful

// instead of:
buf.get_mut(x,y)
  .set_symbol("x")
  .set_style(style);
// what about:
buf[(x,y)] = Cell::styled("x", style);

I'm not sure what impact this would have on allocations (likely many) / perf (likely little). Doesn't have to be part of this PR at all, but I'm curious to hear your thoughts on this.

set_styled could be a neat idea for simpler code. For performance it would be the same as the two methods set_symbol / set_style.
Using a constructor and overriding it seems weird, this should stick with &mut.

The constructor in this PR is mainly simplifying tests.

@joshka joshka merged commit 7a48c5b into ratatui:main May 25, 2024
33 checks passed
@EdJoPaTo EdJoPaTo deleted the cell-constructor branch May 26, 2024 17:14
joshka pushed a commit to erak/ratatui that referenced this pull request Oct 14, 2024
This simplifies calls to `Buffer::filled` in tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants