Skip to content

Commit 8d36e9a

Browse files
asomersLMJW
authored andcommitted
Constify many functions
Constify most functions that can be constified. The exceptions are mostly accessors for structs that have no const constructor.
1 parent 603ecf5 commit 8d36e9a

File tree

14 files changed

+42
-39
lines changed

14 files changed

+42
-39
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
3636
`#[non_exhaustive]`.
3737
(#[1474](https://github.com/nix-rust/nix/pull/1474))
3838

39+
- Many more functions, mostly contructors, are now `const`.
40+
(#[1476](https://github.com/nix-rust/nix/pull/1476))
41+
3942
### Fixed
4043

4144
- Added more errno definitions for better backwards compatibility with

src/errno.rs

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

@@ -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
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl Iterator for SignalIterator {
357357
}
358358

359359
impl Signal {
360-
pub fn iterator() -> SignalIterator {
360+
pub const fn iterator() -> SignalIterator {
361361
SignalIterator{next: 0}
362362
}
363363
}

src/sys/socket/addr.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ impl IpAddr {
373373
/// Create a new IpAddr that contains an IPv4 address.
374374
///
375375
/// The result will represent the IP address a.b.c.d
376-
pub fn new_v4(a: u8, b: u8, c: u8, d: u8) -> IpAddr {
376+
pub const fn new_v4(a: u8, b: u8, c: u8, d: u8) -> IpAddr {
377377
IpAddr::V4(Ipv4Addr::new(a, b, c, d))
378378
}
379379

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

@@ -421,11 +421,11 @@ pub struct Ipv4Addr(pub libc::in_addr);
421421

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

430430
Ipv4Addr(libc::in_addr { s_addr: ip })
431431
}
@@ -437,16 +437,16 @@ impl Ipv4Addr {
437437
Ipv4Addr::new(bits[0], bits[1], bits[2], bits[3])
438438
}
439439

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

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

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

@@ -497,11 +497,11 @@ impl Ipv6Addr {
497497
}
498498

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

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

918-
pub fn pid(&self) -> u32 {
918+
pub const fn pid(&self) -> u32 {
919919
self.0.nl_pid
920920
}
921921

922-
pub fn groups(&self) -> u32 {
922+
pub const fn groups(&self) -> u32 {
923923
self.0.nl_groups
924924
}
925925
}
@@ -1022,7 +1022,7 @@ pub mod sys_control {
10221022
pub struct SysControlAddr(pub libc::sockaddr_ctl);
10231023

10241024
impl SysControlAddr {
1025-
pub fn new(id: u32, unit: u32) -> SysControlAddr {
1025+
pub const fn new(id: u32, unit: u32) -> SysControlAddr {
10261026
let addr = libc::sockaddr_ctl {
10271027
sc_len: mem::size_of::<libc::sockaddr_ctl>() as c_uchar,
10281028
sc_family: AddressFamily::System as c_uchar,
@@ -1049,11 +1049,11 @@ pub mod sys_control {
10491049
Ok(SysControlAddr::new(info.ctl_id, unit))
10501050
}
10511051

1052-
pub fn id(&self) -> u32 {
1052+
pub const fn id(&self) -> u32 {
10531053
self.0.sc_id
10541054
}
10551055

1056-
pub fn unit(&self) -> u32 {
1056+
pub const fn unit(&self) -> u32 {
10571057
self.0.sc_unit
10581058
}
10591059
}

src/sys/socket/mod.rs

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

387387
impl Ipv6MembershipRequest {
388388
/// Instantiate a new `Ipv6MembershipRequest`
389-
pub fn new(group: Ipv6Addr) -> Self {
389+
pub const fn new(group: Ipv6Addr) -> Self {
390390
Ipv6MembershipRequest(libc::ipv6_mreq {
391391
ipv6mr_multiaddr: group.0,
392392
ipv6mr_interface: 0,

src/sys/stat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ pub fn mknodat<P: ?Sized + NixPath>(
6969
}
7070

7171
#[cfg(target_os = "linux")]
72-
pub fn major(dev: dev_t) -> u64 {
72+
pub const fn major(dev: dev_t) -> u64 {
7373
((dev >> 32) & 0xffff_f000) |
7474
((dev >> 8) & 0x0000_0fff)
7575
}
7676

7777
#[cfg(target_os = "linux")]
78-
pub fn minor(dev: dev_t) -> u64 {
78+
pub const fn minor(dev: dev_t) -> u64 {
7979
((dev >> 12) & 0xffff_ff00) |
8080
((dev ) & 0x0000_00ff)
8181
}
8282

8383
#[cfg(target_os = "linux")]
84-
pub fn makedev(major: u64, minor: u64) -> dev_t {
84+
pub const fn makedev(major: u64, minor: u64) -> dev_t {
8585
((major & 0xffff_f000) << 32) |
8686
((major & 0x0000_0fff) << 8) |
8787
((minor & 0xffff_ff00) << 12) |

src/sys/time.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ impl TimeSpec {
187187
}
188188

189189
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
190-
pub fn tv_sec(&self) -> time_t {
190+
pub const fn tv_sec(&self) -> time_t {
191191
self.0.tv_sec
192192
}
193193

194-
pub fn tv_nsec(&self) -> timespec_tv_nsec_t {
194+
pub const fn tv_nsec(&self) -> timespec_tv_nsec_t {
195195
self.0.tv_nsec
196196
}
197197

@@ -404,11 +404,11 @@ impl TimeVal {
404404
}
405405

406406
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
407-
pub fn tv_sec(&self) -> time_t {
407+
pub const fn tv_sec(&self) -> time_t {
408408
self.0.tv_sec
409409
}
410410

411-
pub fn tv_usec(&self) -> suseconds_t {
411+
pub const fn tv_usec(&self) -> suseconds_t {
412412
self.0.tv_usec
413413
}
414414
}

src/sys/timerfd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ bitflags! {
8888
struct TimerSpec(libc::itimerspec);
8989

9090
impl TimerSpec {
91-
pub fn none() -> Self {
91+
pub const fn none() -> Self {
9292
Self(libc::itimerspec {
9393
it_interval: libc::timespec {
9494
tv_sec: 0,

src/time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct ClockId(clockid_t);
2020

2121
impl ClockId {
2222
/// Creates `ClockId` from raw `clockid_t`
23-
pub fn from_raw(clk_id: clockid_t) -> Self {
23+
pub const fn from_raw(clk_id: clockid_t) -> Self {
2424
ClockId(clk_id)
2525
}
2626

@@ -61,7 +61,7 @@ impl ClockId {
6161
}
6262

6363
/// Gets the raw `clockid_t` wrapped by `self`
64-
pub fn as_raw(self) -> clockid_t {
64+
pub const fn as_raw(self) -> clockid_t {
6565
self.0
6666
}
6767

0 commit comments

Comments
 (0)