Skip to content

Commit

Permalink
Added const keyword in functions
Browse files Browse the repository at this point in the history
Signed-off-by: carlosb1 <mcflurry0@gmail.com>
  • Loading branch information
carlosb1 committed Oct 4, 2023
1 parent 870459f commit 16a667d
Showing 1 changed file with 86 additions and 32 deletions.
118 changes: 86 additions & 32 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
))]
#[cfg(feature = "net")]
pub use self::datalink::LinkAddr;
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
pub use self::vsock::VsockAddr;
use super::sa_family_t;
use crate::errno::Errno;
Expand All @@ -41,15 +45,15 @@ use std::{fmt, mem, net, ptr, slice};
#[cfg(feature = "net")]
pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr {
libc::in_addr {
s_addr: u32::from_ne_bytes(addr.octets())
s_addr: u32::from_ne_bytes(addr.octets()),
}
}

/// Convert a std::net::Ipv6Addr into the libc form.
#[cfg(feature = "net")]
pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> libc::in6_addr {
libc::in6_addr {
s6_addr: addr.octets()
s6_addr: addr.octets(),
}
}

Expand Down Expand Up @@ -252,7 +256,11 @@ pub enum AddressFamily {
#[cfg_attr(docsrs, doc(cfg(all())))]
Nfc = libc::AF_NFC,
/// VMWare VSockets protocol for hypervisor-guest interaction.
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
Vsock = libc::AF_VSOCK,
/// ARPANet IMP addresses
Expand Down Expand Up @@ -447,7 +455,11 @@ impl AddressFamily {
target_os = "openbsd"
))]
libc::AF_LINK => Some(AddressFamily::Link),
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
libc::AF_VSOCK => Some(AddressFamily::Vsock),
_ => None,
}
Expand Down Expand Up @@ -488,7 +500,7 @@ enum UnixAddrKind<'a> {
}
impl<'a> UnixAddrKind<'a> {
/// Safety: sun & sun_len must be valid
#[allow(clippy::unnecessary_cast)] // Not unnecessary on all platforms
#[allow(clippy::unnecessary_cast)] // Not unnecessary on all platforms
unsafe fn get(sun: &'a libc::sockaddr_un, sun_len: u8) -> Self {
assert!(sun_len as usize >= offset_of!(libc::sockaddr_un, sun_path));
let path_len =
Expand Down Expand Up @@ -525,7 +537,7 @@ impl<'a> UnixAddrKind<'a> {

impl UnixAddr {
/// Create a new sockaddr_un representing a filesystem path.
#[allow(clippy::unnecessary_cast)] // Not unnecessary on all platforms
#[allow(clippy::unnecessary_cast)] // Not unnecessary on all platforms
pub fn new<P: ?Sized + NixPath>(path: &P) -> Result<UnixAddr> {
path.with_nix_path(|cstr| unsafe {
let mut ret = libc::sockaddr_un {
Expand Down Expand Up @@ -573,7 +585,7 @@ impl UnixAddr {
/// processes to communicate with processes having a different filesystem view.
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[allow(clippy::unnecessary_cast)] // Not unnecessary on all platforms
#[allow(clippy::unnecessary_cast)] // Not unnecessary on all platforms
pub fn new_abstract(path: &[u8]) -> Result<UnixAddr> {
unsafe {
let mut ret = libc::sockaddr_un {
Expand Down Expand Up @@ -770,7 +782,10 @@ impl SockaddrLike for UnixAddr {
mem::size_of::<libc::sockaddr_un>() as libc::socklen_t
}

unsafe fn set_length(&mut self, new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> {
unsafe fn set_length(
&mut self,
new_length: usize,
) -> std::result::Result<(), SocketAddressLengthNotDynamic> {
// `new_length` is only used on some platforms, so it must be provided even when not used
#![allow(unused_variables)]
cfg_if! {
Expand Down Expand Up @@ -946,7 +961,10 @@ pub trait SockaddrLike: private::SockaddrLikePriv {
/// `new_length` must be a valid length for this type of address. Specifically, reads of that
/// length from `self` must be valid.
#[doc(hidden)]
unsafe fn set_length(&mut self, _new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> {
unsafe fn set_length(
&mut self,
_new_length: usize,
) -> std::result::Result<(), SocketAddressLengthNotDynamic> {
Err(SocketAddressLengthNotDynamic)
}
}
Expand Down Expand Up @@ -1006,7 +1024,7 @@ pub struct SockaddrIn(libc::sockaddr_in);
impl SockaddrIn {
/// Returns the IP address associated with this socket address, in native
/// endian.
pub fn ip(&self) -> net::Ipv4Addr {
pub const fn ip(&self) -> net::Ipv4Addr {
net::Ipv4Addr::from(self.0.sin_addr.s_addr.to_ne_bytes())
}

Expand Down Expand Up @@ -1143,7 +1161,7 @@ impl SockaddrIn6 {
}

/// Returns the IP address associated with this socket address.
pub fn ip(&self) -> net::Ipv6Addr {
pub const fn ip(&self) -> net::Ipv6Addr {
net::Ipv6Addr::from(self.0.sin6_addr.s6_addr)
}

Expand Down Expand Up @@ -1293,7 +1311,11 @@ pub union SockaddrStorage {
sin6: SockaddrIn6,
ss: libc::sockaddr_storage,
su: UnixAddr,
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos" ))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
vsock: VsockAddr,
}
Expand Down Expand Up @@ -1385,7 +1407,11 @@ impl SockaddrLike for SockaddrStorage {
libc::AF_SYSTEM => {
SysControlAddr::from_raw(addr, l).map(|sctl| Self { sctl })
}
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos" ))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
libc::AF_VSOCK => {
VsockAddr::from_raw(addr, l).map(|vsock| Self { vsock })
}
Expand All @@ -1409,11 +1435,12 @@ impl SockaddrLike for SockaddrStorage {
}
}

unsafe fn set_length(&mut self, new_length: usize) -> std::result::Result<(), SocketAddressLengthNotDynamic> {
unsafe fn set_length(
&mut self,
new_length: usize,
) -> std::result::Result<(), SocketAddressLengthNotDynamic> {
match self.as_unix_addr_mut() {
Some(addr) => {
addr.set_length(new_length)
},
Some(addr) => addr.set_length(new_length),
None => Err(SocketAddressLengthNotDynamic),
}
}
Expand Down Expand Up @@ -1561,7 +1588,11 @@ impl SockaddrStorage {
accessors! {as_sys_control_addr, as_sys_control_addr_mut, SysControlAddr,
AddressFamily::System, libc::sockaddr_ctl, sctl}

#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
accessors! {as_vsock_addr, as_vsock_addr_mut, VsockAddr,
AddressFamily::Vsock, libc::sockaddr_vm, vsock}
Expand Down Expand Up @@ -1611,7 +1642,11 @@ impl fmt::Display for SockaddrStorage {
#[cfg(feature = "ioctl")]
libc::AF_SYSTEM => self.sctl.fmt(f),
libc::AF_UNIX => self.su.fmt(f),
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
libc::AF_VSOCK => self.vsock.fmt(f),
_ => "<Address family unspecified>".fmt(f),
}
Expand Down Expand Up @@ -1685,7 +1720,11 @@ impl Hash for SockaddrStorage {
#[cfg(feature = "ioctl")]
libc::AF_SYSTEM => self.sctl.hash(s),
libc::AF_UNIX => self.su.hash(s),
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
libc::AF_VSOCK => self.vsock.hash(s),
_ => self.ss.hash(s),
}
Expand Down Expand Up @@ -1727,7 +1766,11 @@ impl PartialEq for SockaddrStorage {
#[cfg(feature = "ioctl")]
(libc::AF_SYSTEM, libc::AF_SYSTEM) => self.sctl == other.sctl,
(libc::AF_UNIX, libc::AF_UNIX) => self.su == other.su,
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "macos"
))]
(libc::AF_VSOCK, libc::AF_VSOCK) => self.vsock == other.vsock,
_ => false,
}
Expand Down Expand Up @@ -1918,16 +1961,12 @@ pub mod alg {

/// Return the socket's cipher type, for example `hash` or `aead`.
pub fn alg_type(&self) -> &CStr {
unsafe {
CStr::from_ptr(self.0.salg_type.as_ptr().cast())
}
unsafe { CStr::from_ptr(self.0.salg_type.as_ptr().cast()) }
}

/// Return the socket's cipher name, for example `sha1`.
pub fn alg_name(&self) -> &CStr {
unsafe {
CStr::from_ptr(self.0.salg_name.as_ptr().cast())
}
unsafe { CStr::from_ptr(self.0.salg_name.as_ptr().cast()) }
}
}

Expand Down Expand Up @@ -2331,8 +2370,17 @@ pub mod vsock {
#[cfg(target_os = "macos")]
fn eq(&self, other: &Self) -> bool {
let (inner, other) = (self.0, other.0);
(inner.svm_family, inner.svm_cid, inner.svm_port, inner.svm_len)
== (other.svm_family, other.svm_cid, other.svm_port, inner.svm_len)
(
inner.svm_family,
inner.svm_cid,
inner.svm_port,
inner.svm_len,
) == (
other.svm_family,
other.svm_cid,
other.svm_port,
inner.svm_len,
)
}
}

Expand All @@ -2347,7 +2395,13 @@ pub mod vsock {
#[cfg(target_os = "macos")]
fn hash<H: Hasher>(&self, s: &mut H) {
let inner = self.0;
(inner.svm_family, inner.svm_cid, inner.svm_port, inner.svm_len).hash(s);
(
inner.svm_family,
inner.svm_cid,
inner.svm_port,
inner.svm_len,
)
.hash(s);
}
}

Expand All @@ -2365,7 +2419,7 @@ pub mod vsock {

#[cfg(target_os = "macos")]
{
addr.svm_len = std::mem::size_of::<sockaddr_vm>() as u8;
addr.svm_len = std::mem::size_of::<sockaddr_vm>() as u8;
}
VsockAddr(addr)
}
Expand Down

0 comments on commit 16a667d

Please sign in to comment.