untitled paste

unlisted ⁨1⁩ ⁨file⁩ 2019-12-01 16:24:59 UTC

demo.rs

Raw
use std::pin::Pin;
use std::task::{Context, Poll};
use std::future::Future;

use tokio::io::Registration;

pub struct PidFd {
    inner: mio_pidfd::PidFd,
    registration: Registration,
}

impl Unpin for PidFd {}

impl PidFd {
    pub fn new(pid: libc::pid_t) -> std::io::Result<PidFd> {
        let inner = mio_pidfd::PidFd::open(pid, 0)?;
        let registration = Registration::new(&inner)?;
        Ok(PidFd { inner, registration })
    }
}

impl Future for PidFd {
    type Output = ();

    fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<()> {
        match self.registration.poll_read_ready(context) {
            Poll::Ready(Ok(_)) => Poll::Ready(()),
            Poll::Ready(Err(_)) => panic!("error handling..."),
            Poll::Pending => Poll::Pending,
        }
    }
}

#[tokio::main]
async fn main() {
    let pid_fd = PidFd::new(154521).unwrap();
    pid_fd.await
}