Skip to content

Commit dda32e4

Browse files
author
Vita Batrla
committed
refactor fix using cfg_if!
1 parent 34878d7 commit dda32e4

File tree

1 file changed

+55
-120
lines changed

1 file changed

+55
-120
lines changed

src/libstd/sys_common/net.rs

+55-120
Original file line numberDiff line numberDiff line change
@@ -12,80 +12,43 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
1212
use crate::time::Duration;
1313

1414
use libc::{c_int, c_void};
15-
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",
16-
target_os = "openbsd", target_os = "netbsd",
17-
target_os = "solaris"))]
18-
use libc::{c_uchar};
19-
20-
#[cfg(not(any(
21-
target_os = "dragonfly",
22-
target_os = "freebsd",
23-
target_os = "ios",
24-
target_os = "macos",
25-
target_os = "openbsd",
26-
target_os = "netbsd",
27-
target_os = "solaris",
28-
target_os = "haiku",
29-
target_os = "l4re"
30-
)))]
31-
use crate::sys::net::netc::IPV6_ADD_MEMBERSHIP;
32-
#[cfg(not(any(
33-
target_os = "dragonfly",
34-
target_os = "freebsd",
35-
target_os = "ios",
36-
target_os = "macos",
37-
target_os = "openbsd",
38-
target_os = "netbsd",
39-
target_os = "solaris",
40-
target_os = "haiku",
41-
target_os = "l4re"
42-
)))]
43-
use crate::sys::net::netc::IPV6_DROP_MEMBERSHIP;
44-
#[cfg(any(
45-
target_os = "dragonfly",
46-
target_os = "freebsd",
47-
target_os = "ios",
48-
target_os = "macos",
49-
target_os = "openbsd",
50-
target_os = "netbsd",
51-
target_os = "solaris",
52-
target_os = "haiku",
53-
target_os = "l4re"
54-
))]
55-
use crate::sys::net::netc::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP;
56-
#[cfg(any(
57-
target_os = "dragonfly",
58-
target_os = "freebsd",
59-
target_os = "ios",
60-
target_os = "macos",
61-
target_os = "openbsd",
62-
target_os = "netbsd",
63-
target_os = "solaris",
64-
target_os = "haiku",
65-
target_os = "l4re"
66-
))]
67-
use crate::sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP;
68-
69-
#[cfg(any(
70-
target_os = "linux",
71-
target_os = "android",
72-
target_os = "dragonfly",
73-
target_os = "freebsd",
74-
target_os = "openbsd",
75-
target_os = "netbsd",
76-
target_os = "haiku"
77-
))]
78-
use libc::MSG_NOSIGNAL;
79-
#[cfg(not(any(
80-
target_os = "linux",
81-
target_os = "android",
82-
target_os = "dragonfly",
83-
target_os = "freebsd",
84-
target_os = "openbsd",
85-
target_os = "netbsd",
86-
target_os = "haiku"
87-
)))]
88-
const MSG_NOSIGNAL: c_int = 0x0;
15+
16+
cfg_if::cfg_if! {
17+
if #[cfg(any(
18+
target_os = "dragonfly", target_os = "freebsd",
19+
target_os = "ios", target_os = "macos",
20+
target_os = "openbsd", target_os = "netbsd",
21+
target_os = "solaris", target_os = "haiku", target_os = "l4re"))] {
22+
use crate::sys::net::netc::IPV6_JOIN_GROUP as IPV6_ADD_MEMBERSHIP;
23+
use crate::sys::net::netc::IPV6_LEAVE_GROUP as IPV6_DROP_MEMBERSHIP;
24+
} else {
25+
use crate::sys::net::netc::IPV6_ADD_MEMBERSHIP;
26+
use crate::sys::net::netc::IPV6_DROP_MEMBERSHIP;
27+
}
28+
}
29+
30+
cfg_if::cfg_if! {
31+
if #[cfg(any(
32+
target_os = "linux", target_os = "android",
33+
target_os = "dragonfly", target_os = "freebsd",
34+
target_os = "openbsd", target_os = "netbsd",
35+
target_os = "haiku"))] {
36+
use libc::MSG_NOSIGNAL;
37+
} else {
38+
const MSG_NOSIGNAL: c_int = 0x0;
39+
}
40+
}
41+
42+
cfg_if::cfg_if! {
43+
if #[cfg(any(
44+
target_os = "dragonfly", target_os = "freebsd",
45+
target_os = "openbsd", target_os = "netbsd",
46+
target_os = "solaris"))] {
47+
type ip_mcast_type_v4 = c_uchar;
48+
} else {
49+
type ip_mcast_type_v4 = c_int;
50+
}
51+
}
8952

9053
////////////////////////////////////////////////////////////////////////////////
9154
// sockaddr and misc bindings
@@ -569,6 +532,24 @@ impl UdpSocket {
569532
Ok(raw != 0)
570533
}
571534

535+
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
536+
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as ip_mcast_type_v4)
537+
}
538+
539+
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
540+
let raw: ip_mcast_type_v4 = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
541+
Ok(raw != 0)
542+
}
543+
544+
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
545+
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as ip_mcast_type_v4)
546+
}
547+
548+
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
549+
let raw: ip_mcast_type_v4 = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
550+
Ok(raw as u32)
551+
}
552+
572553
pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> {
573554
setsockopt(&self.inner, c::IPPROTO_IPV6, c::IPV6_MULTICAST_LOOP, multicast_loop_v6 as c_int)
574555
}
@@ -649,52 +630,6 @@ impl UdpSocket {
649630
}
650631
}
651632

652-
#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd",
653-
target_os = "openbsd", target_os = "netbsd",
654-
target_os = "solaris")))]
655-
impl UdpSocket {
656-
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
657-
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_int)
658-
}
659-
660-
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
661-
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
662-
Ok(raw != 0)
663-
}
664-
665-
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
666-
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_int)
667-
}
668-
669-
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
670-
let raw: c_int = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
671-
Ok(raw as u32)
672-
}
673-
}
674-
675-
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",
676-
target_os = "openbsd", target_os = "netbsd",
677-
target_os = "solaris"))]
678-
impl UdpSocket {
679-
pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> {
680-
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP, multicast_loop_v4 as c_uchar)
681-
}
682-
683-
pub fn multicast_loop_v4(&self) -> io::Result<bool> {
684-
let raw: c_uchar = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_LOOP)?;
685-
Ok(raw != 0)
686-
}
687-
688-
pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> {
689-
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, multicast_ttl_v4 as c_uchar)
690-
}
691-
692-
pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
693-
let raw: c_uchar = getsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL)?;
694-
Ok(raw as u32)
695-
}
696-
}
697-
698633
impl FromInner<Socket> for UdpSocket {
699634
fn from_inner(socket: Socket) -> UdpSocket {
700635
UdpSocket { inner: socket }

0 commit comments

Comments
 (0)