Coverage Report

Created: 2026-01-17 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rustix-1.1.3/src/signal.rs
Line
Count
Source
1
//! Signal numbers.
2
//!
3
//! # Safety
4
//!
5
//! Some signal numbers are reserved by the libc.
6
//! [`Signal::from_raw_unchecked`] and [`Signal::from_raw_nonzero_unchecked`]
7
//! allow constructing `Signal` values with arbitrary values. Users must avoid
8
//! using reserved values to send, consume, or block any signals or alter any
9
//! signal handlers.
10
//!
11
//! See the individual functions' safety comments for more details.
12
#![allow(unsafe_code)]
13
14
use crate::backend::c;
15
use core::fmt;
16
use core::num::NonZeroI32;
17
18
/// A signal number for use with [`kill_process`], [`kill_process_group`], and
19
/// [`kill_current_process_group`].
20
///
21
/// For additional constructors such as [`Signal::rt_min`]
22
/// (aka `libc::SIGRTMIN`), [`Signal::rt_max`] (aka `libc::SIGRTMAX`),
23
/// [`Signal::rt`] (aka `|n| libc::SIGRTMIN() + n`), [`Signal::from_raw`], and
24
/// [`Signal::from_raw_nonzero`], see [rustix-libc-wrappers].
25
///
26
/// # References
27
///  - [POSIX]
28
///  - [Linux]
29
///  - [glibc]
30
///
31
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/signal.h.html
32
/// [Linux]: https://man7.org/linux/man-pages/man7/signal.7.html
33
/// [glibc]: https://sourceware.org/glibc/manual/latest/html_node/Standard-Signals.html
34
///
35
/// [`kill_process`]: crate::process::kill_process
36
/// [`kill_process_group`]: crate::process::kill_process_group
37
/// [`kill_current_process_group`]: crate::process::kill_current_process_group
38
/// [`Signal::rt_min`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.rt_min
39
/// [`Signal::rt_max`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.rt_max
40
/// [`Signal::rt`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.rt
41
/// [`Signal::from_raw`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.from_raw
42
/// [`Signal::from_raw_nonzero`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.from_raw_nonzero
43
/// [rustix-libc-wrappers]: https://docs.rs/rustix-libc-wrappers
44
#[doc(alias = "SIGRTMIN")]
45
#[doc(alias = "SIGRTMAX")]
46
#[derive(Copy, Clone, Eq, PartialEq)]
47
#[repr(transparent)]
48
pub struct Signal(NonZeroI32);
49
50
// SAFETY: The libc-defined signal values are all non-zero.
51
#[rustfmt::skip]
52
impl Signal {
53
    /// `SIGHUP`
54
    pub const HUP: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGHUP) });
55
    /// `SIGINT`
56
    pub const INT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGINT) });
57
    /// `SIGQUIT`
58
    pub const QUIT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGQUIT) });
59
    /// `SIGILL`
60
    pub const ILL: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGILL) });
61
    /// `SIGTRAP`
62
    pub const TRAP: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTRAP) });
63
    /// `SIGABRT`, aka `SIGIOT`
64
    #[doc(alias = "IOT")]
65
    #[doc(alias = "ABRT")]
66
    pub const ABORT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGABRT) });
67
    /// `SIGBUS`
68
    pub const BUS: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGBUS) });
69
    /// `SIGFPE`
70
    pub const FPE: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGFPE) });
71
    /// `SIGKILL`
72
    pub const KILL: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGKILL) });
73
    /// `SIGUSR1`
74
    #[cfg(not(target_os = "vita"))]
75
    pub const USR1: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGUSR1) });
76
    /// `SIGSEGV`
77
    pub const SEGV: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGSEGV) });
78
    /// `SIGUSR2`
79
    #[cfg(not(target_os = "vita"))]
80
    pub const USR2: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGUSR2) });
81
    /// `SIGPIPE`
82
    pub const PIPE: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGPIPE) });
83
    /// `SIGALRM`
84
    #[doc(alias = "ALRM")]
85
    pub const ALARM: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGALRM) });
86
    /// `SIGTERM`
87
    pub const TERM: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTERM) });
88
    /// `SIGSTKFLT`
89
    #[cfg(not(any(
90
        bsd,
91
        solarish,
92
        target_os = "aix",
93
        target_os = "cygwin",
94
        target_os = "haiku",
95
        target_os = "horizon",
96
        target_os = "hurd",
97
        target_os = "nto",
98
        target_os = "vita",
99
        all(
100
            linux_kernel,
101
            any(
102
                target_arch = "mips",
103
                target_arch = "mips32r6",
104
                target_arch = "mips64",
105
                target_arch = "mips64r6",
106
                target_arch = "sparc",
107
                target_arch = "sparc64"
108
            ),
109
        ),
110
    )))]
111
    pub const STKFLT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGSTKFLT) });
112
    /// `SIGCHLD`
113
    #[cfg(not(target_os = "vita"))]
114
    #[doc(alias = "CHLD")]
115
    pub const CHILD: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGCHLD) });
116
    /// `SIGCONT`
117
    #[cfg(not(target_os = "vita"))]
118
    pub const CONT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGCONT) });
119
    /// `SIGSTOP`
120
    #[cfg(not(target_os = "vita"))]
121
    pub const STOP: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGSTOP) });
122
    /// `SIGTSTP`
123
    #[cfg(not(target_os = "vita"))]
124
    pub const TSTP: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTSTP) });
125
    /// `SIGTTIN`
126
    #[cfg(not(target_os = "vita"))]
127
    pub const TTIN: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTTIN) });
128
    /// `SIGTTOU`
129
    #[cfg(not(target_os = "vita"))]
130
    pub const TTOU: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTTOU) });
131
    /// `SIGURG`
132
    #[cfg(not(target_os = "vita"))]
133
    pub const URG: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGURG) });
134
    /// `SIGXCPU`
135
    #[cfg(not(target_os = "vita"))]
136
    pub const XCPU: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGXCPU) });
137
    /// `SIGXFSZ`
138
    #[cfg(not(target_os = "vita"))]
139
    pub const XFSZ: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGXFSZ) });
140
    /// `SIGVTALRM`
141
    #[cfg(not(target_os = "vita"))]
142
    #[doc(alias = "VTALRM")]
143
    pub const VTALARM: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGVTALRM) });
144
    /// `SIGPROF`
145
    #[cfg(not(target_os = "vita"))]
146
    pub const PROF: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGPROF) });
147
    /// `SIGWINCH`
148
    #[cfg(not(target_os = "vita"))]
149
    pub const WINCH: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGWINCH) });
150
    /// `SIGIO`, aka `SIGPOLL`
151
    #[doc(alias = "POLL")]
152
    #[cfg(not(any(target_os = "haiku", target_os = "vita")))]
153
    pub const IO: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGIO) });
154
    /// `SIGPWR`
155
    #[cfg(not(any(
156
        bsd,
157
        target_os = "haiku",
158
        target_os = "horizon",
159
        target_os = "hurd",
160
        target_os = "vita"
161
    )))]
162
    #[doc(alias = "PWR")]
163
    pub const POWER: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGPWR) });
164
    /// `SIGSYS`, aka `SIGUNUSED`
165
    #[doc(alias = "UNUSED")]
166
    pub const SYS: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGSYS) });
167
    /// `SIGEMT`
168
    #[cfg(any(
169
        bsd,
170
        solarish,
171
        target_os = "aix",
172
        target_os = "hermit",
173
        all(
174
            linux_kernel,
175
            any(
176
                target_arch = "mips",
177
                target_arch = "mips32r6",
178
                target_arch = "mips64",
179
                target_arch = "mips64r6",
180
                target_arch = "sparc",
181
                target_arch = "sparc64"
182
            )
183
        )
184
    ))]
185
    pub const EMT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGEMT) });
186
    /// `SIGINFO`
187
    #[cfg(bsd)]
188
    pub const INFO: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGINFO) });
189
    /// `SIGTHR`
190
    #[cfg(target_os = "freebsd")]
191
    #[doc(alias = "LWP")]
192
    pub const THR: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGTHR) });
193
    /// `SIGLIBRT`
194
    #[cfg(target_os = "freebsd")]
195
    pub const LIBRT: Self = Self(unsafe { NonZeroI32::new_unchecked(c::SIGLIBRT) });
196
}
197
198
impl Signal {
199
    /// Convert a `Signal` to a raw signal number.
200
    ///
201
    /// To convert to a `NonZeroI32`, use [`Signal::as_raw_nonzero`].
202
    #[inline]
203
0
    pub const fn as_raw(self) -> i32 {
204
0
        self.0.get()
205
0
    }
206
207
    /// Convert a `Signal` to a raw non-zero signal number.
208
    #[inline]
209
0
    pub const fn as_raw_nonzero(self) -> NonZeroI32 {
210
0
        self.0
211
0
    }
212
213
    /// Convert a raw signal number into a `Signal` without checks.
214
    ///
215
    /// For a safe checked version, see [`Signal::from_raw`] in
216
    /// [rustix-libc-wrappers].
217
    ///
218
    /// # Safety
219
    ///
220
    /// `sig` must be a valid and non-zero signal number.
221
    ///
222
    /// And, if `sig` is a signal number reserved by the libc, such as a value
223
    /// from the libc [`SIGRTMIN`] to the libc [`SIGRTMAX`], inclusive, then
224
    /// the resulting `Signal` must not be used to send, consume, or block any
225
    /// signals or alter any signal handlers.
226
    ///
227
    /// [`Signal::from_raw`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.from_raw
228
    /// [rustix-libc-wrappers]: https://docs.rs/rustix-libc-wrappers
229
    /// [`SIGRTMIN`]: https://docs.rs/libc/*/libc/fn.SIGRTMIN.html
230
    /// [`SIGRTMAX`]: https://docs.rs/libc/*/libc/fn.SIGRTMAX.html
231
    #[inline]
232
0
    pub const unsafe fn from_raw_unchecked(sig: i32) -> Self {
233
0
        Self::from_raw_nonzero_unchecked(NonZeroI32::new_unchecked(sig))
234
0
    }
235
236
    /// Convert a raw non-zero signal number into a `Signal` without checks.
237
    ///
238
    /// For a safe checked version, see [`Signal::from_raw_nonzero`] in
239
    /// [rustix-libc-wrappers].
240
    ///
241
    /// # Safety
242
    ///
243
    /// `sig` must be a valid signal number.
244
    ///
245
    /// And, if `sig` is a signal number reserved by the libc, such as a value
246
    /// from [`SIGRTMIN`] to [`SIGRTMAX`] inclusive, then the resulting
247
    /// `Signal` must not be used to send, consume, or block any signals or
248
    /// alter any signal handlers.
249
    ///
250
    /// [`Signal::from_raw_nonzero`]: https://docs.rs/rustix-libc-wrappers/*/rustix_libc_wrappers/trait.SignalExt.html#tymethod.from_raw_nonzero
251
    /// [rustix-libc-wrappers]: https://docs.rs/rustix-libc-wrappers
252
    /// [`SIGRTMIN`]: https://docs.rs/libc/*/libc/fn.SIGRTMIN.html
253
    /// [`SIGRTMAX`]: https://docs.rs/libc/*/libc/fn.SIGRTMAX.html
254
    #[inline]
255
0
    pub const unsafe fn from_raw_nonzero_unchecked(sig: NonZeroI32) -> Self {
256
0
        Self(sig)
257
0
    }
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
0
    pub const fn from_named_raw(sig: i32) -> Option<Self> {
272
0
        if let Some(sig) = NonZeroI32::new(sig) {
273
0
            Self::from_named_raw_nonzero(sig)
274
        } else {
275
0
            None
276
        }
277
0
    }
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
0
    pub const fn from_named_raw_nonzero(sig: NonZeroI32) -> Option<Self> {
292
0
        match sig.get() {
293
0
            c::SIGHUP => Some(Self::HUP),
294
0
            c::SIGINT => Some(Self::INT),
295
0
            c::SIGQUIT => Some(Self::QUIT),
296
0
            c::SIGILL => Some(Self::ILL),
297
0
            c::SIGTRAP => Some(Self::TRAP),
298
0
            c::SIGABRT => Some(Self::ABORT),
299
0
            c::SIGBUS => Some(Self::BUS),
300
0
            c::SIGFPE => Some(Self::FPE),
301
0
            c::SIGKILL => Some(Self::KILL),
302
            #[cfg(not(target_os = "vita"))]
303
0
            c::SIGUSR1 => Some(Self::USR1),
304
0
            c::SIGSEGV => Some(Self::SEGV),
305
            #[cfg(not(target_os = "vita"))]
306
0
            c::SIGUSR2 => Some(Self::USR2),
307
0
            c::SIGPIPE => Some(Self::PIPE),
308
0
            c::SIGALRM => Some(Self::ALARM),
309
0
            c::SIGTERM => Some(Self::TERM),
310
            #[cfg(not(any(
311
                bsd,
312
                solarish,
313
                target_os = "aix",
314
                target_os = "cygwin",
315
                target_os = "haiku",
316
                target_os = "horizon",
317
                target_os = "hurd",
318
                target_os = "nto",
319
                target_os = "vita",
320
                all(
321
                    linux_kernel,
322
                    any(
323
                        target_arch = "mips",
324
                        target_arch = "mips32r6",
325
                        target_arch = "mips64",
326
                        target_arch = "mips64r6",
327
                        target_arch = "sparc",
328
                        target_arch = "sparc64"
329
                    ),
330
                )
331
            )))]
332
0
            c::SIGSTKFLT => Some(Self::STKFLT),
333
            #[cfg(not(target_os = "vita"))]
334
0
            c::SIGCHLD => Some(Self::CHILD),
335
            #[cfg(not(target_os = "vita"))]
336
0
            c::SIGCONT => Some(Self::CONT),
337
            #[cfg(not(target_os = "vita"))]
338
0
            c::SIGSTOP => Some(Self::STOP),
339
            #[cfg(not(target_os = "vita"))]
340
0
            c::SIGTSTP => Some(Self::TSTP),
341
            #[cfg(not(target_os = "vita"))]
342
0
            c::SIGTTIN => Some(Self::TTIN),
343
            #[cfg(not(target_os = "vita"))]
344
0
            c::SIGTTOU => Some(Self::TTOU),
345
            #[cfg(not(target_os = "vita"))]
346
0
            c::SIGURG => Some(Self::URG),
347
            #[cfg(not(target_os = "vita"))]
348
0
            c::SIGXCPU => Some(Self::XCPU),
349
            #[cfg(not(target_os = "vita"))]
350
0
            c::SIGXFSZ => Some(Self::XFSZ),
351
            #[cfg(not(target_os = "vita"))]
352
0
            c::SIGVTALRM => Some(Self::VTALARM),
353
            #[cfg(not(target_os = "vita"))]
354
0
            c::SIGPROF => Some(Self::PROF),
355
            #[cfg(not(target_os = "vita"))]
356
0
            c::SIGWINCH => Some(Self::WINCH),
357
            #[cfg(not(any(target_os = "haiku", target_os = "vita")))]
358
0
            c::SIGIO => Some(Self::IO),
359
            #[cfg(not(any(
360
                bsd,
361
                target_os = "haiku",
362
                target_os = "horizon",
363
                target_os = "hurd",
364
                target_os = "vita"
365
            )))]
366
0
            c::SIGPWR => Some(Self::POWER),
367
0
            c::SIGSYS => Some(Self::SYS),
368
            #[cfg(any(
369
                bsd,
370
                solarish,
371
                target_os = "aix",
372
                target_os = "hermit",
373
                all(
374
                    linux_kernel,
375
                    any(
376
                        target_arch = "mips",
377
                        target_arch = "mips32r6",
378
                        target_arch = "mips64",
379
                        target_arch = "mips64r6",
380
                        target_arch = "sparc",
381
                        target_arch = "sparc64"
382
                    )
383
                )
384
            ))]
385
            c::SIGEMT => Some(Self::EMT),
386
            #[cfg(bsd)]
387
            c::SIGINFO => Some(Self::INFO),
388
            #[cfg(target_os = "freebsd")]
389
            c::SIGTHR => Some(Self::THR),
390
            #[cfg(target_os = "freebsd")]
391
            c::SIGLIBRT => Some(Self::LIBRT),
392
393
0
            _ => None,
394
        }
395
0
    }
396
}
397
398
impl fmt::Debug for Signal {
399
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
400
0
        match *self {
401
0
            Self::HUP => "Signal::HUP".fmt(f),
402
0
            Self::INT => "Signal::INT".fmt(f),
403
0
            Self::QUIT => "Signal::QUIT".fmt(f),
404
0
            Self::ILL => "Signal::ILL".fmt(f),
405
0
            Self::TRAP => "Signal::TRAP".fmt(f),
406
0
            Self::ABORT => "Signal::ABORT".fmt(f),
407
0
            Self::BUS => "Signal::BUS".fmt(f),
408
0
            Self::FPE => "Signal::FPE".fmt(f),
409
0
            Self::KILL => "Signal::KILL".fmt(f),
410
            #[cfg(not(target_os = "vita"))]
411
0
            Self::USR1 => "Signal::USR1".fmt(f),
412
0
            Self::SEGV => "Signal::SEGV".fmt(f),
413
            #[cfg(not(target_os = "vita"))]
414
0
            Self::USR2 => "Signal::USR2".fmt(f),
415
0
            Self::PIPE => "Signal::PIPE".fmt(f),
416
0
            Self::ALARM => "Signal::ALARM".fmt(f),
417
0
            Self::TERM => "Signal::TERM".fmt(f),
418
            #[cfg(not(any(
419
                bsd,
420
                solarish,
421
                target_os = "aix",
422
                target_os = "cygwin",
423
                target_os = "haiku",
424
                target_os = "horizon",
425
                target_os = "hurd",
426
                target_os = "nto",
427
                target_os = "vita",
428
                all(
429
                    linux_kernel,
430
                    any(
431
                        target_arch = "mips",
432
                        target_arch = "mips32r6",
433
                        target_arch = "mips64",
434
                        target_arch = "mips64r6",
435
                        target_arch = "sparc",
436
                        target_arch = "sparc64"
437
                    ),
438
                ),
439
            )))]
440
0
            Self::STKFLT => "Signal::STKFLT".fmt(f),
441
            #[cfg(not(target_os = "vita"))]
442
0
            Self::CHILD => "Signal::CHILD".fmt(f),
443
            #[cfg(not(target_os = "vita"))]
444
0
            Self::CONT => "Signal::CONT".fmt(f),
445
            #[cfg(not(target_os = "vita"))]
446
0
            Self::STOP => "Signal::STOP".fmt(f),
447
            #[cfg(not(target_os = "vita"))]
448
0
            Self::TSTP => "Signal::TSTP".fmt(f),
449
            #[cfg(not(target_os = "vita"))]
450
0
            Self::TTIN => "Signal::TTIN".fmt(f),
451
            #[cfg(not(target_os = "vita"))]
452
0
            Self::TTOU => "Signal::TTOU".fmt(f),
453
            #[cfg(not(target_os = "vita"))]
454
0
            Self::URG => "Signal::URG".fmt(f),
455
            #[cfg(not(target_os = "vita"))]
456
0
            Self::XCPU => "Signal::XCPU".fmt(f),
457
            #[cfg(not(target_os = "vita"))]
458
0
            Self::XFSZ => "Signal::XFSZ".fmt(f),
459
            #[cfg(not(target_os = "vita"))]
460
0
            Self::VTALARM => "Signal::VTALARM".fmt(f),
461
            #[cfg(not(target_os = "vita"))]
462
0
            Self::PROF => "Signal::PROF".fmt(f),
463
            #[cfg(not(target_os = "vita"))]
464
0
            Self::WINCH => "Signal::WINCH".fmt(f),
465
            #[cfg(not(any(target_os = "haiku", target_os = "vita")))]
466
0
            Self::IO => "Signal::IO".fmt(f),
467
            #[cfg(not(any(
468
                bsd,
469
                target_os = "haiku",
470
                target_os = "horizon",
471
                target_os = "hurd",
472
                target_os = "vita"
473
            )))]
474
0
            Self::POWER => "Signal::POWER".fmt(f),
475
0
            Self::SYS => "Signal::SYS".fmt(f),
476
            #[cfg(any(
477
                bsd,
478
                solarish,
479
                target_os = "aix",
480
                target_os = "hermit",
481
                all(
482
                    linux_kernel,
483
                    any(
484
                        target_arch = "mips",
485
                        target_arch = "mips32r6",
486
                        target_arch = "mips64",
487
                        target_arch = "mips64r6",
488
                        target_arch = "sparc",
489
                        target_arch = "sparc64"
490
                    )
491
                )
492
            ))]
493
            Self::EMT => "Signal::EMT".fmt(f),
494
            #[cfg(bsd)]
495
            Self::INFO => "Signal::INFO".fmt(f),
496
            #[cfg(target_os = "freebsd")]
497
            Self::THR => "Signal::THR".fmt(f),
498
            #[cfg(target_os = "freebsd")]
499
            Self::LIBRT => "Signal::LIBRT".fmt(f),
500
501
0
            n => {
502
0
                "Signal::from_raw(".fmt(f)?;
503
0
                n.as_raw().fmt(f)?;
504
0
                ")".fmt(f)
505
            }
506
        }
507
0
    }
508
}
509
510
#[cfg(test)]
511
mod tests {
512
    use super::*;
513
514
    #[test]
515
    fn test_basics() {
516
        assert_eq!(Signal::HUP.as_raw(), libc::SIGHUP);
517
        unsafe {
518
            assert_eq!(Signal::from_raw_unchecked(libc::SIGHUP), Signal::HUP);
519
            assert_eq!(
520
                Signal::from_raw_nonzero_unchecked(NonZeroI32::new(libc::SIGHUP).unwrap()),
521
                Signal::HUP
522
            );
523
        }
524
    }
525
526
    #[test]
527
    fn test_named() {
528
        assert_eq!(Signal::from_named_raw(-1), None);
529
        assert_eq!(Signal::from_named_raw(0), None);
530
        assert_eq!(Signal::from_named_raw(c::SIGHUP), Some(Signal::HUP));
531
        assert_eq!(Signal::from_named_raw(c::SIGSEGV), Some(Signal::SEGV));
532
        assert_eq!(Signal::from_named_raw(c::SIGSYS), Some(Signal::SYS));
533
        #[cfg(any(linux_like, solarish, target_os = "hurd"))]
534
        {
535
            assert_eq!(Signal::from_named_raw(libc::SIGRTMIN()), None);
536
            assert_eq!(Signal::from_named_raw(libc::SIGRTMAX()), None);
537
        }
538
    }
539
}