Skip to content

Commit 164fde0

Browse files
refactor: replace IpVersion::to_socket_addr with Into<IpAddr> bound
1 parent e59c8f6 commit 164fde0

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

src/ip_version.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
use std::{
22
hash::Hash,
3-
net::{Ipv4Addr, Ipv6Addr},
3+
net::{IpAddr, Ipv4Addr, Ipv6Addr},
44
};
55

66
/// Either an [`Ipv4Addr`] or an [`Ipv6Addr`].
7-
pub trait IpVersion: Copy + Hash + Eq + Unpin + Send + Sync + 'static + private::Sealed {}
7+
pub trait IpVersion:
8+
Copy + Hash + Eq + Unpin + Send + Sync + Into<IpAddr> + 'static + private::Sealed
9+
{
10+
}
811

912
impl IpVersion for Ipv4Addr {}
1013
impl IpVersion for Ipv6Addr {}
1114

1215
pub(crate) mod private {
13-
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
16+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
1417

1518
pub trait Sealed: Sized {
1619
const IS_V4: bool;
1720

18-
fn to_socket_addr(self) -> SocketAddr;
19-
2021
fn from_ip_addr(addr: IpAddr) -> Option<Self>;
2122
}
2223

2324
impl Sealed for Ipv4Addr {
2425
const IS_V4: bool = true;
2526

26-
fn to_socket_addr(self) -> SocketAddr {
27-
SocketAddr::new(IpAddr::V4(self), 0)
28-
}
29-
3027
fn from_ip_addr(addr: IpAddr) -> Option<Self> {
3128
match addr {
3229
IpAddr::V4(v4) => Some(v4),
@@ -37,10 +34,6 @@ pub(crate) mod private {
3734
impl Sealed for Ipv6Addr {
3835
const IS_V4: bool = false;
3936

40-
fn to_socket_addr(self) -> SocketAddr {
41-
SocketAddr::new(IpAddr::V6(self), 0)
42-
}
43-
4437
fn from_ip_addr(addr: IpAddr) -> Option<Self> {
4538
match addr {
4639
IpAddr::V4(_) => None,

src/raw_pinger/blocking.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
io,
44
marker::PhantomData,
55
mem::{self, MaybeUninit},
6-
net::IpAddr,
6+
net::{IpAddr, SocketAddr},
77
time::Duration,
88
};
99

@@ -31,7 +31,7 @@ impl<V: IpVersion> RawBlockingPinger<V> {
3131

3232
/// Send a ICMP ECHO request packet
3333
pub fn send_to(&self, addr: V, packet: &EchoRequestPacket<V>) -> io::Result<()> {
34-
let addr = addr.to_socket_addr();
34+
let addr = SocketAddr::new(addr.into(), 0);
3535
self.socket.send_to(packet.as_bytes(), addr).map(|_sent| ())
3636
}
3737

src/raw_pinger/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
io,
77
marker::PhantomData,
88
mem::{self, MaybeUninit},
9-
net::{IpAddr, Ipv4Addr, Ipv6Addr},
9+
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
1010
pin::Pin,
1111
task::{ready, Context, Poll},
1212
};
@@ -57,7 +57,7 @@ impl<V: IpVersion> RawPinger<V> {
5757
addr: V,
5858
packet: &EchoRequestPacket<V>,
5959
) -> Poll<io::Result<()>> {
60-
let addr = addr.to_socket_addr();
60+
let addr = SocketAddr::new(addr.into(), 0);
6161

6262
let result = ready!(self.socket.poll_write_to(cx, packet.as_bytes(), addr));
6363
Poll::Ready(result.map(|_sent| ()))

0 commit comments

Comments
 (0)