pub struct CountdownWait<'a> { /* private fields */ }Expand description
A blocking wait that reports the time left every tick and stops early
when a CancelToken is cancelled.
Time is counted from the sleeps themselves, so an injected sleeper makes the wait instant and deterministic:
use std::time::Duration;
use rich_ext::countdown::{CountdownWait, WaitOutcome};
let mut ticks = Vec::new();
let outcome = CountdownWait::new(Duration::from_secs(2))
.tick(Duration::from_secs(1))
.sleeper(|_| {})
.run(|left| ticks.push(left.as_secs()));
assert_eq!(outcome, WaitOutcome::Elapsed);
assert_eq!(ticks, [2, 1, 0]);Implementations§
Source§impl<'a> CountdownWait<'a>
impl<'a> CountdownWait<'a>
Sourcepub fn tick(self, tick: Duration) -> Self
pub fn tick(self, tick: Duration) -> Self
Report (and check for cancellation) every tick (at least 1 ms).
Sourcepub fn cancel(self, token: CancelToken) -> Self
pub fn cancel(self, token: CancelToken) -> Self
Stop early once token is cancelled.
Sourcepub fn sleeper(self, sleep: impl FnMut(Duration) + 'a) -> Self
pub fn sleeper(self, sleep: impl FnMut(Duration) + 'a) -> Self
Sleep with sleep instead of std::thread::sleep.
Sourcepub fn run(self, on_tick: impl FnMut(Duration)) -> WaitOutcome
pub fn run(self, on_tick: impl FnMut(Duration)) -> WaitOutcome
Wait, calling on_tick with the time left before each sleep and once
with zero at the end.
Sourcepub fn run_live<W: Write, R: Renderable>(
self,
live: &mut LiveCoordinator<W>,
target: &RenderTarget,
motion: Motion,
frame: impl Fn(Duration) -> R,
) -> Result<WaitOutcome, LiveError>
pub fn run_live<W: Write, R: Renderable>( self, live: &mut LiveCoordinator<W>, target: &RenderTarget, motion: Motion, frame: impl Fn(Duration) -> R, ) -> Result<WaitOutcome, LiveError>
Wait while showing frame(time_left) through live.
Motion::Animated draws the frame in a new region, updates it every
tick and removes it at the end. Motion::Static prints the first
frame once as an ordinary line, which a log keeps.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for CountdownWait<'a>
impl<'a> !Send for CountdownWait<'a>
impl<'a> !Sync for CountdownWait<'a>
impl<'a> !UnwindSafe for CountdownWait<'a>
impl<'a> Freeze for CountdownWait<'a>
impl<'a> Unpin for CountdownWait<'a>
impl<'a> UnsafeUnpin for CountdownWait<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more