Skip to content

Commit

Permalink
refactor: replace IpVersion::to_socket_addr with Into<IpAddr> bound
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini committed Dec 30, 2024
1 parent e59c8f6 commit 164fde0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
19 changes: 6 additions & 13 deletions src/ip_version.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
use std::{
hash::Hash,
net::{Ipv4Addr, Ipv6Addr},
net::{IpAddr, Ipv4Addr, Ipv6Addr},
};

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

impl IpVersion for Ipv4Addr {}
impl IpVersion for Ipv6Addr {}

pub(crate) mod private {
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

pub trait Sealed: Sized {
const IS_V4: bool;

fn to_socket_addr(self) -> SocketAddr;

fn from_ip_addr(addr: IpAddr) -> Option<Self>;
}

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

fn to_socket_addr(self) -> SocketAddr {
SocketAddr::new(IpAddr::V4(self), 0)
}

fn from_ip_addr(addr: IpAddr) -> Option<Self> {
match addr {
IpAddr::V4(v4) => Some(v4),
Expand All @@ -37,10 +34,6 @@ pub(crate) mod private {
impl Sealed for Ipv6Addr {
const IS_V4: bool = false;

fn to_socket_addr(self) -> SocketAddr {
SocketAddr::new(IpAddr::V6(self), 0)
}

fn from_ip_addr(addr: IpAddr) -> Option<Self> {
match addr {
IpAddr::V4(_) => None,
Expand Down
4 changes: 2 additions & 2 deletions src/raw_pinger/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
io,
marker::PhantomData,
mem::{self, MaybeUninit},
net::IpAddr,
net::{IpAddr, SocketAddr},
time::Duration,
};

Expand Down Expand Up @@ -31,7 +31,7 @@ impl<V: IpVersion> RawBlockingPinger<V> {

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

Expand Down
4 changes: 2 additions & 2 deletions src/raw_pinger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
io,
marker::PhantomData,
mem::{self, MaybeUninit},
net::{IpAddr, Ipv4Addr, Ipv6Addr},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
pin::Pin,
task::{ready, Context, Poll},
};
Expand Down Expand Up @@ -57,7 +57,7 @@ impl<V: IpVersion> RawPinger<V> {
addr: V,
packet: &EchoRequestPacket<V>,
) -> Poll<io::Result<()>> {
let addr = addr.to_socket_addr();
let addr = SocketAddr::new(addr.into(), 0);

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

0 comments on commit 164fde0

Please sign in to comment.