cursive/src/utils/counter.rs

31 lines
814 B
Rust
Raw Normal View History

2018-04-10 18:45:02 +00:00
use std::sync::atomic::{AtomicUsize, Ordering};
2018-05-18 00:37:39 +00:00
use std::sync::Arc;
2018-04-10 18:45:02 +00:00
/// Atomic counter used by [`ProgressBar`].
///
/// [`ProgressBar`]: ../views/struct.ProgressBar.html
#[derive(Clone)]
pub struct Counter(pub Arc<AtomicUsize>);
impl Counter {
/// Creates a new `Counter` starting with the given value.
pub fn new(value: usize) -> Self {
Counter(Arc::new(AtomicUsize::new(value)))
}
/// Retrieves the current progress value.
pub fn get(&self) -> usize {
self.0.load(Ordering::Relaxed)
}
/// Sets the current progress value.
pub fn set(&self, value: usize) {
self.0.store(value, Ordering::Relaxed);
}
/// Increase the current progress by `ticks`.
pub fn tick(&self, ticks: usize) {
self.0.fetch_add(ticks, Ordering::Relaxed);
}
}