Skip to content

Commit

Permalink
Rust 1.46.0 compatibility
Browse files Browse the repository at this point in the history
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
  • Loading branch information
asomers committed Mar 22, 2022
1 parent 5661f24 commit 677c656
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ targets = [
libc = { version = "0.2.121", features = [ "extra_traits" ] }
bitflags = "1.1"
cfg-if = "1.0"
static_assertions = "1.1.0"

[target.'cfg(not(target_os = "redox"))'.dependencies]
memoffset = { version = "0.6.3", optional = true }
Expand Down
28 changes: 11 additions & 17 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,24 @@ use crate::sys::socket::addr::sys_control::SysControlAddr;
pub use self::datalink::LinkAddr;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use self::vsock::VsockAddr;
#[cfg(feature = "net")]
use static_assertions::const_assert_eq;

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

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

Expand Down Expand Up @@ -1311,7 +1305,7 @@ impl From<net::SocketAddrV6> for SockaddrIn6 {
sin6_len: mem::size_of::<libc::sockaddr_in6>() as u8,
sin6_family: AddressFamily::Inet6 as sa_family_t,
sin6_port: addr.port().to_be(), // network byte order
sin6_addr: *ipv6addr_to_libc(addr.ip()),
sin6_addr: ipv6addr_to_libc(addr.ip()),
sin6_flowinfo: addr.flowinfo(), // host byte order
sin6_scope_id: addr.scope_id(), // host byte order
.. unsafe { mem::zeroed() }
Expand Down
10 changes: 6 additions & 4 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,13 @@ impl IpMembershipRequest {
pub fn new(group: net::Ipv4Addr, interface: Option<net::Ipv4Addr>)
-> Self
{
let imr_addr = match interface {
None => net::Ipv4Addr::UNSPECIFIED,
Some(addr) => addr
};
IpMembershipRequest(libc::ip_mreq {
imr_multiaddr: ipv4addr_to_libc(group),
imr_interface: ipv4addr_to_libc(
interface.unwrap_or(net::Ipv4Addr::UNSPECIFIED)
),
imr_interface: ipv4addr_to_libc(imr_addr)
})
}
}
Expand All @@ -524,7 +526,7 @@ impl Ipv6MembershipRequest {
/// Instantiate a new `Ipv6MembershipRequest`
pub const fn new(group: net::Ipv6Addr) -> Self {
Ipv6MembershipRequest(libc::ipv6_mreq {
ipv6mr_multiaddr: *ipv6addr_to_libc(&group),
ipv6mr_multiaddr: ipv6addr_to_libc(&group),
ipv6mr_interface: 0,
})
}
Expand Down

0 comments on commit 677c656

Please sign in to comment.