Skip to content

Commit

Permalink
Refactor TickTimeLeft
Browse files Browse the repository at this point in the history
  • Loading branch information
mibac138 committed Mar 23, 2020
1 parent 0966eb2 commit 38e1956
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,18 +982,17 @@ impl MultiProgress {
};

let move_cursor = self.state.read().unwrap().move_cursor;
let time_left: TickTimeTracker = time_limit.into();
let time_tracker: TickTimeTracker = time_limit.into();
while !self.is_done() {
let (idx, draw_state) = if time_left.can_block() {
rx.recv().unwrap()
} else if time_left.can_probe() {
match rx.try_recv() {
let (idx, draw_state) = match time_tracker.time_left() {
TickTimeLeft::CanBlock => rx.recv().unwrap(),
TickTimeLeft::CanProbe => match rx.try_recv() {
Err(TryRecvError::Empty) => return Ok(()),
recv => recv.unwrap(),
}
} else {
return Ok(());
recv => recv.unwrap(),
},
TickTimeLeft::None => return Ok(()),
};

let ts = draw_state.ts;
let force_draw = draw_state.finished || draw_state.force_draw;

Expand Down Expand Up @@ -1086,20 +1085,27 @@ enum TickTimeTracker {
Deadline(Instant),
}

enum TickTimeLeft {
/// Caller can indefinitely block the thread waiting for updates
CanBlock,
/// Caller can block as long as there are continuous updates
CanProbe,
/// The allocated time has run out and the caller must return
None,
}

impl TickTimeTracker {
/// Returns true if the time limit allows for unlimited blocking (created by invoking `join`)
fn can_block(&self) -> bool {
match self {
TickTimeTracker::Unlimited => true,
_ => false,
}
}
/// Returns true if there is still time left and it's allowed to try to update and false if
/// the deadline has already been reached
fn can_probe(&self) -> bool {
fn time_left(&self) -> TickTimeLeft {
match self {
TickTimeTracker::Deadline(instant) => *instant > Instant::now(),
_ => true,
TickTimeTracker::Unlimited => TickTimeLeft::CanBlock,
TickTimeTracker::Deadline(deadline) => {
if *deadline > Instant::now() {
TickTimeLeft::CanProbe
} else {
TickTimeLeft::None
}
}
TickTimeTracker::Indefinite => TickTimeLeft::CanProbe,
}
}
}
Expand Down

0 comments on commit 38e1956

Please sign in to comment.