@@ -29,9 +29,7 @@ impl PosixSpawnAttr {
29
29
let mut attr = mem:: MaybeUninit :: uninit ( ) ;
30
30
let res = unsafe { libc:: posix_spawnattr_init ( attr. as_mut_ptr ( ) ) } ;
31
31
32
- if res != 0 {
33
- return Err ( Errno :: from_raw ( res) ) ;
34
- }
32
+ Errno :: result ( res) ?;
35
33
36
34
let attr = unsafe { attr. assume_init ( ) } ;
37
35
Ok ( PosixSpawnAttr { attr } )
@@ -49,20 +47,9 @@ impl PosixSpawnAttr {
49
47
& mut self . attr as * mut libc:: posix_spawnattr_t ,
50
48
)
51
49
} ;
52
- if res != 0 {
53
- return Err ( Errno :: from_raw ( res) ) ;
54
- }
50
+ Errno :: result ( res) ?;
55
51
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 )
66
53
}
67
54
68
55
/// Set spawn flags. See
@@ -75,27 +62,23 @@ impl PosixSpawnAttr {
75
62
flags. bits ( ) as libc:: c_short ,
76
63
)
77
64
} ;
78
- if res != 0 {
79
- return Err ( Errno :: from_raw ( res) ) ;
80
- }
65
+ Errno :: result ( res) ?;
81
66
82
67
Ok ( ( ) )
83
68
}
84
69
85
70
/// Get spawn flags. See
86
71
/// [posix_spawnattr_getflags](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getflags.html).
87
72
#[ doc( alias( "posix_spawnattr_getflags" ) ) ]
88
- pub fn flags ( & mut self ) -> Result < PosixSpawnFlags > {
73
+ pub fn flags ( & self ) -> Result < PosixSpawnFlags > {
89
74
let mut flags: libc:: c_short = 0 ;
90
75
let res = unsafe {
91
76
libc:: posix_spawnattr_getflags (
92
- & mut self . attr as * mut libc:: posix_spawnattr_t ,
77
+ & self . attr as * const libc:: posix_spawnattr_t ,
93
78
& mut flags,
94
79
)
95
80
} ;
96
- if res != 0 {
97
- return Err ( Errno :: from_raw ( res) ) ;
98
- }
81
+ Errno :: result ( res) ?;
99
82
100
83
Ok ( PosixSpawnFlags :: from_bits_truncate ( flags. into ( ) ) )
101
84
}
@@ -110,30 +93,22 @@ impl PosixSpawnAttr {
110
93
pgroup. as_raw ( ) ,
111
94
)
112
95
} ;
113
- if res != 0 {
114
- return Err ( Errno :: from_raw ( res) ) ;
115
- }
116
-
117
- Ok ( ( ) )
96
+ Errno :: result ( res) . map ( drop)
118
97
}
119
98
120
99
/// Get spawn pgroup. See
121
100
/// [posix_spawnattr_getpgroup](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getpgroup.html).
122
101
#[ doc( alias( "posix_spawnattr_getpgroup" ) ) ]
123
- pub fn pgroup ( & mut self ) -> Result < Pid > {
102
+ pub fn pgroup ( & self ) -> Result < Pid > {
124
103
let mut pid: libc:: pid_t = 0 ;
125
104
126
105
let res = unsafe {
127
106
libc:: posix_spawnattr_getpgroup (
128
- & mut self . attr as * mut libc:: posix_spawnattr_t ,
107
+ & self . attr as * const libc:: posix_spawnattr_t ,
129
108
& mut pid,
130
109
)
131
110
} ;
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) )
137
112
}
138
113
139
114
feature ! {
@@ -148,28 +123,22 @@ impl PosixSpawnAttr {
148
123
sigdefault. as_ref( ) ,
149
124
)
150
125
} ;
151
- if res != 0 {
152
- return Err ( Errno :: from_raw( res) ) ;
153
- }
154
-
155
- Ok ( ( ) )
126
+ Errno :: result( res) . map( drop)
156
127
}
157
128
158
129
/// Get spawn sigdefault. See
159
130
/// [posix_spawnattr_getsigdefault](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigdefault.html).
160
131
#[ doc( alias( "posix_spawnattr_getsigdefault" ) ) ]
161
- pub fn sigdefault( & mut self ) -> Result <SigSet > {
132
+ pub fn sigdefault( & self ) -> Result <SigSet > {
162
133
let mut sigset = mem:: MaybeUninit :: uninit( ) ;
163
134
164
135
let res = unsafe {
165
136
libc:: posix_spawnattr_getsigdefault(
166
- & mut self . attr as * mut libc:: posix_spawnattr_t,
137
+ & self . attr as * const libc:: posix_spawnattr_t,
167
138
sigset. as_mut_ptr( ) ,
168
139
)
169
140
} ;
170
- if res != 0 {
171
- return Err ( Errno :: from_raw( res) ) ;
172
- }
141
+ Errno :: result( res) ?;
173
142
174
143
let sigdefault =
175
144
unsafe { SigSet :: from_sigset_t_unchecked( sigset. assume_init( ) ) } ;
@@ -186,28 +155,22 @@ impl PosixSpawnAttr {
186
155
sigdefault. as_ref( ) ,
187
156
)
188
157
} ;
189
- if res != 0 {
190
- return Err ( Errno :: from_raw( res) ) ;
191
- }
192
-
193
- Ok ( ( ) )
158
+ Errno :: result( res) . map( drop)
194
159
}
195
160
196
161
/// Get spawn sigmask. See
197
162
/// [posix_spawnattr_getsigmask](https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawnattr_getsigmask.html).
198
163
#[ doc( alias( "posix_spawnattr_getsigmask" ) ) ]
199
- pub fn sigmask( & mut self ) -> Result <SigSet > {
164
+ pub fn sigmask( & self ) -> Result <SigSet > {
200
165
let mut sigset = mem:: MaybeUninit :: uninit( ) ;
201
166
202
167
let res = unsafe {
203
168
libc:: posix_spawnattr_getsigmask(
204
- & mut self . attr as * mut libc:: posix_spawnattr_t,
169
+ & self . attr as * const libc:: posix_spawnattr_t,
205
170
sigset. as_mut_ptr( ) ,
206
171
)
207
172
} ;
208
- if res != 0 {
209
- return Err ( Errno :: from_raw( res) ) ;
210
- }
173
+ Errno :: result( res) ?;
211
174
212
175
let sigdefault =
213
176
unsafe { SigSet :: from_sigset_t_unchecked( sigset. assume_init( ) ) } ;
@@ -269,15 +232,9 @@ impl PosixSpawnFileActions {
269
232
libc:: posix_spawn_file_actions_init ( actions. as_mut_ptr ( ) )
270
233
} ;
271
234
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 ( ) } )
281
238
}
282
239
283
240
/// Reinitialize the spawn file actions object.
@@ -292,20 +249,14 @@ impl PosixSpawnFileActions {
292
249
& mut self . fa as * mut libc:: posix_spawn_file_actions_t ,
293
250
)
294
251
} ;
295
- if res != 0 {
296
- return Err ( Errno :: from_raw ( res) ) ;
297
- }
252
+ Errno :: result ( res) ?;
298
253
299
254
let res = unsafe {
300
255
libc:: posix_spawn_file_actions_init (
301
256
& mut self . fa as * mut libc:: posix_spawn_file_actions_t ,
302
257
)
303
258
} ;
304
- if res != 0 {
305
- return Err ( Errno :: from_raw ( res) ) ;
306
- }
307
-
308
- Ok ( self )
259
+ Errno :: result ( res) . map ( self )
309
260
}
310
261
311
262
/// Add a [dup2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup2.html) action. See
@@ -323,11 +274,7 @@ impl PosixSpawnFileActions {
323
274
newfd. as_fd ( ) . as_raw_fd ( ) ,
324
275
)
325
276
} ;
326
- if res != 0 {
327
- return Err ( Errno :: from_raw ( res) ) ;
328
- }
329
-
330
- Ok ( ( ) )
277
+ Errno :: result ( res) . map ( drop)
331
278
}
332
279
333
280
feature ! {
@@ -351,11 +298,7 @@ impl PosixSpawnFileActions {
351
298
mode. bits( ) ,
352
299
)
353
300
} ) ?;
354
- if res != 0 {
355
- return Err ( Errno :: from_raw( res) ) ;
356
- }
357
-
358
- Ok ( ( ) )
301
+ Errno :: result( res) . map( drop)
359
302
}
360
303
}
361
304
@@ -369,11 +312,7 @@ impl PosixSpawnFileActions {
369
312
fd. as_fd ( ) . as_raw_fd ( ) ,
370
313
)
371
314
} ;
372
- if res != 0 {
373
- return Err ( Errno :: from_raw ( res) ) ;
374
- }
375
-
376
- Ok ( ( ) )
315
+ Errno :: result ( res) . map ( drop)
377
316
}
378
317
}
379
318
@@ -392,9 +331,9 @@ impl Drop for PosixSpawnFileActions {
392
331
unsafe fn to_exec_array < S : AsRef < CStr > > ( args : & [ S ] ) -> Vec < * mut libc:: c_char > {
393
332
let mut v: Vec < * mut libc:: c_char > = args
394
333
. iter ( )
395
- . map ( |s| s. as_ref ( ) . as_ptr ( ) as * mut libc :: c_char )
334
+ . map ( |s| s. as_ref ( ) . as_ptr ( ) . cast_mut ( ) )
396
335
. collect ( ) ;
397
- v. push ( std:: ptr:: null :: < libc :: c_char > ( ) as * mut libc :: c_char ) ;
336
+ v. push ( std:: ptr:: null_mut ( ) ) ;
398
337
v
399
338
}
400
339
@@ -423,11 +362,7 @@ pub fn posix_spawn<SA: AsRef<CStr>, SE: AsRef<CStr>>(
423
362
)
424
363
} ;
425
364
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)
431
366
}
432
367
433
368
/// 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>>(
455
390
)
456
391
} ;
457
392
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)
463
394
}
0 commit comments