Skip to content

Commit 3b2c0a9

Browse files
committed
Minor changes to Ipv4Addr
* Impl IntoInner rather than AsInner for Ipv4Addr * Add some comments * Add test to show endiannes of Ipv4Addr display
1 parent 4bb4b96 commit 3b2c0a9

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

library/std/src/net/addr.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl SocketAddrV4 {
268268
inner: c::sockaddr_in {
269269
sin_family: c::AF_INET as c::sa_family_t,
270270
sin_port: htons(port),
271-
sin_addr: *ip.as_inner(),
271+
sin_addr: ip.into_inner(),
272272
..unsafe { mem::zeroed() }
273273
},
274274
}
@@ -286,6 +286,8 @@ impl SocketAddrV4 {
286286
/// ```
287287
#[stable(feature = "rust1", since = "1.0.0")]
288288
pub fn ip(&self) -> &Ipv4Addr {
289+
// SAFETY: `Ipv4Addr` is `#[repr(C)] struct { _: in_addr; }`.
290+
// It is safe to cast from `&in_addr` to `&Ipv4Addr`.
289291
unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) }
290292
}
291293

@@ -302,7 +304,7 @@ impl SocketAddrV4 {
302304
/// ```
303305
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
304306
pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
305-
self.inner.sin_addr = *new_ip.as_inner()
307+
self.inner.sin_addr = new_ip.into_inner()
306308
}
307309

308310
/// Returns the port number associated with this socket address.

library/std/src/net/ip.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::hash;
1212
use crate::io::Write as IoWrite;
1313
use crate::mem::transmute;
1414
use crate::sys::net::netc as c;
15-
use crate::sys_common::{AsInner, FromInner};
15+
use crate::sys_common::{AsInner, FromInner, IntoInner};
1616

1717
/// An IP address, either IPv4 or IPv6.
1818
///
@@ -909,8 +909,8 @@ impl Eq for Ipv4Addr {}
909909
#[stable(feature = "rust1", since = "1.0.0")]
910910
impl hash::Hash for Ipv4Addr {
911911
fn hash<H: hash::Hasher>(&self, s: &mut H) {
912-
// `inner` is #[repr(packed)], so we need to copy `s_addr`.
913-
{ self.inner.s_addr }.hash(s)
912+
// hashing in big endian
913+
self.inner.s_addr.hash(s)
914914
}
915915
}
916916

@@ -944,13 +944,14 @@ impl PartialOrd<IpAddr> for Ipv4Addr {
944944
#[stable(feature = "rust1", since = "1.0.0")]
945945
impl Ord for Ipv4Addr {
946946
fn cmp(&self, other: &Ipv4Addr) -> Ordering {
947+
// Compare as native endian
947948
u32::from_be(self.inner.s_addr).cmp(&u32::from_be(other.inner.s_addr))
948949
}
949950
}
950951

951-
impl AsInner<c::in_addr> for Ipv4Addr {
952-
fn as_inner(&self) -> &c::in_addr {
953-
&self.inner
952+
impl IntoInner<c::in_addr> for Ipv4Addr {
953+
fn into_inner(self) -> c::in_addr {
954+
self.inner
954955
}
955956
}
956957

@@ -2019,6 +2020,7 @@ mod tests {
20192020

20202021
#[test]
20212022
fn ipv4_addr_to_string() {
2023+
assert_eq!(Ipv4Addr::new(127, 0, 0, 1).to_string(), "127.0.0.1");
20222024
// Short address
20232025
assert_eq!(Ipv4Addr::new(1, 1, 1, 1).to_string(), "1.1.1.1");
20242026
// Long address

library/std/src/sys_common/net.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ impl UdpSocket {
585585

586586
pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
587587
let mreq = c::ip_mreq {
588-
imr_multiaddr: *multiaddr.as_inner(),
589-
imr_interface: *interface.as_inner(),
588+
imr_multiaddr: multiaddr.into_inner(),
589+
imr_interface: interface.into_inner(),
590590
};
591591
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_ADD_MEMBERSHIP, mreq)
592592
}
@@ -601,8 +601,8 @@ impl UdpSocket {
601601

602602
pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> {
603603
let mreq = c::ip_mreq {
604-
imr_multiaddr: *multiaddr.as_inner(),
605-
imr_interface: *interface.as_inner(),
604+
imr_multiaddr: multiaddr.into_inner(),
605+
imr_interface: interface.into_inner(),
606606
};
607607
setsockopt(&self.inner, c::IPPROTO_IP, c::IP_DROP_MEMBERSHIP, mreq)
608608
}

0 commit comments

Comments
 (0)