Skip to content

Commit 358adaf

Browse files
committed
Mark most C-derived enums as non_exhaustive
Since libc may add new variants at any time, Nix's consumers should not use exhaustive match patterns. Fixes #1182
1 parent 7033d47 commit 358adaf

File tree

17 files changed

+43
-0
lines changed

17 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1414
- `FdSet::{contains, highest, fds}` no longer require a mutable reference.
1515
(#[1464](https://github.com/nix-rust/nix/pull/1464))
1616

17+
- Most enums that come from C, for example `Errno`, are now marked as
18+
`#[non_exhaustive]`.
19+
(#[1474](https://github.com/nix-rust/nix/pull/1474))
20+
1721
### Fixed
1822

1923
- Added more errno definitions for better backwards compatibility with

src/errno.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ fn desc(errno: Errno) -> &'static str {
768768
mod consts {
769769
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
770770
#[repr(i32)]
771+
#[non_exhaustive]
771772
pub enum Errno {
772773
UnknownErrno = 0,
773774
EPERM = libc::EPERM,
@@ -1073,6 +1074,7 @@ mod consts {
10731074
mod consts {
10741075
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10751076
#[repr(i32)]
1077+
#[non_exhaustive]
10761078
pub enum Errno {
10771079
UnknownErrno = 0,
10781080
EPERM = libc::EPERM,
@@ -1324,6 +1326,7 @@ mod consts {
13241326
mod consts {
13251327
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13261328
#[repr(i32)]
1329+
#[non_exhaustive]
13271330
pub enum Errno {
13281331
UnknownErrno = 0,
13291332
EPERM = libc::EPERM,
@@ -1562,6 +1565,7 @@ mod consts {
15621565
mod consts {
15631566
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15641567
#[repr(i32)]
1568+
#[non_exhaustive]
15651569
pub enum Errno {
15661570
UnknownErrno = 0,
15671571
EPERM = libc::EPERM,
@@ -1796,6 +1800,7 @@ mod consts {
17961800
mod consts {
17971801
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
17981802
#[repr(i32)]
1803+
#[non_exhaustive]
17991804
pub enum Errno {
18001805
UnknownErrno = 0,
18011806
EPERM = libc::EPERM,
@@ -2019,6 +2024,7 @@ mod consts {
20192024
mod consts {
20202025
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20212026
#[repr(i32)]
2027+
#[non_exhaustive]
20222028
pub enum Errno {
20232029
UnknownErrno = 0,
20242030
EPERM = libc::EPERM,
@@ -2244,6 +2250,7 @@ mod consts {
22442250
mod consts {
22452251
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
22462252
#[repr(i32)]
2253+
#[non_exhaustive]
22472254
pub enum Errno {
22482255
UnknownErrno = 0,
22492256
EPERM = libc::EPERM,
@@ -2441,6 +2448,7 @@ mod consts {
24412448
mod consts {
24422449
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
24432450
#[repr(i32)]
2451+
#[non_exhaustive]
24442452
pub enum Errno {
24452453
UnknownErrno = 0,
24462454
EPERM = libc::EPERM,

src/fcntl.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ libc_bitflags!(
374374

375375
#[cfg(not(target_os = "redox"))]
376376
#[derive(Debug, Eq, Hash, PartialEq)]
377+
#[non_exhaustive]
377378
pub enum FcntlArg<'a> {
378379
F_DUPFD(RawFd),
379380
F_DUPFD_CLOEXEC(RawFd),
@@ -405,6 +406,7 @@ pub enum FcntlArg<'a> {
405406

406407
#[cfg(target_os = "redox")]
407408
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
409+
#[non_exhaustive]
408410
pub enum FcntlArg {
409411
F_DUPFD(RawFd),
410412
F_DUPFD_CLOEXEC(RawFd),
@@ -454,6 +456,7 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
454456
}
455457

456458
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
459+
#[non_exhaustive]
457460
pub enum FlockArg {
458461
LockShared,
459462
LockExclusive,
@@ -649,6 +652,7 @@ mod posix_fadvise {
649652

650653
libc_enum! {
651654
#[repr(i32)]
655+
#[non_exhaustive]
652656
pub enum PosixFadviseAdvice {
653657
POSIX_FADV_NORMAL,
654658
POSIX_FADV_SEQUENTIAL,

src/sys/aio.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ libc_enum! {
3939
/// Mode for `AioCb::fsync`. Controls whether only data or both data and
4040
/// metadata are synced.
4141
#[repr(i32)]
42+
#[non_exhaustive]
4243
pub enum AioFsyncMode {
4344
/// do it like `fsync`
4445
O_SYNC,
@@ -57,6 +58,7 @@ libc_enum! {
5758
/// given `aiocb` should be used for a read operation, a write operation, or
5859
/// ignored. Has no effect for any other aio functions.
5960
#[repr(i32)]
61+
#[non_exhaustive]
6062
pub enum LioOpcode {
6163
LIO_NOP,
6264
LIO_WRITE,

src/sys/epoll.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ libc_bitflags!(
2929

3030
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
3131
#[repr(i32)]
32+
#[non_exhaustive]
3233
pub enum EpollOp {
3334
EpollCtlAdd = libc::EPOLL_CTL_ADD,
3435
EpollCtlDel = libc::EPOLL_CTL_DEL,

src/sys/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type type_of_event_filter = i16;
3636
libc_enum! {
3737
#[cfg_attr(target_os = "netbsd", repr(u32))]
3838
#[cfg_attr(not(target_os = "netbsd"), repr(i16))]
39+
#[non_exhaustive]
3940
pub enum EventFilter {
4041
EVFILT_AIO,
4142
/// Returns whenever there is no remaining data in the write buffer

src/sys/mman.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ libc_enum!{
155155
///
156156
/// Used by [`madvise`](./fn.madvise.html).
157157
#[repr(i32)]
158+
#[non_exhaustive]
158159
pub enum MmapAdvise {
159160
/// No further special treatment. This is the default.
160161
MADV_NORMAL,

src/sys/ptrace/bsd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cfg_if! {
2424
libc_enum! {
2525
#[repr(i32)]
2626
/// Ptrace Request enum defining the action to be taken.
27+
#[non_exhaustive]
2728
pub enum Request {
2829
PT_TRACE_ME,
2930
PT_READ_I,

src/sys/ptrace/linux.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ libc_enum!{
3333
#[cfg_attr(not(any(target_env = "musl", target_os = "android")), repr(u32))]
3434
#[cfg_attr(any(target_env = "musl", target_os = "android"), repr(i32))]
3535
/// Ptrace Request enum defining the action to be taken.
36+
#[non_exhaustive]
3637
pub enum Request {
3738
PTRACE_TRACEME,
3839
PTRACE_PEEKTEXT,
@@ -123,6 +124,7 @@ libc_enum!{
123124
/// Using the ptrace options the tracer can configure the tracee to stop
124125
/// at certain events. This enum is used to define those events as defined
125126
/// in `man ptrace`.
127+
#[non_exhaustive]
126128
pub enum Event {
127129
/// Event that stops before a return from fork or clone.
128130
PTRACE_EVENT_FORK,

src/sys/quota.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ libc_enum!{
4242
libc_enum!{
4343
/// The scope of the quota.
4444
#[repr(i32)]
45+
#[non_exhaustive]
4546
pub enum QuotaType {
4647
/// Specify a user quota
4748
USRQUOTA,
@@ -53,6 +54,7 @@ libc_enum!{
5354
libc_enum!{
5455
/// The type of quota format to use.
5556
#[repr(i32)]
57+
#[non_exhaustive]
5658
pub enum QuotaFmt {
5759
/// Use the original quota format.
5860
QFMT_VFS_OLD,

0 commit comments

Comments
 (0)