fix progress 0/0 panic

This commit is contained in:
Stephan Dilly 2021-08-15 17:42:25 +02:00
parent 47ff93734e
commit 62ea1dea04

View file

@ -15,7 +15,7 @@ impl ProgressPercent {
pub fn new(current: usize, total: usize) -> Self {
let total = f64::conv(cmp::max(current, total));
let progress = f64::conv(current) / total * 100.0;
let progress = u8::conv_nearest(progress);
let progress = u8::try_conv_nearest(progress).unwrap_or(100);
Self { progress }
}
///
@ -39,6 +39,12 @@ mod tests {
assert_eq!(prog.progress, 100);
}
#[test]
fn test_progress_zero_all() {
let prog = ProgressPercent::new(0, 0);
assert_eq!(prog.progress, 100);
}
#[test]
fn test_progress_rounding() {
let prog = ProgressPercent::new(2, 10);