use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Debug)]
pub struct Progress {
total: AtomicU64,
current: AtomicU64,
}
impl Progress {
pub fn new() -> Self {
Self {
total: AtomicU64::new(0),
current: AtomicU64::new(0),
}
}
pub fn with_total(total: u64) -> Self {
Self {
total: AtomicU64::new(total),
current: AtomicU64::new(0),
}
}
pub fn set_total(&self, total: u64) {
self.total.store(total, Ordering::Relaxed);
}
pub fn bump(&self) {
self.current.fetch_add(1, Ordering::Relaxed);
}
pub fn bump_by(&self, n: u64) {
self.current.fetch_add(n, Ordering::Relaxed);
}
pub fn total(&self) -> u64 {
self.total.load(Ordering::Relaxed)
}
pub fn current(&self) -> u64 {
self.current.load(Ordering::Relaxed)
}
pub fn fraction(&self) -> f64 {
let total = self.total();
if total == 0 {
return 0.0;
}
(self.current() as f64 / total as f64).clamp(0.0, 1.0)
}
}
impl Default for Progress {
fn default() -> Self {
Self::new()
}
}