Skip to content

Commit 8f82d3e

Browse files
committed
Use cfg_if! in a separated private function for the ifa_ifu field.
1 parent 4e7441e commit 8f82d3e

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/ifaddrs.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ pub struct InterfaceAddress {
3030
pub destination: Option<SockAddr>,
3131
}
3232

33+
cfg_if! {
34+
if #[cfg(any(target_os = "emscripten", target_os = "fuchsia", target_os="linux"))] {
35+
fn get_ifu_from_sockaddr(info: &libc::ifaddrs) -> *const libc::sockaddr {
36+
info.ifa_ifu
37+
}
38+
} else {
39+
fn get_ifu_from_sockaddr(info: &libc::ifaddrs) -> *const libc::sockaddr {
40+
info.ifa_dstaddr;
41+
}
42+
}
43+
}
44+
3345
impl InterfaceAddress {
3446
/// Create an `InterfaceAddress` from the libc struct.
3547
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
@@ -45,14 +57,7 @@ impl InterfaceAddress {
4557
destination: None,
4658
};
4759

48-
let ifu : *mut libc::sockaddr;
49-
50-
#[cfg(any(target_os = "linux", target_os = "emscripten", target_os = "fuchsia"))]
51-
{ ifu = info.ifa_ifu; }
52-
53-
#[cfg(not(any(target_os = "linux", target_os = "emscripten", target_os = "fuchsia")))]
54-
{ ifu = info.ifa_dstaddr; }
55-
60+
let ifu = get_ifu_from_sockaddr(info);
5661
if addr.flags.contains(IFF_POINTOPOINT) {
5762
addr.destination = unsafe { SockAddr::from_libc_sockaddr(ifu) };
5863
} else if addr.flags.contains(IFF_BROADCAST) {

src/sys/socket/addr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ impl SockAddr {
706706
}
707707

708708
/// Creates a `SockAddr` struct from libc's sockaddr.
709-
pub unsafe fn from_libc_sockaddr(addr: *mut libc::sockaddr) -> Option<SockAddr> {
709+
pub unsafe fn from_libc_sockaddr(addr: *const libc::sockaddr) -> Option<SockAddr> {
710710
if addr.is_null() {
711711
None
712712
} else {

0 commit comments

Comments
 (0)