@@ -255,6 +255,143 @@ impl Signal {
255
255
pub const unsafe fn from_raw_nonzero_unchecked ( sig : NonZeroI32 ) -> Self {
256
256
Self ( sig)
257
257
}
258
+
259
+ /// Convert a raw named signal number into a `Signal`.
260
+ ///
261
+ /// If the given signal number corresponds to one of the named constant
262
+ /// signal values, such as [`Signal::HUP`] or [`Signal::INT`], return the
263
+ /// `Signal` value. Otherwise return `None`.
264
+ ///
265
+ /// Signals in the range `SIGRTMIN` through `SIGRTMAX` are not supported by
266
+ /// this function. For a constructor that does recognize those values, see
267
+ /// [`Signal::from_raw`] in [rustix-libc-wrappers].
268
+ ///
269
+ /// [`Signal::from_raw`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.from_raw
270
+ /// [rustix-libc-wrappers]: https://docs.rs/rustix-libc-wrappers
271
+ pub const fn from_named_raw ( sig : i32 ) -> Option < Self > {
272
+ if let Some ( sig) = NonZeroI32 :: new ( sig) {
273
+ Self :: from_named_raw_nonzero ( sig)
274
+ } else {
275
+ None
276
+ }
277
+ }
278
+
279
+ /// Convert a raw non-zero named signal number into a `Signal`.
280
+ ///
281
+ /// If the given signal number corresponds to one of the constant signal
282
+ /// values, such as [`Signal::HUP`] or [`Signal::INT`], return the
283
+ /// `Signal` value. Otherwise return `None`.
284
+ ///
285
+ /// Signals in the range `SIGRTMIN` through `SIGRTMAX` are not supported by
286
+ /// this function. For a constructor that does recognize those values, see
287
+ /// [`Signal::from_raw_nonzero`] in [rustix-libc-wrappers].
288
+ ///
289
+ /// [`Signal::from_raw_nonzero`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.from_raw_nonzero
290
+ /// [rustix-libc-wrappers]: https://docs.rs/rustix-libc-wrappers
291
+ pub const fn from_named_raw_nonzero ( sig : NonZeroI32 ) -> Option < Self > {
292
+ match sig. get ( ) {
293
+ c:: SIGHUP => Some ( Self :: HUP ) ,
294
+ c:: SIGINT => Some ( Self :: INT ) ,
295
+ c:: SIGQUIT => Some ( Self :: QUIT ) ,
296
+ c:: SIGILL => Some ( Self :: ILL ) ,
297
+ c:: SIGTRAP => Some ( Self :: TRAP ) ,
298
+ c:: SIGABRT => Some ( Self :: ABORT ) ,
299
+ c:: SIGBUS => Some ( Self :: BUS ) ,
300
+ c:: SIGFPE => Some ( Self :: FPE ) ,
301
+ c:: SIGKILL => Some ( Self :: KILL ) ,
302
+ #[ cfg( not( target_os = "vita" ) ) ]
303
+ c:: SIGUSR1 => Some ( Self :: USR1 ) ,
304
+ c:: SIGSEGV => Some ( Self :: SEGV ) ,
305
+ #[ cfg( not( target_os = "vita" ) ) ]
306
+ c:: SIGUSR2 => Some ( Self :: USR2 ) ,
307
+ c:: SIGPIPE => Some ( Self :: PIPE ) ,
308
+ c:: SIGALRM => Some ( Self :: ALARM ) ,
309
+ c:: SIGTERM => Some ( Self :: TERM ) ,
310
+ #[ cfg( not( any(
311
+ bsd,
312
+ solarish,
313
+ target_os = "aix" ,
314
+ target_os = "haiku" ,
315
+ target_os = "horizon" ,
316
+ target_os = "hurd" ,
317
+ target_os = "nto" ,
318
+ target_os = "vita" ,
319
+ all(
320
+ linux_kernel,
321
+ any(
322
+ target_arch = "mips" ,
323
+ target_arch = "mips32r6" ,
324
+ target_arch = "mips64" ,
325
+ target_arch = "mips64r6" ,
326
+ target_arch = "sparc" ,
327
+ target_arch = "sparc64"
328
+ ) ,
329
+ )
330
+ ) ) ) ]
331
+ c:: SIGSTKFLT => Some ( Self :: STKFLT ) ,
332
+ #[ cfg( not( target_os = "vita" ) ) ]
333
+ c:: SIGCHLD => Some ( Self :: CHILD ) ,
334
+ #[ cfg( not( target_os = "vita" ) ) ]
335
+ c:: SIGCONT => Some ( Self :: CONT ) ,
336
+ #[ cfg( not( target_os = "vita" ) ) ]
337
+ c:: SIGSTOP => Some ( Self :: STOP ) ,
338
+ #[ cfg( not( target_os = "vita" ) ) ]
339
+ c:: SIGTSTP => Some ( Self :: TSTP ) ,
340
+ #[ cfg( not( target_os = "vita" ) ) ]
341
+ c:: SIGTTIN => Some ( Self :: TTIN ) ,
342
+ #[ cfg( not( target_os = "vita" ) ) ]
343
+ c:: SIGTTOU => Some ( Self :: TTOU ) ,
344
+ #[ cfg( not( target_os = "vita" ) ) ]
345
+ c:: SIGURG => Some ( Self :: URG ) ,
346
+ #[ cfg( not( target_os = "vita" ) ) ]
347
+ c:: SIGXCPU => Some ( Self :: XCPU ) ,
348
+ #[ cfg( not( target_os = "vita" ) ) ]
349
+ c:: SIGXFSZ => Some ( Self :: XFSZ ) ,
350
+ #[ cfg( not( target_os = "vita" ) ) ]
351
+ c:: SIGVTALRM => Some ( Self :: VTALARM ) ,
352
+ #[ cfg( not( target_os = "vita" ) ) ]
353
+ c:: SIGPROF => Some ( Self :: PROF ) ,
354
+ #[ cfg( not( target_os = "vita" ) ) ]
355
+ c:: SIGWINCH => Some ( Self :: WINCH ) ,
356
+ #[ cfg( not( any( target_os = "haiku" , target_os = "vita" ) ) ) ]
357
+ c:: SIGIO => Some ( Self :: IO ) ,
358
+ #[ cfg( not( any(
359
+ bsd,
360
+ target_os = "haiku" ,
361
+ target_os = "horizon" ,
362
+ target_os = "hurd" ,
363
+ target_os = "vita"
364
+ ) ) ) ]
365
+ c:: SIGPWR => Some ( Self :: POWER ) ,
366
+ c:: SIGSYS => Some ( Self :: SYS ) ,
367
+ #[ cfg( any(
368
+ bsd,
369
+ solarish,
370
+ target_os = "aix" ,
371
+ target_os = "hermit" ,
372
+ all(
373
+ linux_kernel,
374
+ any(
375
+ target_arch = "mips" ,
376
+ target_arch = "mips32r6" ,
377
+ target_arch = "mips64" ,
378
+ target_arch = "mips64r6" ,
379
+ target_arch = "sparc" ,
380
+ target_arch = "sparc64"
381
+ )
382
+ )
383
+ ) ) ]
384
+ c:: SIGEMT => Some ( Self :: EMT ) ,
385
+ #[ cfg( bsd) ]
386
+ c:: SIGINFO => Some ( Self :: INFO ) ,
387
+ #[ cfg( target_os = "freebsd" ) ]
388
+ c:: SIGTHR => Some ( Self :: THR ) ,
389
+ #[ cfg( target_os = "freebsd" ) ]
390
+ c:: SIGLIBRT => Some ( Self :: LIBRT ) ,
391
+
392
+ _ => None ,
393
+ }
394
+ }
258
395
}
259
396
260
397
impl fmt:: Debug for Signal {
@@ -384,4 +521,18 @@ mod tests {
384
521
) ;
385
522
}
386
523
}
524
+
525
+ #[ test]
526
+ fn test_named ( ) {
527
+ assert_eq ! ( Signal :: from_named_raw( -1 ) , None ) ;
528
+ assert_eq ! ( Signal :: from_named_raw( 0 ) , None ) ;
529
+ assert_eq ! ( Signal :: from_named_raw( c:: SIGHUP ) , Some ( Signal :: HUP ) ) ;
530
+ assert_eq ! ( Signal :: from_named_raw( c:: SIGSEGV ) , Some ( Signal :: SEGV ) ) ;
531
+ assert_eq ! ( Signal :: from_named_raw( c:: SIGSYS ) , Some ( Signal :: SYS ) ) ;
532
+ #[ cfg( any( linux_like, solarish, target_os = "hurd" ) ) ]
533
+ {
534
+ assert_eq ! ( Signal :: from_named_raw( libc:: SIGRTMIN ( ) ) , None ) ;
535
+ assert_eq ! ( Signal :: from_named_raw( libc:: SIGRTMAX ( ) ) , None ) ;
536
+ }
537
+ }
387
538
}
0 commit comments