Skip to content

Commit b370810

Browse files
committed
Constify many functions
Constify most functions that can be constified. The exceptions are mostly accessors for structs that have no const constructor.
1 parent e88a6cf commit b370810

14 files changed

+58
-55
lines changed

CHANGELOG.md

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

20+
- Many more functions, mostly contructors, are now `const`.
21+
(#[1476](https://github.com/nix-rust/nix/pull/1476))
22+
2023
### Fixed
2124

2225
- Added more errno definitions for better backwards compatibility with

src/errno.rs

Lines changed: 12 additions & 12 deletions
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

@@ -93,7 +93,7 @@ impl Errno {
9393
desc(self)
9494
}
9595

96-
pub fn from_i32(err: i32) -> Errno {
96+
pub const fn from_i32(err: i32) -> Errno {
9797
from_i32(err)
9898
}
9999

@@ -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
}
@@ -927,7 +927,7 @@ mod consts {
927927
pub const ENOTSUP: Errno = Errno::EOPNOTSUPP;
928928
}
929929

930-
pub fn from_i32(e: i32) -> Errno {
930+
pub const fn from_i32(e: i32) -> Errno {
931931
use self::Errno::*;
932932

933933
match e {
@@ -1205,7 +1205,7 @@ mod consts {
12051205
pub const EDEADLOCK: Errno = Errno::EDEADLK;
12061206
}
12071207

1208-
pub fn from_i32(e: i32) -> Errno {
1208+
pub const fn from_i32(e: i32) -> Errno {
12091209
use self::Errno::*;
12101210

12111211
match e {
@@ -1452,7 +1452,7 @@ mod consts {
14521452
pub const EOPNOTSUPP: Errno = Errno::ENOTSUP;
14531453
}
14541454

1455-
pub fn from_i32(e: i32) -> Errno {
1455+
pub const fn from_i32(e: i32) -> Errno {
14561456
use self::Errno::*;
14571457

14581458
match e {
@@ -1688,7 +1688,7 @@ mod consts {
16881688
pub const EOPNOTSUPP: Errno = Errno::ENOTSUP;
16891689
}
16901690

1691-
pub fn from_i32(e: i32) -> Errno {
1691+
pub const fn from_i32(e: i32) -> Errno {
16921692
use self::Errno::*;
16931693

16941694
match e {
@@ -1911,7 +1911,7 @@ mod consts {
19111911
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
19121912
}
19131913

1914-
pub fn from_i32(e: i32) -> Errno {
1914+
pub const fn from_i32(e: i32) -> Errno {
19151915
use self::Errno::*;
19161916

19171917
match e {
@@ -2135,7 +2135,7 @@ mod consts {
21352135
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
21362136
}
21372137

2138-
pub fn from_i32(e: i32) -> Errno {
2138+
pub const fn from_i32(e: i32) -> Errno {
21392139
use self::Errno::*;
21402140

21412141
match e {
@@ -2343,7 +2343,7 @@ mod consts {
23432343
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
23442344
}
23452345

2346-
pub fn from_i32(e: i32) -> Errno {
2346+
pub const fn from_i32(e: i32) -> Errno {
23472347
use self::Errno::*;
23482348

23492349
match e {
@@ -2582,7 +2582,7 @@ mod consts {
25822582
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
25832583
}
25842584

2585-
pub fn from_i32(e: i32) -> Errno {
2585+
pub const fn from_i32(e: i32) -> Errno {
25862586
use self::Errno::*;
25872587

25882588
match e {

src/features.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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/signal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl Signal {
132132
/// This function is equivalent to `<Signal as AsRef<str>>::as_ref()`,
133133
/// with difference that returned string is `'static`
134134
/// and not bound to `self`'s lifetime.
135-
pub fn as_str(self) -> &'static str {
135+
pub const fn as_str(self) -> &'static str {
136136
match self {
137137
Signal::SIGHUP => "SIGHUP",
138138
Signal::SIGINT => "SIGINT",
@@ -356,7 +356,7 @@ impl Iterator for SignalIterator {
356356
}
357357

358358
impl Signal {
359-
pub fn iterator() -> SignalIterator {
359+
pub const fn iterator() -> SignalIterator {
360360
SignalIterator{next: 0}
361361
}
362362
}

src/sys/socket/addr.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl AddressFamily {
236236
///
237237
/// Currently only supports these address families: Unix, Inet (v4 & v6), Netlink, Link/Packet
238238
/// and System. Returns None for unsupported or unknown address families.
239-
pub fn from_i32(family: i32) -> Option<AddressFamily> {
239+
pub const fn from_i32(family: i32) -> Option<AddressFamily> {
240240
match family {
241241
libc::AF_UNIX => Some(AddressFamily::Unix),
242242
libc::AF_INET => Some(AddressFamily::Inet),
@@ -372,7 +372,7 @@ impl IpAddr {
372372
/// Create a new IpAddr that contains an IPv4 address.
373373
///
374374
/// The result will represent the IP address a.b.c.d
375-
pub fn new_v4(a: u8, b: u8, c: u8, d: u8) -> IpAddr {
375+
pub const fn new_v4(a: u8, b: u8, c: u8, d: u8) -> IpAddr {
376376
IpAddr::V4(Ipv4Addr::new(a, b, c, d))
377377
}
378378

@@ -381,18 +381,18 @@ impl IpAddr {
381381
/// The result will represent the IP address a:b:c:d:e:f
382382
#[allow(clippy::many_single_char_names)]
383383
#[allow(clippy::too_many_arguments)]
384-
pub fn new_v6(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> IpAddr {
384+
pub const fn new_v6(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> IpAddr {
385385
IpAddr::V6(Ipv6Addr::new(a, b, c, d, e, f, g, h))
386386
}
387387

388-
pub fn from_std(std: &net::IpAddr) -> IpAddr {
388+
pub const fn from_std(std: &net::IpAddr) -> IpAddr {
389389
match *std {
390390
net::IpAddr::V4(ref std) => IpAddr::V4(Ipv4Addr::from_std(std)),
391391
net::IpAddr::V6(ref std) => IpAddr::V6(Ipv6Addr::from_std(std)),
392392
}
393393
}
394394

395-
pub fn to_std(&self) -> net::IpAddr {
395+
pub const fn to_std(&self) -> net::IpAddr {
396396
match *self {
397397
IpAddr::V4(ref ip) => net::IpAddr::V4(ip.to_std()),
398398
IpAddr::V6(ref ip) => net::IpAddr::V6(ip.to_std()),
@@ -420,32 +420,32 @@ pub struct Ipv4Addr(pub libc::in_addr);
420420

421421
impl Ipv4Addr {
422422
#[allow(clippy::identity_op)] // More readable this way
423-
pub fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
424-
let ip = ((u32::from(a) << 24) |
425-
(u32::from(b) << 16) |
426-
(u32::from(c) << 8) |
427-
(u32::from(d) << 0)).to_be();
423+
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
424+
let ip = (((a as u32) << 24) |
425+
((b as u32) << 16) |
426+
((c as u32) << 8) |
427+
((d as u32) << 0)).to_be();
428428

429429
Ipv4Addr(libc::in_addr { s_addr: ip })
430430
}
431431

432432
// Use pass by reference for symmetry with Ipv6Addr::from_std
433433
#[allow(clippy::trivially_copy_pass_by_ref)]
434-
pub fn from_std(std: &net::Ipv4Addr) -> Ipv4Addr {
434+
pub const fn from_std(std: &net::Ipv4Addr) -> Ipv4Addr {
435435
let bits = std.octets();
436436
Ipv4Addr::new(bits[0], bits[1], bits[2], bits[3])
437437
}
438438

439-
pub fn any() -> Ipv4Addr {
439+
pub const fn any() -> Ipv4Addr {
440440
Ipv4Addr(libc::in_addr { s_addr: libc::INADDR_ANY })
441441
}
442442

443-
pub fn octets(self) -> [u8; 4] {
443+
pub const fn octets(self) -> [u8; 4] {
444444
let bits = u32::from_be(self.0.s_addr);
445445
[(bits >> 24) as u8, (bits >> 16) as u8, (bits >> 8) as u8, bits as u8]
446446
}
447447

448-
pub fn to_std(self) -> net::Ipv4Addr {
448+
pub const fn to_std(self) -> net::Ipv4Addr {
449449
let bits = self.octets();
450450
net::Ipv4Addr::new(bits[0], bits[1], bits[2], bits[3])
451451
}
@@ -486,21 +486,21 @@ macro_rules! to_u16_array {
486486
impl Ipv6Addr {
487487
#[allow(clippy::many_single_char_names)]
488488
#[allow(clippy::too_many_arguments)]
489-
pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
489+
pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
490490
Ipv6Addr(libc::in6_addr{s6_addr: to_u8_array!(a,b,c,d,e,f,g,h)})
491491
}
492492

493-
pub fn from_std(std: &net::Ipv6Addr) -> Ipv6Addr {
493+
pub const fn from_std(std: &net::Ipv6Addr) -> Ipv6Addr {
494494
let s = std.segments();
495495
Ipv6Addr::new(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7])
496496
}
497497

498498
/// Return the eight 16-bit segments that make up this address
499-
pub fn segments(&self) -> [u16; 8] {
499+
pub const fn segments(&self) -> [u16; 8] {
500500
to_u16_array!(self, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
501501
}
502502

503-
pub fn to_std(&self) -> net::Ipv6Addr {
503+
pub const fn to_std(&self) -> net::Ipv6Addr {
504504
let s = self.segments();
505505
net::Ipv6Addr::new(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7])
506506
}
@@ -913,11 +913,11 @@ pub mod netlink {
913913
NetlinkAddr(addr)
914914
}
915915

916-
pub fn pid(&self) -> u32 {
916+
pub const fn pid(&self) -> u32 {
917917
self.0.nl_pid
918918
}
919919

920-
pub fn groups(&self) -> u32 {
920+
pub const fn groups(&self) -> u32 {
921921
self.0.nl_groups
922922
}
923923
}
@@ -1020,7 +1020,7 @@ pub mod sys_control {
10201020
pub struct SysControlAddr(pub libc::sockaddr_ctl);
10211021

10221022
impl SysControlAddr {
1023-
pub fn new(id: u32, unit: u32) -> SysControlAddr {
1023+
pub const fn new(id: u32, unit: u32) -> SysControlAddr {
10241024
let addr = libc::sockaddr_ctl {
10251025
sc_len: mem::size_of::<libc::sockaddr_ctl>() as c_uchar,
10261026
sc_family: AddressFamily::System as c_uchar,
@@ -1047,11 +1047,11 @@ pub mod sys_control {
10471047
Ok(SysControlAddr::new(info.ctl_id, unit))
10481048
}
10491049

1050-
pub fn id(&self) -> u32 {
1050+
pub const fn id(&self) -> u32 {
10511051
self.0.sc_id
10521052
}
10531053

1054-
pub fn unit(&self) -> u32 {
1054+
pub const fn unit(&self) -> u32 {
10551055
self.0.sc_unit
10561056
}
10571057
}

src/sys/socket/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ pub struct Ipv6MembershipRequest(libc::ipv6_mreq);
384384

385385
impl Ipv6MembershipRequest {
386386
/// Instantiate a new `Ipv6MembershipRequest`
387-
pub fn new(group: Ipv6Addr) -> Self {
387+
pub const fn new(group: Ipv6Addr) -> Self {
388388
Ipv6MembershipRequest(libc::ipv6_mreq {
389389
ipv6mr_multiaddr: group.0,
390390
ipv6mr_interface: 0,

src/sys/stat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ pub fn mknod<P: ?Sized + NixPath>(path: &P, kind: SFlag, perm: Mode, dev: dev_t)
5252
}
5353

5454
#[cfg(target_os = "linux")]
55-
pub fn major(dev: dev_t) -> u64 {
55+
pub const fn major(dev: dev_t) -> u64 {
5656
((dev >> 32) & 0xffff_f000) |
5757
((dev >> 8) & 0x0000_0fff)
5858
}
5959

6060
#[cfg(target_os = "linux")]
61-
pub fn minor(dev: dev_t) -> u64 {
61+
pub const fn minor(dev: dev_t) -> u64 {
6262
((dev >> 12) & 0xffff_ff00) |
6363
((dev ) & 0x0000_00ff)
6464
}
6565

6666
#[cfg(target_os = "linux")]
67-
pub fn makedev(major: u64, minor: u64) -> dev_t {
67+
pub const fn makedev(major: u64, minor: u64) -> dev_t {
6868
((major & 0xffff_f000) << 32) |
6969
((major & 0x0000_0fff) << 8) |
7070
((minor & 0xffff_ff00) << 12) |

0 commit comments

Comments
 (0)