-
-
Notifications
You must be signed in to change notification settings - Fork 362
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(buffer): add Buffer::cell, cell_mut and index implementations #1084
Conversation
Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)` can now use index operators, or be transitioned to `buf_get_opt` or `buf.get_mut_opt` for safe access that avoids panics. The new methods accept `Into<Position>` instead of `x` and `y` coordinates, which makes them more ergonomic to use. ```rust let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10)); let cell = buf[(0, 0)]; let cell = buf[Position::new(0, 0)]; let symbol = buf.get_opt((0, 0)).map(|cell| cell.symbol()); let symbol = buf.get_opt(Position::new(0, 0)).map(|cell| cell.symbol()); buf[(0, 0)].set_symbol("🐀"); buf[Position::new(0, 0)].set_symbol("🐀"); buf.get_mut_opt((0, 0)).map(|cell| cell.set_symbol("🐀")); buf.get_mut_opt(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀")); ```
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1084 +/- ##
=======================================
- Coverage 94.5% 94.5% -0.1%
=======================================
Files 65 65
Lines 15431 15488 +57
=======================================
+ Hits 14588 14637 +49
- Misses 843 851 +8 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this was not done with pos_of
too?
#1049: https://github.com/ratatui-org/ratatui/blob/30a6fb2e1335d408547f04d0c1a0f527d578e647/src/buffer/buffer.rs#L241-L250
I don't think |
This looks good to me but maybe we can get more reviewers on this? PR title + description should be updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the usage of the Index trait. It is still panicking like before but should have the same performance.
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
I actually intended to put the deprecation of the existing methods in #1123, but accidentally pushed the commit to this PR. Happy to do it either way (though it would be easier at this point to just keep it in this PR and update the main PR comment. |
Bencher
Click to view all benchmark results
Bencher - Continuous Benchmarking View Public Perf Page Docs | Repo | Chat | Help |
…atatui#1084) Code which previously called `buf.get(x, y)` or `buf.get_mut(x, y)` should now use index operators, or be transitioned to `buff.cell()` or `buf.cell_mut()` for safe access that avoids panics by returning `Option<&Cell>` and `Option<&mut Cell>`. The new methods accept `Into<Position>` instead of `x` and `y` coordinates, which makes them more ergonomic to use. ```rust let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 10)); let cell = buf[(0, 0)]; let cell = buf[Position::new(0, 0)]; let symbol = buf.cell((0, 0)).map(|cell| cell.symbol()); let symbol = buf.cell(Position::new(0, 0)).map(|cell| cell.symbol()); buf[(0, 0)].set_symbol("🐀"); buf[Position::new(0, 0)].set_symbol("🐀"); buf.cell_mut((0, 0)).map(|cell| cell.set_symbol("🐀")); buf.cell_mut(Position::new(0, 0)).map(|cell| cell.set_symbol("🐀")); ``` The existing `get()` and `get_mut()` methods are marked as deprecated. These are fairly widely used and we will leave these methods around on the buffer for a longer time than our normal deprecation approach (2 major release) Addresses part of: ratatui#1011 --------- Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
Code which previously called
buf.get(x, y)
orbuf.get_mut(x, y)
should now use index operators, or be transitioned to
buff.cell()
orbuf.cell_mut()
for safe access that avoids panics by returningOption<&Cell>
andOption<&mut Cell>
.The new methods accept
Into<Position>
instead ofx
andy
coordinates, which makes them more ergonomic to use.
The existing
get()
andget_mut()
methods are marked as deprecated.These are fairly widely used and we will leave these methods around on
the buffer for a longer time than our normal deprecation approach (2
major release)
Addresses part of: #1011