Skip to content

Commit 677c656

Browse files
committed
Rust 1.46.0 compatibility
Rust doesn't allow mem::transmute in a const fn until 1.56.0. And it doesn't allow Ipv4Addr::octets in a const fn until 1.50.0
1 parent 5661f24 commit 677c656

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ targets = [
3030
libc = { version = "0.2.121", features = [ "extra_traits" ] }
3131
bitflags = "1.1"
3232
cfg-if = "1.0"
33-
static_assertions = "1.1.0"
3433

3534
[target.'cfg(not(target_os = "redox"))'.dependencies]
3635
memoffset = { version = "0.6.3", optional = true }

src/sys/socket/addr.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,24 @@ use crate::sys::socket::addr::sys_control::SysControlAddr;
3131
pub use self::datalink::LinkAddr;
3232
#[cfg(any(target_os = "android", target_os = "linux"))]
3333
pub use self::vsock::VsockAddr;
34-
#[cfg(feature = "net")]
35-
use static_assertions::const_assert_eq;
3634

3735
/// Convert a std::net::Ipv4Addr into the libc form.
38-
// In the standard library, this is a simple newtype, but it doesn't expose
39-
// the inner libc type, so we must use a pointer cast.
4036
#[cfg(feature = "net")]
41-
pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr {
42-
const_assert_eq!(mem::size_of::<libc::in_addr>(),
43-
mem::size_of::<net::Ipv4Addr>());
44-
unsafe {
45-
*(&addr as *const net::Ipv4Addr as *const libc::in_addr)
37+
pub(crate) fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr {
38+
let octets = addr.octets();
39+
libc::in_addr {
40+
s_addr: u32::to_be(((octets[0] as u32) << 24) |
41+
((octets[1] as u32) << 16) |
42+
((octets[2] as u32) << 8) |
43+
(octets[3] as u32))
4644
}
4745
}
4846

4947
/// Convert a std::net::Ipv6Addr into the libc form.
50-
// In the standard library, this is a simple newtype, but it doesn't expose
51-
// the inner libc type, so we must use a pointer cast.
5248
#[cfg(feature = "net")]
53-
pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> &libc::in6_addr {
54-
const_assert_eq!(mem::size_of::<libc::in6_addr>(),
55-
mem::size_of::<net::Ipv6Addr>());
56-
unsafe {
57-
&*(addr as *const net::Ipv6Addr as *const libc::in6_addr)
49+
pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> libc::in6_addr {
50+
libc::in6_addr {
51+
s6_addr: addr.octets()
5852
}
5953
}
6054

@@ -1311,7 +1305,7 @@ impl From<net::SocketAddrV6> for SockaddrIn6 {
13111305
sin6_len: mem::size_of::<libc::sockaddr_in6>() as u8,
13121306
sin6_family: AddressFamily::Inet6 as sa_family_t,
13131307
sin6_port: addr.port().to_be(), // network byte order
1314-
sin6_addr: *ipv6addr_to_libc(addr.ip()),
1308+
sin6_addr: ipv6addr_to_libc(addr.ip()),
13151309
sin6_flowinfo: addr.flowinfo(), // host byte order
13161310
sin6_scope_id: addr.scope_id(), // host byte order
13171311
.. unsafe { mem::zeroed() }

src/sys/socket/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,13 @@ impl IpMembershipRequest {
504504
pub fn new(group: net::Ipv4Addr, interface: Option<net::Ipv4Addr>)
505505
-> Self
506506
{
507+
let imr_addr = match interface {
508+
None => net::Ipv4Addr::UNSPECIFIED,
509+
Some(addr) => addr
510+
};
507511
IpMembershipRequest(libc::ip_mreq {
508512
imr_multiaddr: ipv4addr_to_libc(group),
509-
imr_interface: ipv4addr_to_libc(
510-
interface.unwrap_or(net::Ipv4Addr::UNSPECIFIED)
511-
),
513+
imr_interface: ipv4addr_to_libc(imr_addr)
512514
})
513515
}
514516
}
@@ -524,7 +526,7 @@ impl Ipv6MembershipRequest {
524526
/// Instantiate a new `Ipv6MembershipRequest`
525527
pub const fn new(group: net::Ipv6Addr) -> Self {
526528
Ipv6MembershipRequest(libc::ipv6_mreq {
527-
ipv6mr_multiaddr: *ipv6addr_to_libc(&group),
529+
ipv6mr_multiaddr: ipv6addr_to_libc(&group),
528530
ipv6mr_interface: 0,
529531
})
530532
}

0 commit comments

Comments
 (0)