Skip to content

Commit

Permalink
Replace uses of mem::transmute with safe methods
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Jan 26, 2023
1 parent 15d624a commit ea2f370
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ use std::{fmt, mem, net, ptr, slice};
/// Convert a std::net::Ipv4Addr into the libc form.
#[cfg(feature = "net")]
pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr {
static_assertions::assert_eq_size!(net::Ipv4Addr, libc::in_addr);
// Safe because both types have the same memory layout, and no fancy Drop
// impls.
unsafe { mem::transmute(addr) }
libc::in_addr {
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 {
static_assertions::assert_eq_size!(net::Ipv6Addr, libc::in6_addr);
// Safe because both are Newtype wrappers around the same libc type
unsafe { mem::transmute(*addr) }
libc::in6_addr {
s6_addr: addr.octets(),
}
}

/// These constants specify the protocol family to be used
Expand Down

0 comments on commit ea2f370

Please sign in to comment.