Skip to main content

arti/
exit.rs

1//! Detect a "ctrl-c" notification or other reason to exit.
2
3use crate::Result;
4
5/// Wait until a control-c notification is received, using an appropriate
6/// runtime mechanism.
7///
8/// This function can have pretty kludgy side-effects: see
9/// documentation for `tokio::signal::ctrl_c` and `async_ctrlc` for
10/// caveats.  Notably, you can only call this once with async_std.
11#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
12pub(crate) async fn wait_for_ctrl_c() -> Result<()> {
13    #[cfg(feature = "tokio")]
14    {
15        tokio_crate::signal::ctrl_c().await?;
16
17        // Avoid an unused crate warning when building with --all-features.
18        // (The dependency is already conditional on async-std, but Cargo.toml
19        // can't express the not(tokio).)
20        #[cfg(feature = "async-std")]
21        use async_ctrlc as _;
22    }
23    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
24    {
25        async_ctrlc::CtrlC::new().unwrap().await;
26    }
27    Ok(())
28}