@@ -28,10 +28,7 @@ impl PosixSpawnAttr {
28
28
pub fn init ( ) -> Result < PosixSpawnAttr > {
29
29
let mut attr = mem:: MaybeUninit :: uninit ( ) ;
30
30
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) ?;
35
32
36
33
let attr = unsafe { attr. assume_init ( ) } ;
37
34
Ok ( PosixSpawnAttr { attr } )
@@ -49,18 +46,7 @@ impl PosixSpawnAttr {
49
46
& mut self . attr as * mut libc:: posix_spawnattr_t ,
50
47
)
51
48
} ;
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) ?;
64
50
65
51
Ok ( self )
66
52
}
@@ -75,27 +61,23 @@ impl PosixSpawnAttr {
75
61
flags. bits ( ) as libc:: c_short ,
76
62
)
77
63
} ;
78
- if res != 0 {
79
- return Err ( Errno :: from_raw ( res) ) ;
80
- }
64
+ Errno :: result ( res) ?;
81
65
82
66
Ok ( ( ) )
83
67
}
84
68
85
69
/// Get spawn flags. See
86
70
/// [posix_spawnattr_getflags](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getflags.html).
87
71
#[ doc( alias( "posix_spawnattr_getflags" ) ) ]
88
- pub fn flags ( & mut self ) -> Result < PosixSpawnFlags > {
72
+ pub fn flags ( & self ) -> Result < PosixSpawnFlags > {
89
73
let mut flags: libc:: c_short = 0 ;
90
74
let res = unsafe {
91
75
libc:: posix_spawnattr_getflags (
92
- & mut self . attr as * mut libc:: posix_spawnattr_t ,
76
+ & self . attr as * const libc:: posix_spawnattr_t ,
93
77
& mut flags,
94
78
)
95
79
} ;
96
- if res != 0 {
97
- return Err ( Errno :: from_raw ( res) ) ;
98
- }
80
+ Errno :: result ( res) ?;
99
81
100
82
Ok ( PosixSpawnFlags :: from_bits_truncate ( flags. into ( ) ) )
101
83
}
@@ -110,28 +92,22 @@ impl PosixSpawnAttr {
110
92
pgroup. as_raw ( ) ,
111
93
)
112
94
} ;
113
- if res != 0 {
114
- return Err ( Errno :: from_raw ( res) ) ;
115
- }
116
-
117
- Ok ( ( ) )
95
+ Errno :: result ( res) . map ( drop)
118
96
}
119
97
120
98
/// Get spawn pgroup. See
121
99
/// [posix_spawnattr_getpgroup](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getpgroup.html).
122
100
#[ doc( alias( "posix_spawnattr_getpgroup" ) ) ]
123
- pub fn pgroup ( & mut self ) -> Result < Pid > {
101
+ pub fn pgroup ( & self ) -> Result < Pid > {
124
102
let mut pid: libc:: pid_t = 0 ;
125
103
126
104
let res = unsafe {
127
105
libc:: posix_spawnattr_getpgroup (
128
- & mut self . attr as * mut libc:: posix_spawnattr_t ,
106
+ & self . attr as * const libc:: posix_spawnattr_t ,
129
107
& mut pid,
130
108
)
131
109
} ;
132
- if res != 0 {
133
- return Err ( Errno :: from_raw ( res) ) ;
134
- }
110
+ Errno :: result ( res) ?;
135
111
136
112
Ok ( Pid :: from_raw ( pid) )
137
113
}
@@ -148,28 +124,24 @@ impl PosixSpawnAttr {
148
124
sigdefault. as_ref( ) ,
149
125
)
150
126
} ;
151
- if res != 0 {
152
- return Err ( Errno :: from_raw( res) ) ;
153
- }
127
+ Errno :: result( res) ?;
154
128
155
129
Ok ( ( ) )
156
130
}
157
131
158
132
/// Get spawn sigdefault. See
159
133
/// [posix_spawnattr_getsigdefault](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigdefault.html).
160
134
#[ doc( alias( "posix_spawnattr_getsigdefault" ) ) ]
161
- pub fn sigdefault( & mut self ) -> Result <SigSet > {
135
+ pub fn sigdefault( & self ) -> Result <SigSet > {
162
136
let mut sigset = mem:: MaybeUninit :: uninit( ) ;
163
137
164
138
let res = unsafe {
165
139
libc:: posix_spawnattr_getsigdefault(
166
- & mut self . attr as * mut libc:: posix_spawnattr_t,
140
+ & self . attr as * const libc:: posix_spawnattr_t,
167
141
sigset. as_mut_ptr( ) ,
168
142
)
169
143
} ;
170
- if res != 0 {
171
- return Err ( Errno :: from_raw( res) ) ;
172
- }
144
+ Errno :: result( res) ?;
173
145
174
146
let sigdefault =
175
147
unsafe { SigSet :: from_sigset_t_unchecked( sigset. assume_init( ) ) } ;
@@ -186,28 +158,24 @@ impl PosixSpawnAttr {
186
158
sigdefault. as_ref( ) ,
187
159
)
188
160
} ;
189
- if res != 0 {
190
- return Err ( Errno :: from_raw( res) ) ;
191
- }
161
+ Errno :: result( res) ?;
192
162
193
163
Ok ( ( ) )
194
164
}
195
165
196
166
/// Get spawn sigmask. See
197
167
/// [posix_spawnattr_getsigmask](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigmask.html).
198
168
#[ doc( alias( "posix_spawnattr_getsigmask" ) ) ]
199
- pub fn sigmask( & mut self ) -> Result <SigSet > {
169
+ pub fn sigmask( & self ) -> Result <SigSet > {
200
170
let mut sigset = mem:: MaybeUninit :: uninit( ) ;
201
171
202
172
let res = unsafe {
203
173
libc:: posix_spawnattr_getsigmask(
204
- & mut self . attr as * mut libc:: posix_spawnattr_t,
174
+ & self . attr as * const libc:: posix_spawnattr_t,
205
175
sigset. as_mut_ptr( ) ,
206
176
)
207
177
} ;
208
- if res != 0 {
209
- return Err ( Errno :: from_raw( res) ) ;
210
- }
178
+ Errno :: result( res) ?;
211
179
212
180
let sigdefault =
213
181
unsafe { SigSet :: from_sigset_t_unchecked( sigset. assume_init( ) ) } ;
@@ -268,16 +236,13 @@ impl PosixSpawnFileActions {
268
236
let res = unsafe {
269
237
libc:: posix_spawn_file_actions_init ( actions. as_mut_ptr ( ) )
270
238
} ;
239
+ Errno :: result ( res) ?;
271
240
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
+ } )
281
246
}
282
247
283
248
/// Reinitialize the spawn file actions object.
@@ -292,18 +257,14 @@ impl PosixSpawnFileActions {
292
257
& mut self . fa as * mut libc:: posix_spawn_file_actions_t ,
293
258
)
294
259
} ;
295
- if res != 0 {
296
- return Err ( Errno :: from_raw ( res) ) ;
297
- }
260
+ Errno :: result ( res) ?;
298
261
299
262
let res = unsafe {
300
263
libc:: posix_spawn_file_actions_init (
301
264
& mut self . fa as * mut libc:: posix_spawn_file_actions_t ,
302
265
)
303
266
} ;
304
- if res != 0 {
305
- return Err ( Errno :: from_raw ( res) ) ;
306
- }
267
+ Errno :: result ( res) ?;
307
268
308
269
Ok ( self )
309
270
}
@@ -323,9 +284,7 @@ impl PosixSpawnFileActions {
323
284
newfd. as_fd ( ) . as_raw_fd ( ) ,
324
285
)
325
286
} ;
326
- if res != 0 {
327
- return Err ( Errno :: from_raw ( res) ) ;
328
- }
287
+ Errno :: result ( res) ?;
329
288
330
289
Ok ( ( ) )
331
290
}
@@ -351,9 +310,7 @@ impl PosixSpawnFileActions {
351
310
mode. bits( ) ,
352
311
)
353
312
} ) ?;
354
- if res != 0 {
355
- return Err ( Errno :: from_raw( res) ) ;
356
- }
313
+ Errno :: result( res) ?;
357
314
358
315
Ok ( ( ) )
359
316
}
@@ -369,9 +326,7 @@ impl PosixSpawnFileActions {
369
326
fd. as_fd ( ) . as_raw_fd ( ) ,
370
327
)
371
328
} ;
372
- if res != 0 {
373
- return Err ( Errno :: from_raw ( res) ) ;
374
- }
329
+ Errno :: result ( res) ?;
375
330
376
331
Ok ( ( ) )
377
332
}
@@ -392,9 +347,9 @@ impl Drop for PosixSpawnFileActions {
392
347
unsafe fn to_exec_array < S : AsRef < CStr > > ( args : & [ S ] ) -> Vec < * mut libc:: c_char > {
393
348
let mut v: Vec < * mut libc:: c_char > = args
394
349
. iter ( )
395
- . map ( |s| s. as_ref ( ) . as_ptr ( ) as * mut libc :: c_char )
350
+ . map ( |s| s. as_ref ( ) . as_ptr ( ) . cast_mut ( ) )
396
351
. collect ( ) ;
397
- v. push ( std:: ptr:: null :: < libc :: c_char > ( ) as * mut libc :: c_char ) ;
352
+ v. push ( std:: ptr:: null_mut ( ) ) ;
398
353
v
399
354
}
400
355
@@ -422,12 +377,9 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
422
377
env_p. as_ptr ( ) ,
423
378
)
424
379
} ;
380
+ Errno :: result ( res) ?;
425
381
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) )
431
383
}
432
384
433
385
/// 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>>(
454
406
env_p. as_ptr ( ) ,
455
407
)
456
408
} ;
409
+ Errno :: result ( res) ?;
457
410
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) )
463
412
}
0 commit comments