@@ -10,6 +10,7 @@ use std::io::{Error, Result};
10
10
use std:: num:: NonZeroI32 ;
11
11
use std:: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd , OwnedFd , RawFd } ;
12
12
use std:: pin:: Pin ;
13
+ use std:: process:: Child ;
13
14
use std:: task:: { Context , Poll } ;
14
15
15
16
/// A wrapper around a queueable object that waits until it is ready.
@@ -41,14 +42,13 @@ impl<T: Queueable> Filter<T> {
41
42
///
42
43
/// ```no_run
43
44
/// use std::process::Command;
44
- /// use std::num::NonZeroI32;
45
45
/// use async_io::os::kqueue::{Exit, Filter};
46
46
///
47
47
/// // Create a new process to wait for.
48
48
/// let mut child = Command::new("sleep").arg("5").spawn().unwrap();
49
49
///
50
50
/// // 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();
52
52
///
53
53
/// // Wait for the process to exit.
54
54
/// # async_io::block_on(async {
@@ -98,11 +98,10 @@ impl<T> Filter<T> {
98
98
///
99
99
/// ```
100
100
/// use async_io::os::kqueue::{Exit, Filter};
101
- /// use std::num::NonZeroI32;
102
101
///
103
102
/// # futures_lite::future::block_on(async {
104
103
/// 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();
106
105
/// let inner = process.get_ref();
107
106
/// # });
108
107
/// ```
@@ -119,11 +118,10 @@ impl<T> Filter<T> {
119
118
///
120
119
/// ```
121
120
/// use async_io::os::kqueue::{Exit, Filter};
122
- /// use std::num::NonZeroI32;
123
121
///
124
122
/// # futures_lite::future::block_on(async {
125
123
/// 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();
127
125
/// let inner = process.get_mut();
128
126
/// # });
129
127
/// ```
@@ -137,11 +135,10 @@ impl<T> Filter<T> {
137
135
///
138
136
/// ```
139
137
/// use async_io::os::kqueue::{Exit, Filter};
140
- /// use std::num::NonZeroI32;
141
138
///
142
139
/// # futures_lite::future::block_on(async {
143
140
/// 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();
145
142
/// let inner = process.into_inner().unwrap();
146
143
/// # });
147
144
/// ```
@@ -157,13 +154,12 @@ impl<T> Filter<T> {
157
154
/// # Examples
158
155
///
159
156
/// ```no_run
160
- /// use std::num::NonZeroI32;
161
157
/// use std::process::Command;
162
158
/// use async_io::os::kqueue::{Exit, Filter};
163
159
///
164
160
/// # futures_lite::future::block_on(async {
165
161
/// 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))? ;
167
163
///
168
164
/// // Wait for the process to exit.
169
165
/// process.ready().await?;
@@ -187,14 +183,13 @@ impl<T> Filter<T> {
187
183
/// # Examples
188
184
///
189
185
/// ```no_run
190
- /// use std::num::NonZeroI32;
191
186
/// use std::process::Command;
192
187
/// use async_io::os::kqueue::{Exit, Filter};
193
188
/// use futures_lite::future;
194
189
///
195
190
/// # futures_lite::future::block_on(async {
196
191
/// 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))? ;
198
193
///
199
194
/// // Wait for the process to exit.
200
195
/// future::poll_fn(|cx| process.poll_ready(cx)).await?;
@@ -239,7 +234,7 @@ impl QueueableSealed for Signal {
239
234
}
240
235
impl Queueable for Signal { }
241
236
242
- /// Wait for a process to exit.
237
+ /// Wait for a child process to exit.
243
238
///
244
239
/// When registered into [`Async`](crate::Async) via [`with_filter`](AsyncKqueueExt::with_filter),
245
240
/// it will return a [`readable`](crate::Async::readable) event when the child process exits.
@@ -248,7 +243,19 @@ pub struct Exit(NonZeroI32);
248
243
249
244
impl Exit {
250
245
/// 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 {
252
259
Self ( pid)
253
260
}
254
261
}
0 commit comments