Skip to content

Commit ffedd49

Browse files
author
Cameron Nemo
committed
Use Errno::result helper
1 parent 18cd846 commit ffedd49

File tree

1 file changed

+26
-68
lines changed

1 file changed

+26
-68
lines changed

src/spawn.rs

+26-68
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,18 +47,14 @@ 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

5652
let res = unsafe {
5753
libc::posix_spawnattr_init(
5854
&mut self.attr as *mut libc::posix_spawnattr_t,
5955
)
6056
};
61-
if res != 0 {
62-
return Err(Errno::from_raw(res));
63-
}
57+
Errno::result(res)?;
6458

6559
Ok(self)
6660
}
@@ -75,9 +69,7 @@ impl PosixSpawnAttr {
7569
flags.bits() as libc::c_short,
7670
)
7771
};
78-
if res != 0 {
79-
return Err(Errno::from_raw(res));
80-
}
72+
Errno::result(res)?;
8173

8274
Ok(())
8375
}
@@ -93,9 +85,7 @@ impl PosixSpawnAttr {
9385
&mut flags,
9486
)
9587
};
96-
if res != 0 {
97-
return Err(Errno::from_raw(res));
98-
}
88+
Errno::result(res)?;
9989

10090
Ok(PosixSpawnFlags::from_bits_truncate(flags.into()))
10191
}
@@ -110,9 +100,7 @@ impl PosixSpawnAttr {
110100
pgroup.as_raw(),
111101
)
112102
};
113-
if res != 0 {
114-
return Err(Errno::from_raw(res));
115-
}
103+
Errno::result(res)?;
116104

117105
Ok(())
118106
}
@@ -129,9 +117,7 @@ impl PosixSpawnAttr {
129117
&mut pid,
130118
)
131119
};
132-
if res != 0 {
133-
return Err(Errno::from_raw(res));
134-
}
120+
Errno::result(res)?;
135121

136122
Ok(Pid::from_raw(pid))
137123
}
@@ -148,9 +134,7 @@ impl PosixSpawnAttr {
148134
sigdefault.as_ref(),
149135
)
150136
};
151-
if res != 0 {
152-
return Err(Errno::from_raw(res));
153-
}
137+
Errno::result(res)?;
154138

155139
Ok(())
156140
}
@@ -167,9 +151,7 @@ impl PosixSpawnAttr {
167151
sigset.as_mut_ptr(),
168152
)
169153
};
170-
if res != 0 {
171-
return Err(Errno::from_raw(res));
172-
}
154+
Errno::result(res)?;
173155

174156
let sigdefault =
175157
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
@@ -186,9 +168,7 @@ impl PosixSpawnAttr {
186168
sigdefault.as_ref(),
187169
)
188170
};
189-
if res != 0 {
190-
return Err(Errno::from_raw(res));
191-
}
171+
Errno::result(res)?;
192172

193173
Ok(())
194174
}
@@ -205,9 +185,7 @@ impl PosixSpawnAttr {
205185
sigset.as_mut_ptr(),
206186
)
207187
};
208-
if res != 0 {
209-
return Err(Errno::from_raw(res));
210-
}
188+
Errno::result(res)?;
211189

212190
let sigdefault =
213191
unsafe { SigSet::from_sigset_t_unchecked(sigset.assume_init()) };
@@ -268,16 +246,12 @@ impl PosixSpawnFileActions {
268246
let res = unsafe {
269247
libc::posix_spawn_file_actions_init(actions.as_mut_ptr())
270248
};
271-
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-
}
249+
Errno::result(res)?;
250+
Ok(unsafe {
251+
PosixSpawnFileActions {
252+
fa: actions.assume_init(),
253+
}
254+
})
281255
}
282256

283257
/// Reinitialize the spawn file actions object.
@@ -292,18 +266,14 @@ impl PosixSpawnFileActions {
292266
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
293267
)
294268
};
295-
if res != 0 {
296-
return Err(Errno::from_raw(res));
297-
}
269+
Errno::result(res)?;
298270

299271
let res = unsafe {
300272
libc::posix_spawn_file_actions_init(
301273
&mut self.fa as *mut libc::posix_spawn_file_actions_t,
302274
)
303275
};
304-
if res != 0 {
305-
return Err(Errno::from_raw(res));
306-
}
276+
Errno::result(res)?;
307277

308278
Ok(self)
309279
}
@@ -323,9 +293,7 @@ impl PosixSpawnFileActions {
323293
newfd.as_fd().as_raw_fd(),
324294
)
325295
};
326-
if res != 0 {
327-
return Err(Errno::from_raw(res));
328-
}
296+
Errno::result(res)?;
329297

330298
Ok(())
331299
}
@@ -351,9 +319,7 @@ impl PosixSpawnFileActions {
351319
mode.bits(),
352320
)
353321
})?;
354-
if res != 0 {
355-
return Err(Errno::from_raw(res));
356-
}
322+
Errno::result(res)?;
357323

358324
Ok(())
359325
}
@@ -369,9 +335,7 @@ impl PosixSpawnFileActions {
369335
fd.as_fd().as_raw_fd(),
370336
)
371337
};
372-
if res != 0 {
373-
return Err(Errno::from_raw(res));
374-
}
338+
Errno::result(res)?;
375339

376340
Ok(())
377341
}
@@ -428,11 +392,8 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
428392
)
429393
};
430394

431-
if res == 0 {
432-
Ok(Pid::from_raw(pid))
433-
} else {
434-
Err(Errno::from_raw(res))
435-
}
395+
Errno::result(res)?;
396+
Ok(Pid::from_raw(pid))
436397
}
437398

438399
/// Create a new child process from the specified process image. See
@@ -460,9 +421,6 @@ pub fn posix_spawnp<SA: AsRef<CStr>, SE: AsRef<CStr>>(
460421
)
461422
};
462423

463-
if res == 0 {
464-
Ok(Pid::from_raw(pid))
465-
} else {
466-
Err(Errno::from_raw(res))
467-
}
424+
Errno::result(res)?;
425+
Ok(Pid::from_raw(pid))
468426
}

0 commit comments

Comments
 (0)