Skip to content

Commit b540af8

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

File tree

2 files changed

+32
-102
lines changed

2 files changed

+32
-102
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: 31 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ impl PosixSpawnAttr {
2929
let mut attr = mem::MaybeUninit::uninit();
3030
let res = unsafe { libc::posix_spawnattr_init(attr.as_mut_ptr()) };
3131

32-
if res != 0 {
33-
return Err(Errno::from_raw(res));
34-
}
32+
Errno::result(res)?;
3533

3634
let attr = unsafe { attr.assume_init() };
3735
Ok(PosixSpawnAttr { attr })
@@ -49,20 +47,9 @@ impl PosixSpawnAttr {
4947
&mut self.attr as *mut libc::posix_spawnattr_t,
5048
)
5149
};
52-
if res != 0 {
53-
return Err(Errno::from_raw(res));
54-
}
50+
Errno::result(res)?;
5551

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-
}
64-
65-
Ok(self)
52+
Errno::result(res).map(self)
6653
}
6754

6855
/// Set spawn flags. See
@@ -75,27 +62,23 @@ impl PosixSpawnAttr {
7562
flags.bits() as libc::c_short,
7663
)
7764
};
78-
if res != 0 {
79-
return Err(Errno::from_raw(res));
80-
}
65+
Errno::result(res)?;
8166

8267
Ok(())
8368
}
8469

8570
/// Get spawn flags. See
8671
/// [posix_spawnattr_getflags](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getflags.html).
8772
#[doc(alias("posix_spawnattr_getflags"))]
88-
pub fn flags(&mut self) -> Result<PosixSpawnFlags> {
73+
pub fn flags(&self) -> Result<PosixSpawnFlags> {
8974
let mut flags: libc::c_short = 0;
9075
let res = unsafe {
9176
libc::posix_spawnattr_getflags(
92-
&mut self.attr as *mut libc::posix_spawnattr_t,
77+
&self.attr as *const libc::posix_spawnattr_t,
9378
&mut flags,
9479
)
9580
};
96-
if res != 0 {
97-
return Err(Errno::from_raw(res));
98-
}
81+
Errno::result(res)?;
9982

10083
Ok(PosixSpawnFlags::from_bits_truncate(flags.into()))
10184
}
@@ -110,30 +93,22 @@ impl PosixSpawnAttr {
11093
pgroup.as_raw(),
11194
)
11295
};
113-
if res != 0 {
114-
return Err(Errno::from_raw(res));
115-
}
116-
117-
Ok(())
96+
Errno::result(res).map(drop)
11897
}
11998

12099
/// Get spawn pgroup. See
121100
/// [posix_spawnattr_getpgroup](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getpgroup.html).
122101
#[doc(alias("posix_spawnattr_getpgroup"))]
123-
pub fn pgroup(&mut self) -> Result<Pid> {
102+
pub fn pgroup(&self) -> Result<Pid> {
124103
let mut pid: libc::pid_t = 0;
125104

126105
let res = unsafe {
127106
libc::posix_spawnattr_getpgroup(
128-
&mut self.attr as *mut libc::posix_spawnattr_t,
107+
&self.attr as *const libc::posix_spawnattr_t,
129108
&mut pid,
130109
)
131110
};
132-
if res != 0 {
133-
return Err(Errno::from_raw(res));
134-
}
135-
136-
Ok(Pid::from_raw(pid))
111+
Errno::result(res).map(Pid::from_raw))
137112
}
138113

139114
feature! {
@@ -148,28 +123,22 @@ impl PosixSpawnAttr {
148123
sigdefault.as_ref(),
149124
)
150125
};
151-
if res != 0 {
152-
return Err(Errno::from_raw(res));
153-
}
154-
155-
Ok(())
126+
Errno::result(res).map(drop)
156127
}
157128

158129
/// Get spawn sigdefault. See
159130
/// [posix_spawnattr_getsigdefault](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigdefault.html).
160131
#[doc(alias("posix_spawnattr_getsigdefault"))]
161-
pub fn sigdefault(&mut self) -> Result<SigSet> {
132+
pub fn sigdefault(&self) -> Result<SigSet> {
162133
let mut sigset = mem::MaybeUninit::uninit();
163134

164135
let res = unsafe {
165136
libc::posix_spawnattr_getsigdefault(
166-
&mut self.attr as *mut libc::posix_spawnattr_t,
137+
&self.attr as *const libc::posix_spawnattr_t,
167138
sigset.as_mut_ptr(),
168139
)
169140
};
170-
if res != 0 {
171-
return Err(Errno::from_raw(res));
172-
}
141+
Errno::result(res)?;
173142

174143
let sigdefault =
175144
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
@@ -186,28 +155,22 @@ impl PosixSpawnAttr {
186155
sigdefault.as_ref(),
187156
)
188157
};
189-
if res != 0 {
190-
return Err(Errno::from_raw(res));
191-
}
192-
193-
Ok(())
158+
Errno::result(res).map(drop)
194159
}
195160

196161
/// Get spawn sigmask. See
197162
/// [posix_spawnattr_getsigmask](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigmask.html).
198163
#[doc(alias("posix_spawnattr_getsigmask"))]
199-
pub fn sigmask(&mut self) -> Result<SigSet> {
164+
pub fn sigmask(&self) -> Result<SigSet> {
200165
let mut sigset = mem::MaybeUninit::uninit();
201166

202167
let res = unsafe {
203168
libc::posix_spawnattr_getsigmask(
204-
&mut self.attr as *mut libc::posix_spawnattr_t,
169+
&self.attr as *const libc::posix_spawnattr_t,
205170
sigset.as_mut_ptr(),
206171
)
207172
};
208-
if res != 0 {
209-
return Err(Errno::from_raw(res));
210-
}
173+
Errno::result(res)?;
211174

212175
let sigdefault =
213176
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
@@ -269,15 +232,9 @@ impl PosixSpawnFileActions {
269232
libc::posix_spawn_file_actions_init(actions.as_mut_ptr())
270233
};
271234

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-
}
235+
Errno::result(res)?;
236+
237+
Ok( PosixSpawnFileActions { fa: actions.assume_init() } )
281238
}
282239

283240
/// Reinitialize the spawn file actions object.
@@ -292,20 +249,14 @@ impl PosixSpawnFileActions {
292249
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
293250
)
294251
};
295-
if res != 0 {
296-
return Err(Errno::from_raw(res));
297-
}
252+
Errno::result(res)?;
298253

299254
let res = unsafe {
300255
libc::posix_spawn_file_actions_init(
301256
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
302257
)
303258
};
304-
if res != 0 {
305-
return Err(Errno::from_raw(res));
306-
}
307-
308-
Ok(self)
259+
Errno::result(res).map(self)
309260
}
310261

311262
/// Add a [dup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html) action. See
@@ -323,11 +274,7 @@ impl PosixSpawnFileActions {
323274
newfd.as_fd().as_raw_fd(),
324275
)
325276
};
326-
if res != 0 {
327-
return Err(Errno::from_raw(res));
328-
}
329-
330-
Ok(())
277+
Errno::result(res).map(drop)
331278
}
332279

333280
feature! {
@@ -351,11 +298,7 @@ impl PosixSpawnFileActions {
351298
mode.bits(),
352299
)
353300
})?;
354-
if res != 0 {
355-
return Err(Errno::from_raw(res));
356-
}
357-
358-
Ok(())
301+
Errno::result(res).map(drop)
359302
}
360303
}
361304

@@ -369,11 +312,7 @@ impl PosixSpawnFileActions {
369312
fd.as_fd().as_raw_fd(),
370313
)
371314
};
372-
if res != 0 {
373-
return Err(Errno::from_raw(res));
374-
}
375-
376-
Ok(())
315+
Errno::result(res).map(drop)
377316
}
378317
}
379318

@@ -392,9 +331,9 @@ impl Drop for PosixSpawnFileActions {
392331
unsafe fn to_exec_array<S: AsRef<CStr>>(args: &[S]) -> Vec<*mut libc::c_char> {
393332
let mut v: Vec<*mut libc::c_char> = args
394333
.iter()
395-
.map(|s| s.as_ref().as_ptr() as *mut libc::c_char)
334+
.map(|s| s.as_ref().as_ptr().cast_mut())
396335
.collect();
397-
v.push(std::ptr::null::<libc::c_char>() as *mut libc::c_char);
336+
v.push(std::ptr::null_mut());
398337
v
399338
}
400339

@@ -423,11 +362,7 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
423362
)
424363
};
425364

426-
if res == 0 {
427-
Ok(Pid::from_raw(pid))
428-
} else {
429-
Err(Errno::from_raw(res))
430-
}
365+
Errno::result(res).map(Pid::from_raw)
431366
}
432367

433368
/// Create a new child process from the specified process image. See
@@ -455,9 +390,5 @@ pub fn posix_spawnp<SA: AsRef<CStr>, SE: AsRef<CStr>>(
455390
)
456391
};
457392

458-
if res == 0 {
459-
Ok(Pid::from_raw(pid))
460-
} else {
461-
Err(Errno::from_raw(res))
462-
}
393+
Errno::result(res).map(Pid::from_raw)
463394
}

0 commit comments

Comments
 (0)