async_integration/async_integration.rs
1//! Demonstrates async integration via `as_raw_fd()` with `std::os::unix::io::OwnedFd`.
2//!
3//! This example shows how to use `ProcConnector` with a simple event loop
4//! (or with `tokio::AsyncFd`, `mio`, etc.) by polling the raw file descriptor.
5//!
6//! Usage:
7//! ```sh
8//! cargo run --example async_integration
9//! ```
10
11use proc_connector::ProcConnector;
12use std::time::Duration;
13
14fn main() {
15 let conn = ProcConnector::new().expect("failed to create proc connector (try as root)");
16 let mut buf = vec![0u8; 4096];
17
18 println!("polling for events with 2s timeout... (Ctrl+C to stop)");
19
20 // Example: poll-based timeout (no external dependencies needed)
21 loop {
22 match conn.recv_timeout(&mut buf, Duration::from_secs(2)) {
23 Ok(Some(event)) => println!("{event}"),
24 Ok(None) => {
25 // Timeout expired, do other work
26 print!(".");
27 std::io::Write::flush(&mut std::io::stdout()).ok();
28 }
29 Err(e) => {
30 eprintln!("error: {e}");
31 break;
32 }
33 }
34 }
35
36 // The raw fd can be used with tokio::AsyncFd, mio, etc.:
37 let _raw_fd = conn.as_raw_fd();
38 // With tokio:
39 // let async_fd = tokio::io::unix::AsyncFd::new(conn)?;
40 // loop {
41 // let mut guard = async_fd.readable().await?;
42 // match conn.recv(&mut buf) { ... }
43 // guard.clear_ready();
44 // }
45}