mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-03-31 03:29:19 +00:00
Avoid unneeded castings
This commit is contained in:
parent
337f6b1521
commit
ceb98475e2
@ -52,8 +52,8 @@ pub enum CheckProgress {
|
||||
pub struct AppState {
|
||||
current_exercise_ind: usize,
|
||||
exercises: Vec<Exercise>,
|
||||
// Caches the number of done exercises to avoid iterating over all exercises every time.
|
||||
n_done: u16,
|
||||
// Cache the number of done exercises to avoid iterating over all exercises every time.
|
||||
n_done: u32,
|
||||
final_message: &'static str,
|
||||
state_file: File,
|
||||
// Preallocated buffer for reading and writing the state file.
|
||||
@ -191,13 +191,13 @@ impl AppState {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn n_done(&self) -> u16 {
|
||||
pub fn n_done(&self) -> u32 {
|
||||
self.n_done
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn n_pending(&self) -> u16 {
|
||||
self.exercises.len() as u16 - self.n_done
|
||||
pub fn n_pending(&self) -> u32 {
|
||||
self.exercises.len() as u32 - self.n_done
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
@ -229,7 +229,7 @@ impl<'a> ListState<'a> {
|
||||
progress_bar(
|
||||
&mut MaxLenWriter::new(stdout, self.term_width as usize),
|
||||
self.app_state.n_done(),
|
||||
self.app_state.exercises().len() as u16,
|
||||
self.app_state.exercises().len() as u32,
|
||||
self.term_width,
|
||||
)?;
|
||||
next_ln(stdout)?;
|
||||
|
||||
11
src/term.rs
11
src/term.rs
@ -193,8 +193,8 @@ impl Drop for ProgressCounter<'_, '_> {
|
||||
|
||||
pub fn progress_bar<'a>(
|
||||
writer: &mut impl CountedWrite<'a>,
|
||||
progress: u16,
|
||||
total: u16,
|
||||
progress: u32,
|
||||
total: u32,
|
||||
term_width: u16,
|
||||
) -> io::Result<()> {
|
||||
const PREFIX: &[u8] = b"Progress: [";
|
||||
@ -215,10 +215,9 @@ pub fn progress_bar<'a>(
|
||||
let stdout = writer.stdout();
|
||||
stdout.write_all(PREFIX)?;
|
||||
|
||||
let width = term_width - WRAPPER_WIDTH;
|
||||
// Use u32 to prevent the intermediate multiplication from overflowing u16
|
||||
let filled = (width as u32 * progress as u32) / total as u32;
|
||||
let filled = filled as u16;
|
||||
// Use u32 to prevent the intermediate multiplication from overflowing
|
||||
let width = u32::from(term_width - WRAPPER_WIDTH);
|
||||
let filled = (width * progress) / total;
|
||||
|
||||
stdout.queue(SetForegroundColor(Color::Green))?;
|
||||
for _ in 0..filled {
|
||||
|
||||
@ -245,7 +245,7 @@ impl<'a> WatchState<'a> {
|
||||
progress_bar(
|
||||
stdout,
|
||||
self.app_state.n_done(),
|
||||
self.app_state.exercises().len() as u16,
|
||||
self.app_state.exercises().len() as u32,
|
||||
self.term_width,
|
||||
)?;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user