Skip to content

Commit 3ca28f6

Browse files
bors[bot]asomers
andauthored
Merge #1685
1685: Deprecate IpAddr, Ipv4Addr, and Ipv6Addr r=rtzoeller a=asomers Because they're redundant with types in the standard library. Fixes #1681 Co-authored-by: Alan Somers <[email protected]>
2 parents 0fe6682 + fbeabf3 commit 3ca28f6

File tree

4 files changed

+87
-16
lines changed

4 files changed

+87
-16
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
7777
- Deprecated `InetAddr` and `SockAddr` in favor of `SockaddrIn`, `SockaddrIn6`,
7878
and `SockaddrStorage`.
7979
(#[1684](https://github.com/nix-rust/nix/pull/1684))
80+
- Deprecated `IpAddr`, `Ipv4Addr`, and `Ipv6Addr` in favor of their equivalents
81+
from the standard library.
82+
(#[1685](https://github.com/nix-rust/nix/pull/1685))
8083

8184
### Fixed
8285

src/sys/socket/addr.rs

+65-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ pub use self::datalink::LinkAddr;
3232
#[cfg(any(target_os = "android", target_os = "linux"))]
3333
pub use self::vsock::VsockAddr;
3434

35+
/// Convert a std::net::Ipv4Addr into the libc form.
36+
#[cfg(feature = "net")]
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))
44+
}
45+
}
46+
47+
/// Convert a std::net::Ipv6Addr into the libc form.
48+
#[cfg(feature = "net")]
49+
pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> libc::in6_addr {
50+
libc::in6_addr {
51+
s6_addr: addr.octets()
52+
}
53+
}
54+
3555
/// These constants specify the protocol family to be used
3656
/// in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
3757
///
@@ -497,14 +517,20 @@ impl fmt::Display for InetAddr {
497517
* ===== IpAddr =====
498518
*
499519
*/
500-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
520+
#[allow(missing_docs)] // Since they're all deprecated anyway
521+
#[allow(deprecated)]
501522
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
523+
#[deprecated(
524+
since = "0.24.0",
525+
note = "Use std::net::IpAddr instead"
526+
)]
502527
pub enum IpAddr {
503528
V4(Ipv4Addr),
504529
V6(Ipv6Addr),
505530
}
506531

507-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
532+
#[allow(deprecated)]
533+
#[allow(missing_docs)] // Since they're all deprecated anyway
508534
impl IpAddr {
509535
/// Create a new IpAddr that contains an IPv4 address.
510536
///
@@ -537,6 +563,7 @@ impl IpAddr {
537563
}
538564
}
539565

566+
#[allow(deprecated)]
540567
impl fmt::Display for IpAddr {
541568
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
542569
match *self {
@@ -552,12 +579,17 @@ impl fmt::Display for IpAddr {
552579
*
553580
*/
554581

555-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
582+
#[deprecated(
583+
since = "0.24.0",
584+
note = "Use std::net::Ipv4Addr instead"
585+
)]
586+
#[allow(missing_docs)] // Since they're all deprecated anyway
556587
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
557588
#[repr(transparent)]
558589
pub struct Ipv4Addr(pub libc::in_addr);
559590

560-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
591+
#[allow(deprecated)]
592+
#[allow(missing_docs)] // Since they're all deprecated anyway
561593
impl Ipv4Addr {
562594
#[allow(clippy::identity_op)] // More readable this way
563595
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
@@ -591,6 +623,7 @@ impl Ipv4Addr {
591623
}
592624
}
593625

626+
#[allow(deprecated)]
594627
impl fmt::Display for Ipv4Addr {
595628
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
596629
let octets = self.octets();
@@ -604,7 +637,11 @@ impl fmt::Display for Ipv4Addr {
604637
*
605638
*/
606639

607-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
640+
#[deprecated(
641+
since = "0.24.0",
642+
note = "Use std::net::Ipv6Addr instead"
643+
)]
644+
#[allow(missing_docs)] // Since they're all deprecated anyway
608645
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
609646
#[repr(transparent)]
610647
pub struct Ipv6Addr(pub libc::in6_addr);
@@ -625,7 +662,8 @@ macro_rules! to_u16_array {
625662
}
626663
}
627664

628-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
665+
#[allow(deprecated)]
666+
#[allow(missing_docs)] // Since they're all deprecated anyway
629667
impl Ipv6Addr {
630668
#[allow(clippy::many_single_char_names)]
631669
#[allow(clippy::too_many_arguments)]
@@ -649,6 +687,7 @@ impl Ipv6Addr {
649687
}
650688
}
651689

690+
#[allow(deprecated)]
652691
impl fmt::Display for Ipv6Addr {
653692
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
654693
self.to_std().fmt(fmt)
@@ -1172,7 +1211,7 @@ impl From<net::SocketAddrV4> for SockaddrIn {
11721211
sin_len: mem::size_of::<libc::sockaddr_in>() as u8,
11731212
sin_family: AddressFamily::Inet as sa_family_t,
11741213
sin_port: addr.port().to_be(), // network byte order
1175-
sin_addr: Ipv4Addr::from_std(addr.ip()).0,
1214+
sin_addr: ipv4addr_to_libc(*addr.ip()),
11761215
.. unsafe { mem::zeroed() }
11771216
})
11781217
}
@@ -1266,7 +1305,7 @@ impl From<net::SocketAddrV6> for SockaddrIn6 {
12661305
sin6_len: mem::size_of::<libc::sockaddr_in6>() as u8,
12671306
sin6_family: AddressFamily::Inet6 as sa_family_t,
12681307
sin6_port: addr.port().to_be(), // network byte order
1269-
sin6_addr: Ipv6Addr::from_std(addr.ip()).0,
1308+
sin6_addr: ipv6addr_to_libc(addr.ip()),
12701309
sin6_flowinfo: addr.flowinfo(), // host byte order
12711310
sin6_scope_id: addr.scope_id(), // host byte order
12721311
.. unsafe { mem::zeroed() }
@@ -2545,6 +2584,24 @@ pub mod vsock {
25452584
mod tests {
25462585
use super::*;
25472586

2587+
mod types {
2588+
use super::*;
2589+
2590+
#[test]
2591+
fn test_ipv4addr_to_libc() {
2592+
let s = std::net::Ipv4Addr::new(1, 2, 3, 4);
2593+
let l = ipv4addr_to_libc(s);
2594+
assert_eq!(l.s_addr, u32::to_be(0x01020304));
2595+
}
2596+
2597+
#[test]
2598+
fn test_ipv6addr_to_libc() {
2599+
let s = std::net::Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8);
2600+
let l = ipv6addr_to_libc(&s);
2601+
assert_eq!(l.s6_addr, [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]);
2602+
}
2603+
}
2604+
25482605
mod link {
25492606
#[cfg(any(target_os = "ios",
25502607
target_os = "macos",

src/sys/socket/mod.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use libc::{self, c_void, c_int, iovec, socklen_t, size_t,
88
use std::convert::TryInto;
99
use std::{mem, ptr, slice};
1010
use std::os::unix::io::RawFd;
11+
#[cfg(feature = "net")]
12+
use std::net;
1113
#[cfg(target_os = "linux")]
1214
#[cfg(feature = "uio")]
1315
use crate::sys::time::TimeSpec;
@@ -93,6 +95,9 @@ pub use libc::{sockaddr_in, sockaddr_in6};
9395
#[doc(hidden)]
9496
pub use libc::{c_uint, CMSG_SPACE};
9597

98+
#[cfg(feature = "net")]
99+
use crate::sys::socket::addr::{ipv4addr_to_libc, ipv6addr_to_libc};
100+
96101
/// These constants are used to specify the communication semantics
97102
/// when creating a socket with [`socket()`](fn.socket.html)
98103
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -496,10 +501,16 @@ impl IpMembershipRequest {
496501
/// Instantiate a new `IpMembershipRequest`
497502
///
498503
/// If `interface` is `None`, then `Ipv4Addr::any()` will be used for the interface.
499-
pub fn new(group: Ipv4Addr, interface: Option<Ipv4Addr>) -> Self {
504+
pub fn new(group: net::Ipv4Addr, interface: Option<net::Ipv4Addr>)
505+
-> Self
506+
{
507+
let imr_addr = match interface {
508+
None => net::Ipv4Addr::UNSPECIFIED,
509+
Some(addr) => addr
510+
};
500511
IpMembershipRequest(libc::ip_mreq {
501-
imr_multiaddr: group.0,
502-
imr_interface: interface.unwrap_or_else(Ipv4Addr::any).0,
512+
imr_multiaddr: ipv4addr_to_libc(group),
513+
imr_interface: ipv4addr_to_libc(imr_addr)
503514
})
504515
}
505516
}
@@ -513,9 +524,9 @@ pub struct Ipv6MembershipRequest(libc::ipv6_mreq);
513524

514525
impl Ipv6MembershipRequest {
515526
/// Instantiate a new `Ipv6MembershipRequest`
516-
pub const fn new(group: Ipv6Addr) -> Self {
527+
pub const fn new(group: net::Ipv6Addr) -> Self {
517528
Ipv6MembershipRequest(libc::ipv6_mreq {
518-
ipv6mr_multiaddr: group.0,
529+
ipv6mr_multiaddr: ipv6addr_to_libc(&group),
519530
ipv6mr_interface: 0,
520531
})
521532
}

test/sys/test_socket.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ mod linux_errqueue {
18331833
if let Some(origin) = err_addr {
18341834
// Validate that our network error originated from 127.0.0.1:0.
18351835
assert_eq!(origin.sin_family, AddressFamily::Inet as _);
1836-
assert_eq!(Ipv4Addr(origin.sin_addr), Ipv4Addr::new(127, 0, 0, 1));
1836+
assert_eq!(origin.sin_addr.s_addr, u32::from_be(0x7f000001));
18371837
assert_eq!(origin.sin_port, 0);
18381838
} else {
18391839
panic!("Expected some error origin");
@@ -1877,8 +1877,8 @@ mod linux_errqueue {
18771877
// Validate that our network error originated from localhost:0.
18781878
assert_eq!(origin.sin6_family, AddressFamily::Inet6 as _);
18791879
assert_eq!(
1880-
Ipv6Addr(origin.sin6_addr),
1881-
Ipv6Addr::from_std(&"::1".parse().unwrap()),
1880+
origin.sin6_addr.s6_addr,
1881+
std::net::Ipv6Addr::LOCALHOST.octets()
18821882
);
18831883
assert_eq!(origin.sin6_port, 0);
18841884
} else {

0 commit comments

Comments
 (0)