pub struct Shutdown { /* private fields */ }
Implementations§
source§impl Shutdown
impl Shutdown
sourcepub fn branch(&self) -> Self
pub fn branch(&self) -> Self
Create a new branch (child) that can be signalled independent of the root (parent). When the root (or parent to be more precise) is signalled, the new branch (and any child branches) will also be signalled.
Examples
use shutdown::Shutdown;
let root = Shutdown::new().unwrap();
let branch = root.branch();
// Signal a specific branch
branch.signal();
sourcepub fn subscribe(&self) -> Self
pub fn subscribe(&self) -> Self
Create a new subscriber (sibling) that listens to an existing root (or previously created branch) for any shutdown signals.
Examples
use shutdown::Shutdown;
let root = Shutdown::new().unwrap();
let subscriber = root.subscribe();
sourcepub fn is_signalled(&self) -> bool
pub fn is_signalled(&self) -> bool
Returns true
if a shutdown signal has been received.
Examples
use shutdown::Shutdown;
let root = Shutdown::new().unwrap();
while !root.is_signalled() {
// Do stuff...
}
sourcepub fn signal(&self)
pub fn signal(&self)
Manually signal the root or a branch. This causes all connected subscribers and any child branches to be signalled as well.
Examples
use shutdown::Shutdown;
let root = Shutdown::new().unwrap();
let branch = root.branch();
// Trigger a signal from code
root.signal();
sourcepub async fn signalled(&self)
pub async fn signalled(&self)
Block until a shutdown signal is received. This can, for example, be used in a select to block to wait for a long running task while still being able to respond to a shutdown signal.
Examples
use shutdown::Shutdown;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let mut root = Shutdown::new().unwrap();
tokio::select! {
_ = root.signalled() => (),
_ = sleep(Duration::from_secs(300)) => (), // Long runnnig task
}
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Shutdown
impl Send for Shutdown
impl Sync for Shutdown
impl Unpin for Shutdown
impl UnwindSafe for Shutdown
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