Refactor unix-signal example to use signal-hook-async-std

signal-hook now has separate async crates. As of version 0.2.1 the
async-std version only depends on async-io and futures-lite for it's
async stuff, thereby making it "compatible" with smol. This may be a
better example since it's the more conical way to use signal-hook.
This commit is contained in:
Ben Aaron Goldberg 2021-02-07 01:51:41 -05:00
parent 76dac2a493
commit 77a0fe4aca
2 changed files with 6 additions and 9 deletions

View File

@ -42,6 +42,7 @@ native-tls = "0.2.7"
num_cpus = "1.13.0"
scraper = "0.12.0"
signal-hook = "0.3.2"
signal-hook-async-std = "0.2.1"
surf = { version = "2.1.0", default-features = false, features = ["h1-client"] }
tempfile = "3.2.0"
tide = "0.15.0"

View File

@ -8,19 +8,15 @@
#[cfg(unix)]
fn main() -> std::io::Result<()> {
use std::os::unix::{io::AsRawFd, net::UnixStream};
use smol::{prelude::*, Async};
use smol::prelude::*;
smol::block_on(async {
// Create a Unix stream that receives a byte on each signal occurrence.
let (a, mut b) = Async::<UnixStream>::pair()?;
// Async isn't IntoRawFd, but it is AsRawFd, so let's pass the raw fd directly.
signal_hook::low_level::pipe::register_raw(signal_hook::consts::SIGINT, a.as_raw_fd())?;
// Create a signal stream for SIGINT.
let mut signal = signal_hook_async_std::Signals::new(&[signal_hook::consts::SIGINT])?;
println!("Waiting for Ctrl-C...");
// Receive a byte that indicates the Ctrl-C signal occurred.
b.read_exact(&mut [0]).await?;
// Wait for the signal stream to return a signal.
signal.next().await;
println!("Done!");
Ok(())