mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-24 09:55:08 +00:00
31 lines
814 B
Rust
31 lines
814 B
Rust
|
use std::sync::Arc;
|
||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||
|
|
||
|
/// 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);
|
||
|
}
|
||
|
}
|