pub struct Watcher { /* private fields */ }Expand description
A loop’s view of the stop signal.
Cheap to clone, and safe to hold across an await.
Implementations§
Source§impl Watcher
impl Watcher
Sourcepub fn is_stopping(&self) -> bool
pub fn is_stopping(&self) -> bool
Whether a stop has been requested.
The condition a while loop tests between units of work.
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Whether the loop should keep going.
The inverse of Watcher::is_stopping, spelled the way a loop
reads best: while watcher.is_running() { … }.
Sourcepub async fn wait(&self)
pub async fn wait(&self)
Wait until a stop is requested.
Returns immediately if one already has been. Use it with select!
to cut a long operation short.
Sourcepub async fn sleep(&self, duration: Duration) -> bool
pub async fn sleep(&self, duration: Duration) -> bool
Sleep, unless a stop is requested first.
Returns true if the sleep completed, false if it was cut short.
§Examples
use std::time::Duration;
use graceful_worker::Shutdown;
let shutdown = Shutdown::new();
let watcher = shutdown.watcher();
shutdown.stop();
// Already stopping, so the sleep is abandoned rather than served.
assert!(!watcher.sleep(Duration::from_secs(900)).await);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Watcher
impl RefUnwindSafe for Watcher
impl Send for Watcher
impl Sync for Watcher
impl Unpin for Watcher
impl UnsafeUnpin for Watcher
impl UnwindSafe for Watcher
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