Skip to content

Commit

Permalink
constify more functions
Browse files Browse the repository at this point in the history
Constify more functions, since we're raising the MSRV from 1.41.0 to
1.46.0.

Fixes #1477
  • Loading branch information
asomers committed Aug 13, 2021
1 parent 5495bbc commit 7049d42
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

- Many more functions, mostly contructors, are now `const`.
(#[1476](https://github.com/nix-rust/nix/pull/1476))
(#[1492](https://github.com/nix-rust/nix/pull/1492))

- `sys::event::KEvent::filter` now returns a `Result` instead of being
infalliable. The only cases where it will now return an error are cases
Expand Down
18 changes: 9 additions & 9 deletions src/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Errno {
desc(self)
}

pub fn from_i32(err: i32) -> Errno {
pub const fn from_i32(err: i32) -> Errno {
from_i32(err)
}

Expand Down Expand Up @@ -928,7 +928,7 @@ mod consts {
pub const ENOTSUP: Errno = Errno::EOPNOTSUPP;
}

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

match e {
Expand Down Expand Up @@ -1207,7 +1207,7 @@ mod consts {
pub const EDEADLOCK: Errno = Errno::EDEADLK;
}

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

match e {
Expand Down Expand Up @@ -1455,7 +1455,7 @@ mod consts {
pub const EOPNOTSUPP: Errno = Errno::ENOTSUP;
}

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

match e {
Expand Down Expand Up @@ -1692,7 +1692,7 @@ mod consts {
pub const EOPNOTSUPP: Errno = Errno::ENOTSUP;
}

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

match e {
Expand Down Expand Up @@ -1916,7 +1916,7 @@ mod consts {
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
}

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

match e {
Expand Down Expand Up @@ -2141,7 +2141,7 @@ mod consts {
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
}

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

match e {
Expand Down Expand Up @@ -2350,7 +2350,7 @@ mod consts {
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
}

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

match e {
Expand Down Expand Up @@ -2590,7 +2590,7 @@ mod consts {
pub const EWOULDBLOCK: Errno = Errno::EAGAIN;
}

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

match e {
Expand Down
2 changes: 1 addition & 1 deletion src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Signal {
/// This function is equivalent to `<Signal as AsRef<str>>::as_ref()`,
/// with difference that returned string is `'static`
/// and not bound to `self`'s lifetime.
pub fn as_str(self) -> &'static str {
pub const fn as_str(self) -> &'static str {
match self {
Signal::SIGHUP => "SIGHUP",
Signal::SIGINT => "SIGINT",
Expand Down
8 changes: 4 additions & 4 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl AddressFamily {
///
/// Currently only supports these address families: Unix, Inet (v4 & v6), Netlink, Link/Packet
/// and System. Returns None for unsupported or unknown address families.
pub fn from_i32(family: i32) -> Option<AddressFamily> {
pub const fn from_i32(family: i32) -> Option<AddressFamily> {
match family {
libc::AF_UNIX => Some(AddressFamily::Unix),
libc::AF_INET => Some(AddressFamily::Inet),
Expand Down Expand Up @@ -314,15 +314,15 @@ impl InetAddr {
}
}
/// Gets the IP address associated with this socket address.
pub fn ip(&self) -> IpAddr {
pub const fn ip(&self) -> IpAddr {
match *self {
InetAddr::V4(ref sa) => IpAddr::V4(Ipv4Addr(sa.sin_addr)),
InetAddr::V6(ref sa) => IpAddr::V6(Ipv6Addr(sa.sin6_addr)),
}
}

/// Gets the port number associated with this socket address
pub fn port(&self) -> u16 {
pub const fn port(&self) -> u16 {
match *self {
InetAddr::V6(ref sa) => u16::from_be(sa.sin6_port),
InetAddr::V4(ref sa) => u16::from_be(sa.sin_port),
Expand Down Expand Up @@ -393,7 +393,7 @@ impl IpAddr {
}
}

pub fn to_std(&self) -> net::IpAddr {
pub const fn to_std(&self) -> net::IpAddr {
match *self {
IpAddr::V4(ref ip) => net::IpAddr::V4(ip.to_std()),
IpAddr::V6(ref ip) => net::IpAddr::V6(ip.to_std()),
Expand Down

0 comments on commit 7049d42

Please sign in to comment.