Skip to content

Commit e7c2321

Browse files
committed
Deprecate IpAddr, Ipv4Addr, and Ipv6Addr
Because they're redundant with types in the standard library. Fixes #1681
1 parent 0fe6682 commit e7c2321

File tree

4 files changed

+89
-13
lines changed

4 files changed

+89
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ 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"
3334

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

src/sys/socket/addr.rs

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ 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;
36+
37+
/// 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.
40+
#[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)
46+
}
47+
}
48+
49+
/// 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.
52+
#[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)
58+
}
59+
}
3460

3561
/// These constants specify the protocol family to be used
3662
/// in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
@@ -497,14 +523,20 @@ impl fmt::Display for InetAddr {
497523
* ===== IpAddr =====
498524
*
499525
*/
500-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
526+
#[allow(missing_docs)] // Since they're all deprecated anyway
527+
#[allow(deprecated)]
501528
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
529+
#[deprecated(
530+
since = "0.24.0",
531+
note = "Use std::net::IpAddr instead"
532+
)]
502533
pub enum IpAddr {
503534
V4(Ipv4Addr),
504535
V6(Ipv6Addr),
505536
}
506537

507-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
538+
#[allow(deprecated)]
539+
#[allow(missing_docs)] // Since they're all deprecated anyway
508540
impl IpAddr {
509541
/// Create a new IpAddr that contains an IPv4 address.
510542
///
@@ -537,6 +569,7 @@ impl IpAddr {
537569
}
538570
}
539571

572+
#[allow(deprecated)]
540573
impl fmt::Display for IpAddr {
541574
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
542575
match *self {
@@ -552,12 +585,17 @@ impl fmt::Display for IpAddr {
552585
*
553586
*/
554587

555-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
588+
#[deprecated(
589+
since = "0.24.0",
590+
note = "Use std::net::Ipv4Addr instead"
591+
)]
592+
#[allow(missing_docs)] // Since they're all deprecated anyway
556593
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
557594
#[repr(transparent)]
558595
pub struct Ipv4Addr(pub libc::in_addr);
559596

560-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
597+
#[allow(deprecated)]
598+
#[allow(missing_docs)] // Since they're all deprecated anyway
561599
impl Ipv4Addr {
562600
#[allow(clippy::identity_op)] // More readable this way
563601
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
@@ -591,6 +629,7 @@ impl Ipv4Addr {
591629
}
592630
}
593631

632+
#[allow(deprecated)]
594633
impl fmt::Display for Ipv4Addr {
595634
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
596635
let octets = self.octets();
@@ -604,7 +643,11 @@ impl fmt::Display for Ipv4Addr {
604643
*
605644
*/
606645

607-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
646+
#[deprecated(
647+
since = "0.24.0",
648+
note = "Use std::net::Ipv6Addr instead"
649+
)]
650+
#[allow(missing_docs)] // Since they're all deprecated anyway
608651
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
609652
#[repr(transparent)]
610653
pub struct Ipv6Addr(pub libc::in6_addr);
@@ -625,7 +668,8 @@ macro_rules! to_u16_array {
625668
}
626669
}
627670

628-
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
671+
#[allow(deprecated)]
672+
#[allow(missing_docs)] // Since they're all deprecated anyway
629673
impl Ipv6Addr {
630674
#[allow(clippy::many_single_char_names)]
631675
#[allow(clippy::too_many_arguments)]
@@ -649,6 +693,7 @@ impl Ipv6Addr {
649693
}
650694
}
651695

696+
#[allow(deprecated)]
652697
impl fmt::Display for Ipv6Addr {
653698
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
654699
self.to_std().fmt(fmt)
@@ -1172,7 +1217,7 @@ impl From<net::SocketAddrV4> for SockaddrIn {
11721217
sin_len: mem::size_of::<libc::sockaddr_in>() as u8,
11731218
sin_family: AddressFamily::Inet as sa_family_t,
11741219
sin_port: addr.port().to_be(), // network byte order
1175-
sin_addr: Ipv4Addr::from_std(addr.ip()).0,
1220+
sin_addr: ipv4addr_to_libc(*addr.ip()),
11761221
.. unsafe { mem::zeroed() }
11771222
})
11781223
}
@@ -1266,7 +1311,7 @@ impl From<net::SocketAddrV6> for SockaddrIn6 {
12661311
sin6_len: mem::size_of::<libc::sockaddr_in6>() as u8,
12671312
sin6_family: AddressFamily::Inet6 as sa_family_t,
12681313
sin6_port: addr.port().to_be(), // network byte order
1269-
sin6_addr: Ipv6Addr::from_std(addr.ip()).0,
1314+
sin6_addr: *ipv6addr_to_libc(&addr.ip()),
12701315
sin6_flowinfo: addr.flowinfo(), // host byte order
12711316
sin6_scope_id: addr.scope_id(), // host byte order
12721317
.. unsafe { mem::zeroed() }
@@ -2545,6 +2590,24 @@ pub mod vsock {
25452590
mod tests {
25462591
use super::*;
25472592

2593+
mod types {
2594+
use super::*;
2595+
2596+
#[test]
2597+
fn test_ipv4addr_to_libc() {
2598+
let s = std::net::Ipv4Addr::new(1, 2, 3, 4);
2599+
let l = ipv4addr_to_libc(s);
2600+
assert_eq!(l.s_addr, u32::to_be(0x01020304));
2601+
}
2602+
2603+
#[test]
2604+
fn test_ipv6addr_to_libc() {
2605+
let s = std::net::Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8);
2606+
let l = ipv6addr_to_libc(&s);
2607+
assert_eq!(l.s6_addr, [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]);
2608+
}
2609+
}
2610+
25482611
mod link {
25492612
#[cfg(any(target_os = "ios",
25502613
target_os = "macos",

src/sys/socket/mod.rs

Lines changed: 14 additions & 5 deletions
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,14 @@ 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+
{
500507
IpMembershipRequest(libc::ip_mreq {
501-
imr_multiaddr: group.0,
502-
imr_interface: interface.unwrap_or_else(Ipv4Addr::any).0,
508+
imr_multiaddr: ipv4addr_to_libc(group),
509+
imr_interface: ipv4addr_to_libc(
510+
interface.unwrap_or(net::Ipv4Addr::UNSPECIFIED)
511+
),
503512
})
504513
}
505514
}
@@ -513,9 +522,9 @@ pub struct Ipv6MembershipRequest(libc::ipv6_mreq);
513522

514523
impl Ipv6MembershipRequest {
515524
/// Instantiate a new `Ipv6MembershipRequest`
516-
pub const fn new(group: Ipv6Addr) -> Self {
525+
pub const fn new(group: net::Ipv6Addr) -> Self {
517526
Ipv6MembershipRequest(libc::ipv6_mreq {
518-
ipv6mr_multiaddr: group.0,
527+
ipv6mr_multiaddr: *ipv6addr_to_libc(&group),
519528
ipv6mr_interface: 0,
520529
})
521530
}

0 commit comments

Comments
 (0)