Skip to content

Commit 3c45e08

Browse files
bors[bot]lucabasomers
authored
1473: sys/stat: add a safe wrapper for mknodat(2) r=asomers a=lucab This introduces a new `mknodat` helper. Ref: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html 1474: Mark most C-derived enums as non_exhaustive r=asomers a=asomers Since libc may add new variants at any time, Nix's consumers should not use exhaustive match patterns. Fixes #1182 1476: Constify many functions r=asomers a=asomers Constify most functions that can be constified. The exceptions are mostly accessors for structs that have no const constructor. Co-authored-by: Luca BRUNO <[email protected]> Co-authored-by: Alan Somers <[email protected]>
4 parents 62b227c + 6e2158b + 358adaf + 86acc26 commit 3c45e08

26 files changed

+150
-43
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1616
(#[1471](https://github.com/nix-rust/nix/pull/1471))
1717
- Added `pthread_kill`.
1818
(#[1472](https://github.com/nix-rust/nix/pull/1472))
19+
- Added `mknodat`.
20+
(#[1473](https://github.com/nix-rust/nix/pull/1473))
1921

2022
### Changed
2123

@@ -30,6 +32,13 @@ This project adheres to [Semantic Versioning](https://semver.org/).
3032
builds.
3133
(#[1481](https://github.com/nix-rust/nix/pull/1481))
3234

35+
- Most enums that come from C, for example `Errno`, are now marked as
36+
`#[non_exhaustive]`.
37+
(#[1474](https://github.com/nix-rust/nix/pull/1474))
38+
39+
- Many more functions, mostly contructors, are now `const`.
40+
(#[1476](https://github.com/nix-rust/nix/pull/1476))
41+
3342
### Fixed
3443

3544
- Added more errno definitions for better backwards compatibility with

src/errno.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Errno {
6363
since = "0.22.0",
6464
note = "It's a no-op now; just delete it."
6565
)]
66-
pub fn as_errno(self) -> Option<Self> {
66+
pub const fn as_errno(self) -> Option<Self> {
6767
Some(self)
6868
}
6969

@@ -81,7 +81,7 @@ impl Errno {
8181
since = "0.22.0",
8282
note = "Use Errno::EINVAL instead"
8383
)]
84-
pub fn invalid_argument() -> Error {
84+
pub const fn invalid_argument() -> Error {
8585
Errno::EINVAL
8686
}
8787

@@ -122,7 +122,7 @@ impl Errno {
122122
)]
123123
#[allow(non_snake_case)]
124124
#[inline]
125-
pub fn Sys(errno: Errno) -> Error {
125+
pub const fn Sys(errno: Errno) -> Error {
126126
errno
127127
}
128128
}
@@ -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

+4
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/features.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod os {
9797
#[cfg(any(target_os = "illumos"))]
9898
mod os {
9999
/// Check if the OS supports atomic close-on-exec for sockets
100-
pub fn socket_atomic_cloexec() -> bool {
100+
pub const fn socket_atomic_cloexec() -> bool {
101101
true
102102
}
103103
}
@@ -109,7 +109,7 @@ mod os {
109109
target_os = "solaris"))]
110110
mod os {
111111
/// Check if the OS supports atomic close-on-exec for sockets
112-
pub fn socket_atomic_cloexec() -> bool {
112+
pub const fn socket_atomic_cloexec() -> bool {
113113
false
114114
}
115115
}

src/mount/bsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl NmountError {
111111
}
112112

113113
/// Returns the inner [`Error`]
114-
pub fn error(&self) -> Error {
114+
pub const fn error(&self) -> Error {
115115
self.errno
116116
}
117117

src/mqueue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl MqAttr {
5959
}
6060
}
6161

62-
pub fn flags(&self) -> mq_attr_member_t {
62+
pub const fn flags(&self) -> mq_attr_member_t {
6363
self.mq_attr.mq_flags
6464
}
6565
}

src/poll.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct PollFd {
2525
impl PollFd {
2626
/// Creates a new `PollFd` specifying the events of interest
2727
/// for a given file descriptor.
28-
pub fn new(fd: RawFd, events: PollFlags) -> PollFd {
28+
pub const fn new(fd: RawFd, events: PollFlags) -> PollFd {
2929
PollFd {
3030
pollfd: libc::pollfd {
3131
fd,

src/sched.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod sched_linux_like {
9797
}
9898

9999
/// Return the maximum number of CPU in CpuSet
100-
pub fn count() -> usize {
100+
pub const fn count() -> usize {
101101
8 * mem::size_of::<libc::cpu_set_t>()
102102
}
103103
}

src/sys/aio.rs

+2
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

+1
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

+1
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

+1
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

+1
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

+2
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

+2
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,

src/sys/reboot.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ libc_enum! {
1212
/// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for
1313
/// enabling/disabling Ctrl-Alt-Delete.
1414
#[repr(i32)]
15+
#[non_exhaustive]
1516
pub enum RebootMode {
1617
RB_HALT_SYSTEM,
1718
RB_KEXEC,

src/sys/signal.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ libc_enum!{
2323
// We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately
2424
// this is not (yet) possible.
2525
#[repr(i32)]
26+
#[non_exhaustive]
2627
pub enum Signal {
2728
SIGHUP,
2829
SIGINT,
@@ -356,7 +357,7 @@ impl Iterator for SignalIterator {
356357
}
357358

358359
impl Signal {
359-
pub fn iterator() -> SignalIterator {
360+
pub const fn iterator() -> SignalIterator {
360361
SignalIterator{next: 0}
361362
}
362363
}
@@ -396,6 +397,7 @@ libc_bitflags!{
396397

397398
libc_enum! {
398399
#[repr(i32)]
400+
#[non_exhaustive]
399401
pub enum SigmaskHow {
400402
SIG_BLOCK,
401403
SIG_UNBLOCK,

0 commit comments

Comments
 (0)