Skip to content

Commit

Permalink
AtomicPosition: replace default with new
Browse files Browse the repository at this point in the history
The `Default` implementation was confusing to me. I would expect that `AtomicPosition::default() == AtomicPosition::default()` in all cases.
  • Loading branch information
arxanas committed Mar 20, 2022
1 parent b3a7e0a commit c36af4a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ProgressBar {

/// Creates a new progress bar with a given length and draw target
pub fn with_draw_target(len: u64, draw_target: ProgressDrawTarget) -> ProgressBar {
let pos = Arc::new(AtomicPosition::default());
let pos = Arc::new(AtomicPosition::new());
ProgressBar {
state: Arc::new(Mutex::new(BarState::new(len, draw_target, pos.clone()))),
pos,
Expand Down
20 changes: 9 additions & 11 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@ pub(crate) struct AtomicPosition {
}

impl AtomicPosition {
pub(crate) fn new() -> Self {
Self {
pos: AtomicU64::new(0),
capacity: AtomicU8::new(MAX_BURST),
prev: AtomicU64::new(0),
start: Instant::now(),
}
}

pub(crate) fn allow(&self, now: Instant) -> bool {
if now < self.start {
return false;
Expand Down Expand Up @@ -444,17 +453,6 @@ impl AtomicPosition {
}
}

impl Default for AtomicPosition {
fn default() -> Self {
Self {
pos: AtomicU64::new(0),
capacity: AtomicU8::new(MAX_BURST),
prev: AtomicU64::new(0),
start: Instant::now(),
}
}
}

const INTERVAL: u64 = 1_000_000;
const MAX_BURST: u8 = 10;

Expand Down
6 changes: 3 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ mod tests {
#[test]
fn test_expand_template() {
const WIDTH: u16 = 80;
let pos = Arc::new(AtomicPosition::default());
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(10, pos);
let mut buf = Vec::new();

Expand All @@ -705,7 +705,7 @@ mod tests {
set_colors_enabled(true);

const WIDTH: u16 = 80;
let pos = Arc::new(AtomicPosition::default());
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(10, pos);
let mut buf = Vec::new();

Expand Down Expand Up @@ -735,7 +735,7 @@ mod tests {
#[test]
fn align_truncation() {
const WIDTH: u16 = 10;
let pos = Arc::new(AtomicPosition::default());
let pos = Arc::new(AtomicPosition::new());
let state = ProgressState::new(10, pos);
let mut buf = Vec::new();

Expand Down

0 comments on commit c36af4a

Please sign in to comment.