Skip to content

Commit ce6aeb3

Browse files
kosayodaSteveLauC
andcommitted
Apply suggestions from code review
Co-authored-by: SteveLauC <[email protected]>
1 parent 4db179f commit ce6aeb3

File tree

2 files changed

+36
-88
lines changed

2 files changed

+36
-88
lines changed

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ mod poll_timeout;
190190
target_os = "haiku",
191191
target_os = "linux",
192192
target_os = "netbsd",
193-
target_os = "macos",
194-
target_os = "ios"
193+
apple_targets
195194
))]
196195
feature! {
197196
#![feature = "process"]

src/spawn.rs

Lines changed: 35 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ impl PosixSpawnAttr {
2828
pub fn init() -> Result<PosixSpawnAttr> {
2929
let mut attr = mem::MaybeUninit::uninit();
3030
let res = unsafe { libc::posix_spawnattr_init(attr.as_mut_ptr()) };
31-
32-
if res != 0 {
33-
return Err(Errno::from_raw(res));
34-
}
31+
Errno::result(res)?;
3532

3633
let attr = unsafe { attr.assume_init() };
3734
Ok(PosixSpawnAttr { attr })
@@ -49,18 +46,7 @@ impl PosixSpawnAttr {
4946
&mut self.attr as *mut libc::posix_spawnattr_t,
5047
)
5148
};
52-
if res != 0 {
53-
return Err(Errno::from_raw(res));
54-
}
55-
56-
let res = unsafe {
57-
libc::posix_spawnattr_init(
58-
&mut self.attr as *mut libc::posix_spawnattr_t,
59-
)
60-
};
61-
if res != 0 {
62-
return Err(Errno::from_raw(res));
63-
}
49+
Errno::result(res)?;
6450

6551
Ok(self)
6652
}
@@ -75,27 +61,23 @@ impl PosixSpawnAttr {
7561
flags.bits() as libc::c_short,
7662
)
7763
};
78-
if res != 0 {
79-
return Err(Errno::from_raw(res));
80-
}
64+
Errno::result(res)?;
8165

8266
Ok(())
8367
}
8468

8569
/// Get spawn flags. See
8670
/// [posix_spawnattr_getflags](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getflags.html).
8771
#[doc(alias("posix_spawnattr_getflags"))]
88-
pub fn flags(&mut self) -> Result<PosixSpawnFlags> {
72+
pub fn flags(&self) -> Result<PosixSpawnFlags> {
8973
let mut flags: libc::c_short = 0;
9074
let res = unsafe {
9175
libc::posix_spawnattr_getflags(
92-
&mut self.attr as *mut libc::posix_spawnattr_t,
76+
&self.attr as *const libc::posix_spawnattr_t,
9377
&mut flags,
9478
)
9579
};
96-
if res != 0 {
97-
return Err(Errno::from_raw(res));
98-
}
80+
Errno::result(res)?;
9981

10082
Ok(PosixSpawnFlags::from_bits_truncate(flags.into()))
10183
}
@@ -110,28 +92,22 @@ impl PosixSpawnAttr {
11092
pgroup.as_raw(),
11193
)
11294
};
113-
if res != 0 {
114-
return Err(Errno::from_raw(res));
115-
}
116-
117-
Ok(())
95+
Errno::result(res).map(drop)
11896
}
11997

12098
/// Get spawn pgroup. See
12199
/// [posix_spawnattr_getpgroup](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getpgroup.html).
122100
#[doc(alias("posix_spawnattr_getpgroup"))]
123-
pub fn pgroup(&mut self) -> Result<Pid> {
101+
pub fn pgroup(&self) -> Result<Pid> {
124102
let mut pid: libc::pid_t = 0;
125103

126104
let res = unsafe {
127105
libc::posix_spawnattr_getpgroup(
128-
&mut self.attr as *mut libc::posix_spawnattr_t,
106+
&self.attr as *const libc::posix_spawnattr_t,
129107
&mut pid,
130108
)
131109
};
132-
if res != 0 {
133-
return Err(Errno::from_raw(res));
134-
}
110+
Errno::result(res)?;
135111

136112
Ok(Pid::from_raw(pid))
137113
}
@@ -148,28 +124,24 @@ impl PosixSpawnAttr {
148124
sigdefault.as_ref(),
149125
)
150126
};
151-
if res != 0 {
152-
return Err(Errno::from_raw(res));
153-
}
127+
Errno::result(res)?;
154128

155129
Ok(())
156130
}
157131

158132
/// Get spawn sigdefault. See
159133
/// [posix_spawnattr_getsigdefault](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigdefault.html).
160134
#[doc(alias("posix_spawnattr_getsigdefault"))]
161-
pub fn sigdefault(&mut self) -> Result<SigSet> {
135+
pub fn sigdefault(&self) -> Result<SigSet> {
162136
let mut sigset = mem::MaybeUninit::uninit();
163137

164138
let res = unsafe {
165139
libc::posix_spawnattr_getsigdefault(
166-
&mut self.attr as *mut libc::posix_spawnattr_t,
140+
&self.attr as *const libc::posix_spawnattr_t,
167141
sigset.as_mut_ptr(),
168142
)
169143
};
170-
if res != 0 {
171-
return Err(Errno::from_raw(res));
172-
}
144+
Errno::result(res)?;
173145

174146
let sigdefault =
175147
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
@@ -186,28 +158,24 @@ impl PosixSpawnAttr {
186158
sigdefault.as_ref(),
187159
)
188160
};
189-
if res != 0 {
190-
return Err(Errno::from_raw(res));
191-
}
161+
Errno::result(res)?;
192162

193163
Ok(())
194164
}
195165

196166
/// Get spawn sigmask. See
197167
/// [posix_spawnattr_getsigmask](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigmask.html).
198168
#[doc(alias("posix_spawnattr_getsigmask"))]
199-
pub fn sigmask(&mut self) -> Result<SigSet> {
169+
pub fn sigmask(&self) -> Result<SigSet> {
200170
let mut sigset = mem::MaybeUninit::uninit();
201171

202172
let res = unsafe {
203173
libc::posix_spawnattr_getsigmask(
204-
&mut self.attr as *mut libc::posix_spawnattr_t,
174+
&self.attr as *const libc::posix_spawnattr_t,
205175
sigset.as_mut_ptr(),
206176
)
207177
};
208-
if res != 0 {
209-
return Err(Errno::from_raw(res));
210-
}
178+
Errno::result(res)?;
211179

212180
let sigdefault =
213181
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
@@ -268,16 +236,13 @@ impl PosixSpawnFileActions {
268236
let res = unsafe {
269237
libc::posix_spawn_file_actions_init(actions.as_mut_ptr())
270238
};
239+
Errno::result(res)?;
271240

272-
if res == 0 {
273-
Ok(unsafe {
274-
PosixSpawnFileActions {
275-
fa: actions.assume_init(),
276-
}
277-
})
278-
} else {
279-
Err(Errno::from_raw(res))
280-
}
241+
Ok(unsafe {
242+
PosixSpawnFileActions {
243+
fa: actions.assume_init(),
244+
}
245+
})
281246
}
282247

283248
/// Reinitialize the spawn file actions object.
@@ -292,18 +257,14 @@ impl PosixSpawnFileActions {
292257
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
293258
)
294259
};
295-
if res != 0 {
296-
return Err(Errno::from_raw(res));
297-
}
260+
Errno::result(res)?;
298261

299262
let res = unsafe {
300263
libc::posix_spawn_file_actions_init(
301264
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
302265
)
303266
};
304-
if res != 0 {
305-
return Err(Errno::from_raw(res));
306-
}
267+
Errno::result(res)?;
307268

308269
Ok(self)
309270
}
@@ -323,9 +284,7 @@ impl PosixSpawnFileActions {
323284
newfd.as_fd().as_raw_fd(),
324285
)
325286
};
326-
if res != 0 {
327-
return Err(Errno::from_raw(res));
328-
}
287+
Errno::result(res)?;
329288

330289
Ok(())
331290
}
@@ -351,9 +310,7 @@ impl PosixSpawnFileActions {
351310
mode.bits(),
352311
)
353312
})?;
354-
if res != 0 {
355-
return Err(Errno::from_raw(res));
356-
}
313+
Errno::result(res)?;
357314

358315
Ok(())
359316
}
@@ -369,9 +326,7 @@ impl PosixSpawnFileActions {
369326
fd.as_fd().as_raw_fd(),
370327
)
371328
};
372-
if res != 0 {
373-
return Err(Errno::from_raw(res));
374-
}
329+
Errno::result(res)?;
375330

376331
Ok(())
377332
}
@@ -392,9 +347,9 @@ impl Drop for PosixSpawnFileActions {
392347
unsafe fn to_exec_array<S: AsRef<CStr>>(args: &[S]) -> Vec<*mut libc::c_char> {
393348
let mut v: Vec<*mut libc::c_char> = args
394349
.iter()
395-
.map(|s| s.as_ref().as_ptr() as *mut libc::c_char)
350+
.map(|s| s.as_ref().as_ptr().cast_mut())
396351
.collect();
397-
v.push(std::ptr::null::<libc::c_char>() as *mut libc::c_char);
352+
v.push(std::ptr::null_mut());
398353
v
399354
}
400355

@@ -422,12 +377,9 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
422377
env_p.as_ptr(),
423378
)
424379
};
380+
Errno::result(res)?;
425381

426-
if res == 0 {
427-
Ok(Pid::from_raw(pid))
428-
} else {
429-
Err(Errno::from_raw(res))
430-
}
382+
Ok(Pid::from_raw(pid))
431383
}
432384

433385
/// Create a new child process from the specified process image. See
@@ -454,10 +406,7 @@ pub fn posix_spawnp<SA: AsRef<CStr>, SE: AsRef<CStr>>(
454406
env_p.as_ptr(),
455407
)
456408
};
409+
Errno::result(res)?;
457410

458-
if res == 0 {
459-
Ok(Pid::from_raw(pid))
460-
} else {
461-
Err(Errno::from_raw(res))
462-
}
411+
Ok(Pid::from_raw(pid))
463412
}

0 commit comments

Comments
 (0)