Avoid unneeded castings

This commit is contained in:
mo8it 2026-03-14 17:36:18 +01:00
parent 337f6b1521
commit ceb98475e2
4 changed files with 12 additions and 13 deletions

View File

@ -52,8 +52,8 @@ pub enum CheckProgress {
pub struct AppState { pub struct AppState {
current_exercise_ind: usize, current_exercise_ind: usize,
exercises: Vec<Exercise>, exercises: Vec<Exercise>,
// Caches the number of done exercises to avoid iterating over all exercises every time. // Cache the number of done exercises to avoid iterating over all exercises every time.
n_done: u16, n_done: u32,
final_message: &'static str, final_message: &'static str,
state_file: File, state_file: File,
// Preallocated buffer for reading and writing the state file. // Preallocated buffer for reading and writing the state file.
@ -191,13 +191,13 @@ impl AppState {
} }
#[inline] #[inline]
pub fn n_done(&self) -> u16 { pub fn n_done(&self) -> u32 {
self.n_done self.n_done
} }
#[inline] #[inline]
pub fn n_pending(&self) -> u16 { pub fn n_pending(&self) -> u32 {
self.exercises.len() as u16 - self.n_done self.exercises.len() as u32 - self.n_done
} }
#[inline] #[inline]

View File

@ -229,7 +229,7 @@ impl<'a> ListState<'a> {
progress_bar( progress_bar(
&mut MaxLenWriter::new(stdout, self.term_width as usize), &mut MaxLenWriter::new(stdout, self.term_width as usize),
self.app_state.n_done(), self.app_state.n_done(),
self.app_state.exercises().len() as u16, self.app_state.exercises().len() as u32,
self.term_width, self.term_width,
)?; )?;
next_ln(stdout)?; next_ln(stdout)?;

View File

@ -193,8 +193,8 @@ impl Drop for ProgressCounter<'_, '_> {
pub fn progress_bar<'a>( pub fn progress_bar<'a>(
writer: &mut impl CountedWrite<'a>, writer: &mut impl CountedWrite<'a>,
progress: u16, progress: u32,
total: u16, total: u32,
term_width: u16, term_width: u16,
) -> io::Result<()> { ) -> io::Result<()> {
const PREFIX: &[u8] = b"Progress: ["; const PREFIX: &[u8] = b"Progress: [";
@ -215,10 +215,9 @@ pub fn progress_bar<'a>(
let stdout = writer.stdout(); let stdout = writer.stdout();
stdout.write_all(PREFIX)?; stdout.write_all(PREFIX)?;
let width = term_width - WRAPPER_WIDTH; // Use u32 to prevent the intermediate multiplication from overflowing
// Use u32 to prevent the intermediate multiplication from overflowing u16 let width = u32::from(term_width - WRAPPER_WIDTH);
let filled = (width as u32 * progress as u32) / total as u32; let filled = (width * progress) / total;
let filled = filled as u16;
stdout.queue(SetForegroundColor(Color::Green))?; stdout.queue(SetForegroundColor(Color::Green))?;
for _ in 0..filled { for _ in 0..filled {

View File

@ -245,7 +245,7 @@ impl<'a> WatchState<'a> {
progress_bar( progress_bar(
stdout, stdout,
self.app_state.n_done(), self.app_state.n_done(),
self.app_state.exercises().len() as u16, self.app_state.exercises().len() as u32,
self.term_width, self.term_width,
)?; )?;