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(buffer): add Buffer::get_opt, get_mut_opt and index implementations #1084

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

joshka
Copy link
Member

@joshka joshka commented May 3, 2024

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.

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.
I'd anticipate these are widely used and we should leave these methods
around on the buffer for a very long time (perhaps 3-4 major releases rather
than the normal 2).

Partially addresses #1011

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("🐀"));
```
Copy link

codecov bot commented May 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.2%. Comparing base (7a48c5b) to head (903eea1).

Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1084     +/-   ##
=======================================
- Coverage   94.2%   94.2%   -0.1%     
=======================================
  Files         60      60             
  Lines      14511   14566     +55     
=======================================
+ Hits       13672   13722     +50     
- Misses       839     844      +5     

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

Copy link
Member

@EdJoPaTo EdJoPaTo left a 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:

pub const fn pos_of_opt(&self, i: usize) -> Option<(u16, u16)> {
if i < self.area.area() as usize {
Some((
self.area.x + (i as u16) % self.area.width,
self.area.y + (i as u16) / self.area.width,
))
} else {
None
}
}

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

joshka commented May 3, 2024

Is there a reason why this was not done with pos_of too?

I don't think pos_of should be part of the API, and I'd prefer not to make it worse. (note the new index_of_opt method is private)

src/buffer/buffer.rs Outdated Show resolved Hide resolved
@kdheepak
Copy link
Collaborator

This looks good to me but maybe we can get more reviewers on this?

PR title + description should be updated.

Copy link
Member

@EdJoPaTo EdJoPaTo left a 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.

src/buffer/buffer.rs Outdated Show resolved Hide resolved
src/buffer/buffer.rs Outdated Show resolved Hide resolved
src/buffer/buffer.rs Show resolved Hide resolved
Co-authored-by: EdJoPaTo <rfc-conform-git-commit-email@funny-long-domain-label-everyone-hates-as-it-is-too-long.edjopato.de>
@joshka
Copy link
Member Author

joshka commented May 22, 2024

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.

@joshka joshka requested review from EdJoPaTo and kdheepak May 25, 2024 23:29
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.

None yet

3 participants