Use saturating add/sub
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m4s
Cargo CI / Lint (pull_request) Successful in 43s

This commit is contained in:
Wojciech Kozlowski 2024-02-05 23:40:02 +01:00
parent 4f9ede294a
commit 5c36a7379a
2 changed files with 17 additions and 17 deletions

View File

@ -56,7 +56,7 @@ impl Delta {
fn as_usize(&self, state: &WidgetState) -> usize { fn as_usize(&self, state: &WidgetState) -> usize {
match self { match self {
Delta::Line => 1, Delta::Line => 1,
Delta::Page => state.height.checked_sub(1).unwrap_or(0), Delta::Page => state.height.saturating_sub(1),
} }
} }
} }
@ -174,10 +174,10 @@ impl ArtistSelection {
fn increment_by(&mut self, artists: &[Artist], by: usize) { fn increment_by(&mut self, artists: &[Artist], by: usize) {
if let Some(index) = self.state.list.selected() { if let Some(index) = self.state.list.selected() {
let result = index let mut result = index.saturating_add(by);
.checked_add(by) if result >= artists.len() {
.filter(|i| i < &artists.len()) result = artists.len() - 1;
.unwrap_or_else(|| artists.len() - 1); }
if self.state.list.selected() != Some(result) { if self.state.list.selected() != Some(result) {
self.state.list.select(Some(result)); self.state.list.select(Some(result));
self.album = AlbumSelection::initialise(&artists[result].albums); self.album = AlbumSelection::initialise(&artists[result].albums);
@ -203,7 +203,7 @@ impl ArtistSelection {
fn decrement_by(&mut self, artists: &[Artist], by: usize) { fn decrement_by(&mut self, artists: &[Artist], by: usize) {
if let Some(index) = self.state.list.selected() { if let Some(index) = self.state.list.selected() {
let result = index.checked_sub(by).unwrap_or(0); let result = index.saturating_sub(by);
if self.state.list.selected() != Some(result) { if self.state.list.selected() != Some(result) {
self.state.list.select(Some(result)); self.state.list.select(Some(result));
self.album = AlbumSelection::initialise(&artists[result].albums); self.album = AlbumSelection::initialise(&artists[result].albums);
@ -271,10 +271,10 @@ impl AlbumSelection {
fn increment_by(&mut self, albums: &[Album], by: usize) { fn increment_by(&mut self, albums: &[Album], by: usize) {
if let Some(index) = self.state.list.selected() { if let Some(index) = self.state.list.selected() {
let result = index let mut result = index.saturating_add(by);
.checked_add(by) if result >= albums.len() {
.filter(|i| i < &albums.len()) result = albums.len() - 1;
.unwrap_or_else(|| albums.len() - 1); }
if self.state.list.selected() != Some(result) { if self.state.list.selected() != Some(result) {
self.state.list.select(Some(result)); self.state.list.select(Some(result));
self.track = TrackSelection::initialise(&albums[result].tracks); self.track = TrackSelection::initialise(&albums[result].tracks);
@ -294,7 +294,7 @@ impl AlbumSelection {
fn decrement_by(&mut self, albums: &[Album], by: usize) { fn decrement_by(&mut self, albums: &[Album], by: usize) {
if let Some(index) = self.state.list.selected() { if let Some(index) = self.state.list.selected() {
let result = index.checked_sub(by).unwrap_or(0); let result = index.saturating_sub(by);
if self.state.list.selected() != Some(result) { if self.state.list.selected() != Some(result) {
self.state.list.select(Some(result)); self.state.list.select(Some(result));
self.track = TrackSelection::initialise(&albums[result].tracks); self.track = TrackSelection::initialise(&albums[result].tracks);
@ -345,10 +345,10 @@ impl TrackSelection {
fn increment_by(&mut self, tracks: &[Track], by: usize) { fn increment_by(&mut self, tracks: &[Track], by: usize) {
if let Some(index) = self.state.list.selected() { if let Some(index) = self.state.list.selected() {
let result = index let mut result = index.saturating_add(by);
.checked_add(by) if result >= tracks.len() {
.filter(|i| i < &tracks.len()) result = tracks.len() - 1;
.unwrap_or_else(|| tracks.len() - 1); }
if self.state.list.selected() != Some(result) { if self.state.list.selected() != Some(result) {
self.state.list.select(Some(result)); self.state.list.select(Some(result));
} }
@ -361,7 +361,7 @@ impl TrackSelection {
fn decrement_by(&mut self, _tracks: &[Track], by: usize) { fn decrement_by(&mut self, _tracks: &[Track], by: usize) {
if let Some(index) = self.state.list.selected() { if let Some(index) = self.state.list.selected() {
let result = index.checked_sub(by).unwrap_or(0); let result = index.saturating_sub(by);
if self.state.list.selected() != Some(result) { if self.state.list.selected() != Some(result) {
self.state.list.select(Some(result)); self.state.list.select(Some(result));
} }

View File

@ -355,7 +355,7 @@ impl Ui {
area, area,
&mut state.list, &mut state.list,
); );
state.height = area.height.checked_sub(2).unwrap_or(0) as usize; state.height = area.height.saturating_sub(2) as usize;
} }
fn render_info_widget<B: Backend>( fn render_info_widget<B: Backend>(