Skip to content

Commit 4308464

Browse files
committed
feat: add new from_pid constructor
1 parent 1fedf03 commit 4308464

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

examples/kqueue-process.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ fn main() -> std::io::Result<()> {
2727
.expect("failed to spawn process");
2828

2929
// Wrap the process in an `Async` object that waits for it to exit.
30-
let process_handle = Filter::new(Exit::new(
31-
NonZeroI32::new(process.id().try_into().expect("invalid process pid"))
32-
.expect("non zero pid"),
33-
))?;
30+
let process_handle = unsafe {
31+
Filter::new(Exit::from_pid(
32+
NonZeroI32::new(process.id().try_into().expect("invalid process pid"))
33+
.expect("non zero pid"),
34+
))?
35+
};
3436

3537
// Wait for the process to exit.
3638
process_handle.ready().await?;

src/os/kqueue.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::io::{Error, Result};
1010
use std::num::NonZeroI32;
1111
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};
1212
use std::pin::Pin;
13+
use std::process::Child;
1314
use std::task::{Context, Poll};
1415

1516
/// A wrapper around a queueable object that waits until it is ready.
@@ -41,14 +42,13 @@ impl<T: Queueable> Filter<T> {
4142
///
4243
/// ```no_run
4344
/// use std::process::Command;
44-
/// use std::num::NonZeroI32;
4545
/// use async_io::os::kqueue::{Exit, Filter};
4646
///
4747
/// // Create a new process to wait for.
4848
/// let mut child = Command::new("sleep").arg("5").spawn().unwrap();
4949
///
5050
/// // Wrap the process in an `Async` object that waits for it to exit.
51-
/// let mut process = Filter::new(Exit::new(NonZeroI32::new(child.id().try_into().unwrap()).unwrap())).unwrap();
51+
/// let process = Filter::new(Exit::new(child)).unwrap();
5252
///
5353
/// // Wait for the process to exit.
5454
/// # async_io::block_on(async {
@@ -98,11 +98,10 @@ impl<T> Filter<T> {
9898
///
9999
/// ```
100100
/// use async_io::os::kqueue::{Exit, Filter};
101-
/// use std::num::NonZeroI32;
102101
///
103102
/// # futures_lite::future::block_on(async {
104103
/// let child = std::process::Command::new("sleep").arg("5").spawn().unwrap();
105-
/// let mut process = Filter::new(Exit::new(NonZeroI32::new(child.id().try_into().unwrap()).unwrap())).unwrap();
104+
/// let process = Filter::new(Exit::new(child)).unwrap();
106105
/// let inner = process.get_ref();
107106
/// # });
108107
/// ```
@@ -119,11 +118,10 @@ impl<T> Filter<T> {
119118
///
120119
/// ```
121120
/// use async_io::os::kqueue::{Exit, Filter};
122-
/// use std::num::NonZeroI32;
123121
///
124122
/// # futures_lite::future::block_on(async {
125123
/// let child = std::process::Command::new("sleep").arg("5").spawn().unwrap();
126-
/// let mut process = Filter::new(Exit::new(NonZeroI32::new(child.id().try_into().unwrap()).unwrap())).unwrap();
124+
/// let mut process = Filter::new(Exit::new(child)).unwrap();
127125
/// let inner = process.get_mut();
128126
/// # });
129127
/// ```
@@ -137,11 +135,10 @@ impl<T> Filter<T> {
137135
///
138136
/// ```
139137
/// use async_io::os::kqueue::{Exit, Filter};
140-
/// use std::num::NonZeroI32;
141138
///
142139
/// # futures_lite::future::block_on(async {
143140
/// let child = std::process::Command::new("sleep").arg("5").spawn().unwrap();
144-
/// let mut process = Filter::new(Exit::new(NonZeroI32::new(child.id().try_into().unwrap()).unwrap())).unwrap();
141+
/// let process = Filter::new(Exit::new(child)).unwrap();
145142
/// let inner = process.into_inner().unwrap();
146143
/// # });
147144
/// ```
@@ -157,13 +154,12 @@ impl<T> Filter<T> {
157154
/// # Examples
158155
///
159156
/// ```no_run
160-
/// use std::num::NonZeroI32;
161157
/// use std::process::Command;
162158
/// use async_io::os::kqueue::{Exit, Filter};
163159
///
164160
/// # futures_lite::future::block_on(async {
165161
/// let child = Command::new("sleep").arg("5").spawn()?;
166-
/// let process = Filter::new(Exit::new(NonZeroI32::new(child.id().try_into().unwrap()).unwrap())).unwrap();
162+
/// let process = Filter::new(Exit::new(child))?;
167163
///
168164
/// // Wait for the process to exit.
169165
/// process.ready().await?;
@@ -187,14 +183,13 @@ impl<T> Filter<T> {
187183
/// # Examples
188184
///
189185
/// ```no_run
190-
/// use std::num::NonZeroI32;
191186
/// use std::process::Command;
192187
/// use async_io::os::kqueue::{Exit, Filter};
193188
/// use futures_lite::future;
194189
///
195190
/// # futures_lite::future::block_on(async {
196191
/// let child = Command::new("sleep").arg("5").spawn()?;
197-
/// let process = Filter::new(Exit::new(NonZeroI32::new(child.id().try_into().unwrap()).unwrap())).unwrap();
192+
/// let process = Filter::new(Exit::new(child))?;
198193
///
199194
/// // Wait for the process to exit.
200195
/// future::poll_fn(|cx| process.poll_ready(cx)).await?;
@@ -239,7 +234,7 @@ impl QueueableSealed for Signal {
239234
}
240235
impl Queueable for Signal {}
241236

242-
/// Wait for a process to exit.
237+
/// Wait for a child process to exit.
243238
///
244239
/// When registered into [`Async`](crate::Async) via [`with_filter`](AsyncKqueueExt::with_filter),
245240
/// it will return a [`readable`](crate::Async::readable) event when the child process exits.
@@ -248,7 +243,19 @@ pub struct Exit(NonZeroI32);
248243

249244
impl Exit {
250245
/// Create a new `Exit` object.
251-
pub fn new(pid: NonZeroI32) -> Self {
246+
pub fn new(child: Child) -> Self {
247+
Self(
248+
NonZeroI32::new(child.id().try_into().expect("unable to parse pid"))
249+
.expect("cannot register pid with zero value"),
250+
)
251+
}
252+
253+
/// Create a new `Exit` object from a PID.
254+
///
255+
/// # Safety
256+
///
257+
/// The PID must be tied to an actual child process.
258+
pub unsafe fn from_pid(pid: NonZeroI32) -> Self {
252259
Self(pid)
253260
}
254261
}

0 commit comments

Comments
 (0)